button discord python
Доброго времени суток.
Наткнулся на кнопки в дискорде и решил изучить, но дается трудно, не один пример из интернета не работает(
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle
import os, sys, re
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(f"{cfg['Prefix']}"),
case_insensitive=True,
intents=discord.Intents.all())
adm = [937680246159835156, 915928364563451945]
class butt(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def button(self, ctx):
await ctx.send("Hello, World!",
components = [Button(label = "WOW button!", custom_id = "button1")]
)
interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
await interaction.send(content = "Button clicked!")
def setup(bot):
bot.add_cog(butt(bot))
Команда работает, но кнопки не нажимаются, пишет "Ошибка взаимодействия"
Ответы (1 шт):
Автор решения: denisnumb
→ Ссылка
Чтобы не намешивать библиотеки, я все же предлагаю использовать py-cord:
import discord
from discord.ext import commands
from discord.ui import Button, View
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
Тогда обработку нажатия кнопки можно будет реализовать вот так:
@bot.command()
async def test(ctx):
await ctx.send('Hello World!', view=View(
Button(custom_id='button1', label='WOW button!', style=discord.ButtonStyle.green)
))
interaction = await bot.wait_for('interaction', check=lambda i: i.custom_id == 'button1')
await interaction.response.edit_message(content='Button clicked!', view=None)
Или так, через установку отдельной функции в качестве свойства отклика кнопки:
@bot.command()
async def test(ctx):
async def button_callback(interaction):
await interaction.response.edit_message(content='Button clicked!', view=None)
button = Button(custom_id='button1', label='WOW button!', style=discord.ButtonStyle.green)
button.callback = button_callback
await ctx.send('Hello World!', view=View(button))
Всю информацию я брал из документации. Если вам нужен был пример, то тоже не пойму как вы его не нашли, потому что я нашел идеальное видео сразу по запросу discord buttons: https://www.youtube.com/watch?v=kNUuYEWGOxA

