Ошибка взаимодействия кнопок discord.py
Я хочу сделать отслеживание нажатия кнопок в discord.py, то есть сделать так, что бы при нажатии на одну кнопку бот отправлял одно сообщение, при нажатии на другую кнопку - отправлял другое сообщение.
Я ожидал, что бот будет отправлять различные сообщения в зависимости от нажатой кнопки, а получилось так, что сообщения не отправились, и в Дискорде после нажатия на одну из кнопок под сообщением появлялась надпись "Ошибка взаимодействия". В консоли Python никаких ошибок нет.
Пробовал делать отслеживание через @bot.event(), через responce.component.id - ничего не помогло.
Через @bot.event():
from discord.ext import commands
from config import settings
import discord
import discord.ui
import keep_alive
client = discord.Client(intents=discord.Intents.default())
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=settings['prefix'], intents=intents)
bot.remove_command('help')
@bot.event
async def on_button_click(ctx):
button_id = ctx.custom_id
if button_id == '1':
await ctx.send('вы нажали на кнопку 1')
elif button_id == '2':
await ctx.send('вы нажали на кнопку 2')
@bot.command()
async def test(ctx, *arg):
button1 = discord.ui.Button(label="1", style=discord.ButtonStyle.gray, custom_id="1")
button2 = discord.ui.Button(label="2", style=discord.ButtonStyle.gray, custom_id="2")
msg = "нажмите на одну из кнопок!"
view = discord.ui.View()
view.add_item(button1)
view.add_item(button2)
await ctx.send(msg, view=view)
await bot.wait_for('button_click')
Через responce.component.id:
from discord.ext import commands
from config import settings
import discord
import discord.ui
import keep_alive
client = discord.Client(intents=discord.Intents.default())
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=settings['prefix'], intents=intents)
bot.remove_command('help')
@bot.command()
async def test(ctx, *arg):
button1 = discord.ui.Button(label="1", style=discord.ButtonStyle.gray, custom_id="1")
button2 = discord.ui.Button(label="2", style=discord.ButtonStyle.gray, custom_id="2")
msg = "нажмите на одну из кнопок!"
view = discord.ui.View()
view.add_item(button1)
view.add_item(button2)
await ctx.send(msg, view=view)
responce = await bot.wait_for('button_click')
button_id = ctx.custom_id
if responce.component.id == '1':
await ctx.send('вы нажали на кнопку 1')
elif responce.component.id == '2':
await ctx.send('вы нажали на кнопку 2')
keep_alive.keep_alive()
bot.run(settings['token'])
Подскажите, пожалуйста, как правильно сделать то, что мне нужно?
Источник: Stack Overflow на русском