Код: Выделить всё
class ConfirmResetView(discord.ui.View):
def __init__(self, cog):
super().__init__(timeout=60)
self.cog = cog # Reference to the cog for resetting the event
self.answer = None # To store the user's decision
@discord.ui.button(label="Yes", style=discord.ButtonStyle.blurple)
async def yes_button(self, interaction: discord.Interaction, button: discord.ui.Button):
self.clear_items()
self.answer = 'Yes'
await interaction.response.edit_message(delete_after=.1)
self.stop()
@discord.ui.button(label="No", style=discord.ButtonStyle.red)
async def no_button(self, interaction: discord.Interaction, button: discord.ui.Button):
self.clear_items()
self.answer = 'No'
await interaction.response.edit_message(delete_after=.1)
self.stop()
Код: Выделить всё
view = ConfirmResetView(self)
message = await ctx.send("Are you sure?", view=view)
# Wait for the user to respond (button click)
await view.wait()
print(view.answer)
Код: Выделить всё
import discord
class Question_dialog(discord.ui.View):
def __init__(self, cog, timeout=60, buttons=[('Yes', 'Yes', discord.ButtonStyle.blurple), ('No', 'No', discord.ButtonStyle.red)]):
super().__init__(timeout=timeout)
self.cog = cog # Reference to the cog for resetting the event
self.answer = None # To store the user's decision
for i, (label, answer, style) in enumerate(buttons):
@discord.ui.button(label=label, style=style)
async def button(self, interaction: discord.Interaction, button: discord.ui.Button):
self.clear_items()
self.answer = answer
await interaction.response.edit_message(delete_after=.1)
self.stop()
setattr(self, f"button{i}", button)
Код: Выделить всё
import discord
class QuestionDialog(discord.ui.View):
def __init__(self, cog, timeout=60, buttons=[('Yes', 'Yes', discord.ButtonStyle.blurple), ('No', 'No', discord.ButtonStyle.red)]):
super().__init__(timeout=timeout)
self.cog = cog # Reference to the cog for resetting the event
self.answer = None # To store the user's decision
# Loop over the buttons and create them dynamically
for i, (label, answer, style) in enumerate(buttons):
self.add_item(self.create_button(label, answer, style))
def create_button(self, label, answer, style):
# This helper function returns a button that has a separate scope
async def button_callback(interaction: discord.Interaction):
self.clear_items()
self.answer = answer
await interaction.response.edit_message(delete_after=.1)
self.stop()
# Create and return a discord.ui.Button with the proper callback
return discord.ui.button(label=label, style=style, custom_id=f"button_{label}", row=0, disabled=False)(button_callback)
Код: Выделить всё
view = Question_dialog(self)
Подробнее здесь: https://stackoverflow.com/questions/789 ... mmatically