refactor: integrate message components with buttons
This commit is contained in:
parent
6f00297b84
commit
f1c511b2eb
@ -2,7 +2,6 @@ import os
|
||||
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
|
||||
@ -406,6 +405,9 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
return (f"<{post_url}>\n{random_result['file_url']}", all_results)
|
||||
|
||||
class GelbooruButtons(ui.LayoutView):
|
||||
container = ui.Container()
|
||||
buttons = ui.ActionRow()
|
||||
|
||||
def __init__(self, cog: 'GelbooruWatcherBaseCog', tags: str, all_results: list, hidden: bool = False):
|
||||
super().__init__(timeout=300)
|
||||
self.cog = cog
|
||||
@ -414,8 +416,6 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
self.hidden = hidden
|
||||
self.current_index = 0
|
||||
|
||||
self.container = ui.Container()
|
||||
self.add_item(self.container)
|
||||
if self.all_results:
|
||||
self._update_container(random.choice(self.all_results))
|
||||
|
||||
@ -430,21 +430,21 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
post_url = self.cog.post_url_template.format(result["id"])
|
||||
self.container.add_item(ui.TextDisplay(post_url))
|
||||
|
||||
@discord.ui.button(label="New Random", style=discord.ButtonStyle.primary)
|
||||
async def new_random(self, interaction: discord.Interaction, button: Button):
|
||||
@buttons.button(label="New Random", style=discord.ButtonStyle.primary)
|
||||
async def new_random(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
random_result = random.choice(self.all_results)
|
||||
self._update_container(random_result)
|
||||
await interaction.response.edit_message(content="", view=self)
|
||||
|
||||
@discord.ui.button(label="Random In New Message", style=discord.ButtonStyle.success)
|
||||
async def new_message(self, interaction: discord.Interaction, button: Button):
|
||||
@buttons.button(label="Random In New Message", style=discord.ButtonStyle.success)
|
||||
async def new_message(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
random_result = random.choice(self.all_results)
|
||||
new_view = self.cog.GelbooruButtons(self.cog, self.tags, self.all_results, self.hidden)
|
||||
new_view._update_container(random_result)
|
||||
await interaction.response.send_message(content="", view=new_view, ephemeral=self.hidden)
|
||||
|
||||
@discord.ui.button(label="Browse Results", style=discord.ButtonStyle.secondary)
|
||||
async def browse_results(self, interaction: discord.Interaction, button: Button):
|
||||
@buttons.button(label="Browse Results", style=discord.ButtonStyle.secondary)
|
||||
async def browse_results(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
if not self.all_results:
|
||||
await interaction.response.send_message("No results to browse.", ephemeral=True)
|
||||
return
|
||||
@ -452,16 +452,22 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
view = self.cog.BrowseView(self.cog, self.tags, self.all_results, self.hidden, self.current_index)
|
||||
await interaction.response.edit_message(content="", view=view)
|
||||
|
||||
@discord.ui.button(label="Pin", style=discord.ButtonStyle.danger)
|
||||
async def pin_message(self, interaction: discord.Interaction, button: Button):
|
||||
@buttons.button(label="Pin", style=discord.ButtonStyle.danger)
|
||||
async def pin_message(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
if interaction.message:
|
||||
try:
|
||||
await interaction.message.pin()
|
||||
await interaction.response.send_message("Message pinned successfully!", ephemeral=True)
|
||||
except discord.Forbidden: await interaction.response.send_message("I don't have permission to pin messages.", ephemeral=True)
|
||||
except discord.HTTPException as e: await interaction.response.send_message(f"Failed to pin: {e}", ephemeral=True)
|
||||
except discord.Forbidden:
|
||||
await interaction.response.send_message("I don't have permission to pin messages.", ephemeral=True)
|
||||
except discord.HTTPException as e:
|
||||
await interaction.response.send_message(f"Failed to pin: {e}", ephemeral=True)
|
||||
|
||||
class BrowseView(ui.LayoutView):
|
||||
container = ui.Container()
|
||||
nav_row = ui.ActionRow()
|
||||
extra_row = ui.ActionRow()
|
||||
|
||||
def __init__(self, cog: 'GelbooruWatcherBaseCog', tags: str, all_results: list, hidden: bool = False, current_index: int = 0):
|
||||
super().__init__(timeout=300)
|
||||
self.cog = cog
|
||||
@ -470,8 +476,6 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
self.hidden = hidden
|
||||
self.current_index = current_index
|
||||
|
||||
self.container = ui.Container()
|
||||
self.add_item(self.container)
|
||||
if self.all_results:
|
||||
self._refresh_container()
|
||||
|
||||
@ -490,28 +494,28 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
self._refresh_container()
|
||||
await interaction.response.edit_message(content="", view=self)
|
||||
|
||||
@discord.ui.button(label="First", style=discord.ButtonStyle.secondary, emoji="⏪")
|
||||
async def first(self, interaction: discord.Interaction, button: Button):
|
||||
@nav_row.button(label="First", style=discord.ButtonStyle.secondary, emoji="⏪")
|
||||
async def first(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
self.current_index = 0
|
||||
await self._update_message(interaction)
|
||||
|
||||
@discord.ui.button(label="Previous", style=discord.ButtonStyle.secondary, emoji="◀️")
|
||||
async def previous(self, interaction: discord.Interaction, button: Button):
|
||||
@nav_row.button(label="Previous", style=discord.ButtonStyle.secondary, emoji="◀️")
|
||||
async def previous(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
self.current_index = (self.current_index - 1 + len(self.all_results)) % len(self.all_results)
|
||||
await self._update_message(interaction)
|
||||
|
||||
@discord.ui.button(label="Next", style=discord.ButtonStyle.primary, emoji="▶️")
|
||||
async def next_result(self, interaction: discord.Interaction, button: Button): # Renamed from next to avoid conflict
|
||||
@nav_row.button(label="Next", style=discord.ButtonStyle.primary, emoji="▶️")
|
||||
async def next_result(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
self.current_index = (self.current_index + 1) % len(self.all_results)
|
||||
await self._update_message(interaction)
|
||||
|
||||
@discord.ui.button(label="Last", style=discord.ButtonStyle.secondary, emoji="⏩")
|
||||
async def last(self, interaction: discord.Interaction, button: Button):
|
||||
@nav_row.button(label="Last", style=discord.ButtonStyle.secondary, emoji="⏩")
|
||||
async def last(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
self.current_index = len(self.all_results) - 1
|
||||
await self._update_message(interaction)
|
||||
|
||||
@discord.ui.button(label="Go To", style=discord.ButtonStyle.primary, row=1)
|
||||
async def goto(self, interaction: discord.Interaction, button: Button):
|
||||
@extra_row.button(label="Go To", style=discord.ButtonStyle.primary)
|
||||
async def goto(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
modal = self.cog.GoToModal(len(self.all_results))
|
||||
await interaction.response.send_modal(modal)
|
||||
await modal.wait()
|
||||
@ -521,9 +525,8 @@ class GelbooruWatcherBaseCog(commands.Cog, abc.ABC, metaclass=GelbooruWatcherMet
|
||||
interaction.message.id, content="", view=self
|
||||
)
|
||||
|
||||
|
||||
@discord.ui.button(label="Back to Main Controls", style=discord.ButtonStyle.danger, row=1)
|
||||
async def back_to_main(self, interaction: discord.Interaction, button: Button):
|
||||
@extra_row.button(label="Back to Main Controls", style=discord.ButtonStyle.danger)
|
||||
async def back_to_main(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
# Send a random one from all_results as the content
|
||||
if not self.all_results: # Should not happen if browse was initiated
|
||||
await interaction.response.edit_message(content="No results available.", view=None)
|
||||
|
Loading…
x
Reference in New Issue
Block a user