Answer the question
In order to leave comments, you need to log in
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")
Answer the question
In order to leave comments, you need to log in
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="в чем проблема?")
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="в чем проблема?")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question