Update Gelbooru watcher to use layout components
This commit is contained in:
parent
d21350f02d
commit
09d4fc3270
@ -3,6 +3,7 @@ import discord
|
||||
from discord.ext import commands, tasks
|
||||
from discord import app_commands
|
||||
from discord.ui import Button, View
|
||||
from discord import ui
|
||||
import random
|
||||
import aiohttp
|
||||
import time
|
||||
@ -190,7 +191,13 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
log.exception(f"Unexpected error creating webhook in {channel.mention} ({self.cog_name})")
|
||||
return None
|
||||
|
||||
async def _send_via_webhook(self, webhook_url: str, content: str, thread_id: typing.Optional[str] = None):
|
||||
async def _send_via_webhook(
|
||||
self,
|
||||
webhook_url: str,
|
||||
content: str = "",
|
||||
thread_id: typing.Optional[str] = None,
|
||||
view: typing.Optional[discord.ui.View] = None,
|
||||
):
|
||||
if not self.session or self.session.closed:
|
||||
self.session = aiohttp.ClientSession()
|
||||
log.info(f"Recreated aiohttp.ClientSession in _send_via_webhook for {self.cog_name}")
|
||||
@ -208,7 +215,8 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
content=content,
|
||||
username=f"{self.bot.user.name} {self.cog_name} Watcher" if self.bot.user else f"{self.cog_name} Watcher",
|
||||
avatar_url=self.bot.user.display_avatar.url if self.bot.user and self.bot.user.display_avatar else None,
|
||||
thread=target_thread_obj
|
||||
thread=target_thread_obj,
|
||||
view=view,
|
||||
)
|
||||
log.debug(f"Sent message via webhook to {webhook_url[:30]}... (Thread: {thread_id if thread_id else 'None'}) ({self.cog_name})")
|
||||
return True
|
||||
@ -506,6 +514,16 @@ 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:
|
||||
view = ui.LayoutView(timeout=None)
|
||||
container = ui.Container()
|
||||
view.add_item(container)
|
||||
section = ui.Section(accessory=ui.Thumbnail(media=file_url, description="Post"))
|
||||
container.add_item(section)
|
||||
section.add_item(ui.TextDisplay(f"New {self.cog_name} post for tags `{tags}`:"))
|
||||
container.add_item(ui.TextDisplay(file_url))
|
||||
return view
|
||||
|
||||
async def _prefix_command_logic(self, ctx: commands.Context, tags: str):
|
||||
# Loading message is handled by _fetch_posts_logic if ctx is passed
|
||||
response = await self._fetch_posts_logic(ctx, tags)
|
||||
@ -636,11 +654,16 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
latest_sent_id_for_this_sub = last_known_post_id
|
||||
for new_post in new_posts_to_send:
|
||||
post_id = int(new_post["id"])
|
||||
message_content = f"New {self.cog_name} post for tags `{tags}`:\n{new_post['file_url']}"
|
||||
|
||||
target_thread_id: typing.Optional[str] = sub.get("target_post_id") or sub.get("thread_id")
|
||||
|
||||
send_success = await self._send_via_webhook(webhook_url, message_content, thread_id=target_thread_id)
|
||||
view = self._build_new_post_view(tags, new_post["file_url"])
|
||||
|
||||
send_success = await self._send_via_webhook(
|
||||
webhook_url,
|
||||
content="",
|
||||
thread_id=target_thread_id,
|
||||
view=view,
|
||||
)
|
||||
if send_success:
|
||||
latest_sent_id_for_this_sub = post_id
|
||||
original_guild_subs = self.subscriptions_data.get(guild_id_str)
|
||||
|
Loading…
x
Reference in New Issue
Block a user