Бот не реагирует на кнопки в дискорде python
Вот код
async def test(ctx):
await ctx.send(
embed=discord.Embed(title='Помощник прибыл!',
description='Выбери что тебе нужно',
colour=discord.Color(0x0ffff)),
components=[
#первая линия
[Button(style=ButtonStyle.red, label='Модерация'),
Button(style=ButtonStyle.green, label='Развлечения')],
#вторая линия
[Button(style=ButtonStyle.URL, label='Наш дискорд сервер!', url="https://discord.gg/buKtm6xYSR"),
Button(style=ButtonStyle.grey, label='Наш сайт')]
]
)
response = await bot.await_for("button_click")
if response.channel == ctx.channel:
if response.component.label == "Наш сайт":
await response.respond(content= "Наш сайт")
else:
await response.respond(
embed=discord.Embed(title="Сайт в разработке"),
components=[
Button(style=ButtonStyle.URL, label="Дискорд сервер", url="https://discord.gg/buKtm6xYSR")
]
)
Ответы (1 шт):
Автор решения: Ilya Gorbunov
→ Ссылка
Данный способ создания устарел. Лучше для кнопок использовать библиотеку pycord.
Пример создания кнопок:
import discord
bot = discord.Bot() # Create a bot object
# Create a class called MyView that subclasses discord.ui.View
class MyView(discord.ui.View):
# Create a button with the label "? Click me!" with color Blurple
@discord.ui.button(label="Click me!", style=discord.ButtonStyle.primary, emoji="?")
async def button_callback(self, button, interaction):
# Send a message when the button is clicked
await interaction.response.send_message("You clicked the button!")
@bot.slash_command() # Create a slash command
async def button(ctx):
# Send a message with our View class that contains the button
await ctx.respond("This is a button!", view=MyView())
bot.run("TOKEN") # Run the bot