From 95199e473ad1fb5a6a68f7fdb6616b42dd9e02e6 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 5 Jun 2025 03:08:42 +0000 Subject: [PATCH] Update Gelbooru watcher to use post links --- cogs/gelbooru_watcher_base_cog.py | 27 +++++++++++++++++++++------ cogs/rule34_cog.py | 3 ++- cogs/safebooru_cog.py | 3 ++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cogs/gelbooru_watcher_base_cog.py b/cogs/gelbooru_watcher_base_cog.py index 2f5e64f..f726526 100644 --- a/cogs/gelbooru_watcher_base_cog.py +++ b/cogs/gelbooru_watcher_base_cog.py @@ -23,7 +23,17 @@ class GelbooruWatcherMeta(commands.CogMeta, abc.ABCMeta): pass class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMeta): - def __init__(self, bot: commands.Bot, cog_name: str, api_base_url: str, default_tags: str, is_nsfw_site: bool, command_group_name: str, main_command_name: str): + def __init__( + self, + bot: commands.Bot, + cog_name: str, + api_base_url: str, + default_tags: str, + is_nsfw_site: bool, + command_group_name: str, + main_command_name: str, + post_url_template: str, + ): self.bot = bot # Ensure super().__init__() is called for Cog's metaclass features, especially if 'name' was passed to Cog. # However, 'name' is handled by the derived classes (Rule34Cog, SafebooruCog) @@ -39,6 +49,7 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet self.is_nsfw_site = is_nsfw_site self.command_group_name = command_group_name self.main_command_name = main_command_name + self.post_url_template = post_url_template self.cache_file = f"{self.cog_name.lower()}_cache.json" self.subscriptions_file = f"{self.cog_name.lower()}_subscriptions.json" @@ -516,7 +527,7 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet except ValueError: await interaction.response.send_message("Please enter a valid number",ephemeral=True) - def _build_new_post_view(self, tags: str, file_url: str) -> ui.LayoutView: + def _build_new_post_view(self, tags: str, file_url: str, post_id: int) -> ui.LayoutView: view = ui.LayoutView(timeout=None) container = ui.Container() view.add_item(container) @@ -525,7 +536,8 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet gallery.add_item(media=file_url) container.add_item(gallery) container.add_item(ui.TextDisplay(f"New {self.cog_name} post for tags `{tags}`:")) - container.add_item(ui.TextDisplay(file_url)) + post_url = self.post_url_template.format(post_id) + container.add_item(ui.TextDisplay(post_url)) return view @@ -661,7 +673,7 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet post_id = int(new_post["id"]) target_thread_id: typing.Optional[str] = sub.get("target_post_id") or sub.get("thread_id") - view = self._build_new_post_view(tags, new_post["file_url"]) + view = self._build_new_post_view(tags, new_post["file_url"], post_id) send_success = await self._send_via_webhook( webhook_url, @@ -1028,18 +1040,21 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet fetched = await self._fetch_posts_logic("internal_test_msg", tags, pid_override=0, limit_override=1) file_url = None + post_id = None if isinstance(fetched, list) and fetched: first = fetched[0] if isinstance(first, dict): file_url = first.get("file_url") + if "id" in first: + post_id = int(first["id"]) - if not file_url: + if not file_url or post_id is None: await interaction.response.send_message( f"❌ Failed to fetch a post for tags `{tags}`.", ephemeral=True ) return - view = self._build_new_post_view(tags, file_url) + view = self._build_new_post_view(tags, file_url, post_id) thread_id = target_sub.get("target_post_id") or target_sub.get("thread_id") send_success = await self._send_via_webhook( webhook_url, content="", thread_id=thread_id, view=view diff --git a/cogs/rule34_cog.py b/cogs/rule34_cog.py index 77a597b..a1fa951 100644 --- a/cogs/rule34_cog.py +++ b/cogs/rule34_cog.py @@ -86,7 +86,8 @@ class Rule34Cog(GelbooruWatcherBaseCog): # Removed name="Rule34" default_tags="kasane_teto breast_milk", # Example default, will be overridden if tags are required is_nsfw_site=True, command_group_name="r34watch", # For potential use in base class messages - main_command_name="rule34" # For potential use in base class messages + main_command_name="rule34", # For potential use in base class messages + post_url_template="https://rule34.xxx/index.php?page=post&s=view&id={}" ) # Initialize Google GenAI Client for Vertex AI try: diff --git a/cogs/safebooru_cog.py b/cogs/safebooru_cog.py index 897328c..4e5aa97 100644 --- a/cogs/safebooru_cog.py +++ b/cogs/safebooru_cog.py @@ -21,7 +21,8 @@ class SafebooruCog(GelbooruWatcherBaseCog): # Removed name="Safebooru" default_tags="hatsune_miku 1girl", # Example default is_nsfw_site=False, # Safebooru is generally SFW command_group_name="safebooruwatch", - main_command_name="safebooru" + main_command_name="safebooru", + post_url_template="https://safebooru.org/index.php?page=post&s=view&id={}" ) # --- Prefix Command ---