"Ошибка взаимодействия"
Мне не выдает ошибок в консоль, но я не могу взаимодействовать с кнопками в команде /new_game.
#newgame.py
from discord import commands
import discord
import random
from discord.ext.commands import Cog
class Newgame(Cog):
def __init__(self, bot):
self.bot = bot
async def game(self):
view = discord.ui.View()
door1 = discord.ui.Button(style=discord.ButtonStyle.grey, custom_id="door1", emoji="?")
door2 = discord.ui.Button(style=discord.ButtonStyle.grey, custom_id="door2", emoji="?")
door3 = discord.ui.Button(style=discord.ButtonStyle.grey, custom_id="door3", emoji="?")
button_list = [door1, door2, door3]
random.shuffle(button_list)
return button_list
async def button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
button_id = button.custom_id
await self.handle_button_click(interaction, interaction.component)
@Cog.listener()
async def on_button_click(interaction: discord.Interaction):
# Проверка, что эта кнопка была создана внутри view, привязанной к этому сообщению.
if interaction.type == discord.InteractionType.button:
await interaction.defer() # отложенный ответ, чтобы бот мог обработать запрос без задержки
# Получение объекта кнопки, на которую нажал пользователь
button = interaction.component
# Получение ID нажатой кнопки
button_id = button.custom_id
if button_id == "door1":
embed = discord.Embed(color=discord.Color.brand_red, title="Oops...", description="Not right door.")
embed.set_footer(text="You lose!", icon_url=interaction.user.avatar_url)
await interaction.respond(embed=embed)
elif button_id == "door2":
view = discord.ui.View()
buttons = await self.game()
for button in buttons:
view.add_item(button)
await interaction.respond(view=view)
elif button_id == "door3":
embed = discord.Embed(color=discord.Color.brand_red, title="Oops...", description="Not right door.")
embed.set_footer(text="You lose!", icon_url=interaction.user.avatar_url)
await interaction.respond(embed=embed)
@commands.slash_command(description="Starts a new game.")
async def new_game(self, ctx):
try:
view = discord.ui.View()
buttons = await self.game()
for button in buttons:
view.add_item(button)
await ctx.send("Starting a new game...", view=view)
# ожидание нажатия кнопки
interaction = await self.bot.wait_for(
"button_click",
check=lambda i: i.message.id == view.message.id and i.user.id == ctx.author.id,
timeout=60
)
await self.button_callback(interaction.component, interaction)
except Exception as e:
print(e)
raise e
def setup(bot):
bot.add_cog(Newgame(bot))
PS: Час сидел в чате с ChatGPT, никак не помог.