R
R
rirclear2021-11-20 14:12:02
Python
rirclear, 2021-11-20 14:12:02

Why is the discord bot not responding when I click the button?

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RuslanUC, 2021-11-20
@rirclear

Firstly, where bot.wait_for(...), you have not "button_click", but "buttton_click", i.e. with three t.
Secondly, when you run the code, you will get an error saying that response does not have a components property. It may work in previous versions, but not in the new one. The solution is to add a custom_id to the buttons, and check it:

@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="в чем проблема?")

And it's better to add a channel check to wait_for, you can do it like this:
def response_check(inter):
  return inter.channel == ctx.channel

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

or like this:
response = await bot.wait_for("button_click", check=lambda inter: inter.channel == ctx.channel)

Then the code will look like this:
@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="в чем проблема?")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question