Update Gelbooru watcher to use post links

This commit is contained in:
Codex 2025-06-05 03:08:42 +00:00
parent 6336ef9db1
commit 95199e473a
3 changed files with 25 additions and 8 deletions

View File

@ -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

View File

@ -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:

View File

@ -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 ---