Программное создание кнопок бота DiscordPython

Программы на Python
Anonymous
Программное создание кнопок бота Discord

Сообщение Anonymous »

В моем боте Discord на Python я могу определить discord.ui.View следующим образом:

Код: Выделить всё

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)
Я хочу создать подкласс View общего назначения, который можно использовать для генерации вопросов с любым количеством кнопок. Я пробовал несколькими способами:

Код: Выделить всё

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

Вернуться в «Python»