@rirclear

Почему бот в дискорде не отвечает при нажатии на кнопку?

import discord 
from discord.ext import commands 
from discord_components import DiscordComponents, Button, ButtonStyle

bot = commands.Bot(command_prefix=".", intents=discord.Intents.all())

@bot.event 
async def on_ready():
	DiscordComponents(bot)
	print("бот подключен")

@bot.command()
async def test(ctx):
	await ctx.send(
		embed=discord.Embed(title="тебе нравится наш сервер?"),
		components=[
			Button(style=ButtonStyle.red, label="ДА!", emoji=""),
			Button(style=ButtonStyle.green, label="ну такое..", emoji=""),
		]	
	)

	response = await bot.wait_for("buttton_click")
	if response.channel == ctx.channel:
		if response.components.label == "ДА!":
			await response.respond(content="рады стараться!")
		else:
			await response.respond(content="в чем проблема?")

bot.run("token")

6198d7c4134aa017377973.png
  • Вопрос задан
  • 503 просмотра
Решения вопроса 1
@RuslanUC
Во первых, там где bot.wait_for(...), у вас прописано не "button_click", а "buttton_click", т.е. с тремя t.
Во вторых при запуске кода вам выдаст ошибку, мол у response нет свойства components. Возможно это работает в прошлых версиях, но в новой - нет. Решение - добавить кнопкам custom_id, и проверять его:
@bot.command()
async def test(ctx):
  await ctx.send(
    embed=discord.Embed(title="тебе нравится наш сервер?"),
    components=[
      Button(style=ButtonStyle.red, label="ДА!", custom_id="yes"),
      Button(style=ButtonStyle.green, label="ну такое..", custom_id="no"),
    ]	
  )

  response = await bot.wait_for("button_click")
  if response.channel == ctx.channel:
    if response.custom_id == "yes":
      await response.respond(content="рады стараться!")
    else:
      await response.respond(content="в чем проблема?")


И лучше добавить проверку канала в wait_for, сделать это можно так:
def response_check(inter):
  return inter.channel == ctx.channel

response = await bot.wait_for("button_click", check=response_check)

или так:
response = await bot.wait_for("button_click", check=lambda inter: inter.channel == ctx.channel)

Тогда код будет выглядеть так:
@bot.command()
async def test(ctx):
  await ctx.send(
    embed=discord.Embed(title="тебе нравится наш сервер?"),
    components=[
      Button(style=ButtonStyle.red, label="ДА!", custom_id="yes"),
      Button(style=ButtonStyle.green, label="ну такое..", custom_id="no"),
    ]	
  )

  response = await bot.wait_for("button_click", check=lambda inter: inter.channel == ctx.channel)
  if response.custom_id == "yes":
    await response.respond(content="рады стараться!")
  else:
    await response.respond(content="в чем проблема?")
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы