discord.Interactions don't work only in my bot [solved]

117 Views Asked by At

I was trying to create a button for my discord bot and I had an "interaction error". after I searched the entire Internet in search of a solution, I noticed that my bot basically does not work discord.Interactions what should I do? could this be related to the transition from discord.py on pycord?

for example, my button code:

import discord
from discord.ext import commands
from discord.ui import View, Button
intents = discord.Intents.all()
intents.message_content = True

bot = commands.Bot(command_prefix='k.', intents=intents)
@bot.command()
async def button(ctx):
    button = Button(label='button', style=discord.ButtonStyle.blurple, emoji='')
    async def but_callback(interaction):
        await interaction.response.send_message("HI!")
    button.callback = but_callback
    view = View()
    view.add_item(button)
    await ctx.send('click me!', view=view)

bot.run(token)
2

There are 2 best solutions below

1
On BEST ANSWER

the problem turned out to be very trivial. after receiving the developer's badge, I did not delete the link to the special site. after the removal, everything worked =|

7
On

Pycord is different from discord.py in a lot of thing. I'll provide you a button example that is 100% working in discord.py. If it will not work for you, I recommand reading the pycord documentation on buttons and interactions.

The code:

class MyView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(label="Test",style=discord.ButtonStyle.green, custom_id="my_custom_button")
    async def testbutton(self,interaction:discord.Interaction,button:discord.ui.Button):
        await interaction.response.send_message("Testing...")

@bot.tree.command(name='testbutton')
async def testbutton(interaction:discord.Interaction):
    await interaction.response.send_message(view=MyView())

Also if you want it to work after a bot restart paste this into your on_ready() event or a command: bot.add_view(MyView())