Add support for dynamic items that parse custom_id for state

This commit is contained in:
Rapptz
2023-07-20 22:49:38 -04:00
parent 78ce5f2331
commit a852f90358
8 changed files with 393 additions and 4 deletions

View File

@ -2,6 +2,7 @@
from discord.ext import commands
import discord
import re
# Define a simple View that persists between bot restarts
@ -29,6 +30,38 @@ class PersistentView(discord.ui.View):
await interaction.response.send_message('This is grey.', ephemeral=True)
# More complicated cases might require parsing state out from the custom_id instead.
# For this use case, the library provides a `DynamicItem` to make this easier.
# The same constraints as above apply to this too.
# For this example, the `template` class parameter is used to give the library a regular
# expression to parse the custom_id with.
# These custom IDs will be in the form of e.g. `button:user:80088516616269824`.
class DynamicButton(discord.ui.DynamicItem[discord.ui.Button], template=r'button:user:(?P<id>[0-9]+)'):
def __init__(self, user_id: int) -> None:
super().__init__(
discord.ui.Button(
label='Do Thing',
style=discord.ButtonStyle.blurple,
custom_id=f'button:user:{user_id}',
emoji='\N{THUMBS UP SIGN}',
)
)
self.user_id: int = user_id
# This is called when the button is clicked and the custom_id matches the template.
@classmethod
async def from_custom_id(cls, interaction: discord.Interaction, match: re.Match[str], /):
user_id = int(match['id'])
return cls(user_id)
async def interaction_check(self, interaction: discord.Interaction) -> bool:
# Only allow the user who created the button to interact with it.
return interaction.user.id == self.user_id
async def callback(self, interaction: discord.Interaction) -> None:
await interaction.response.send_message('This is your very own button!', ephemeral=True)
class PersistentViewBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
@ -43,6 +76,8 @@ class PersistentViewBot(commands.Bot):
# If you have the message_id you can also pass it as a keyword argument, but for this example
# we don't have one.
self.add_view(PersistentView())
# For dynamic items, we must register the classes instead of the views.
self.add_dynamic_items(DynamicButton)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
@ -63,4 +98,13 @@ async def prepare(ctx: commands.Context):
await ctx.send("What's your favourite colour?", view=PersistentView())
@bot.command()
async def dynamic_button(ctx: commands.Context):
"""Starts a dynamic button."""
view = discord.ui.View(timeout=None)
view.add_item(DynamicButton(ctx.author.id))
await ctx.send('Here is your very own button!', view=view)
bot.run('token')