Home > Net >  Discord.py - Interactions - Buttons
Discord.py - Interactions - Buttons

Time:06-29

I've been trying to create Buttons for my Discord bot but it seems like I have something wrong, I just can't figure out what it is.

Here's the code I currently have:

@bot.command()
async def test(ctx: interactions.CommandContext):
    button = Button(
        style=ButtonStyle.PRIMARY,
        custom_id="primary",
        label="Blue button",
    )
    await ctx.send("Hello World!", components=button)

I also have the following imports:

import discord
from discord.ext import commands
import random
import time
import interactions
from interactions import Button, ButtonStyle, SelectMenu, SelectOption, ActionRow

What I expect: The bot replying to command !test with the message "Hello World!" and a button with the text 'Blue button' attached to that message.

What it does: Absolutely nothing.

Now I have been staring at this for quite a while, so fair chance I've missed something rather simple, but I'd appreciate the help either way :)

CodePudding user response:

Instal the discord_components Module . pip install discord_components

Then this code should work for you :

from discord_components import *

bot = ComponentsBot(command_prefix="?")

@bot.command()
async def test(ctx):
    button = Button(
        style=ButtonStyle.blue,
        custom_id="primary",
        label="Blue button",
    )
    await ctx.send("Hello World!", components=[button])
  • Related