Have a look at an example to quickly get started!
To create buttons, you first need to import the modules and create an instance of ButtonsClient
from discord.ext import commands
from discord_buttons_plugin import *
bot = commands.Bot(command_prefix = "!")
buttons = ButtonsClient(bot)
Then, you can create buttons using
await buttons.send(
content = "This is an example message!",
channel = ctx.channel.id,
components = [
ActionRow([
Button(
label="Hello",
style=ButtonType().Primary,
custom_id="button_one"
)
])
]
)
Breaking it down:
# Create an action row, it takes in one argument,
# which is the list of buttons that will go inside
# that action row.
# There can be more than one action rows.
ActionRow([
...
])
# Create a Button component inside the action row
# And here we have our component
ActionRow([
Button(
label = "Button label",
style = ButtonType().Primary,
custom_id = "my_button"
)
])
# You can also make a link button
# You must not add a custom_id in link button
ActionRow([
Button(
...
),
Button(
label = "Button label",
style = ButtonType().Link,
url = "https://github.com/SilentJungle399/discord_buttons_plugin"
)
])
# To use different styles, the "style" argument must be changed
Button(
style = ButtonType().Primary
)
## Other styles are:
# ButtonType().Primary
# ButtonType().Success
# ButtonType().Secondary
# ButtonType().Danger
# ButtonType().Link
# For emoji, You must provide a dictionary with these 3 key-values pairs
Button(
emoji = {
"id": None,
"name": "🙃",
"animated": False
},
)
To listen to button clicks, you can use @buttons.click
decorator
# The function name here must be the custom_id you set
# while sending the button component
@buttons.click
async def button_one(ctx):
await ctx.reply("Hello!")
The ctx
parameter is an instance of InteractionContext
.id
int : The interaction ID..token
str : The interaction token..message
discord.Message : The message where buttons are present.channel
discord.TextChannel : The channel where buttons are present..guild
discord.Guild : The guild where buttons are present..member
discord.User : The user who clicked the button. Remember this isdiscord.User
and notdiscord.Member
await .reply(content=None, *, channel=None, tts=False, embed=None, flags=None)
: Used while replying to the button click. The parameters are:content
str : The message you want to reply withchannel
int : Theid
of the channel where this message must be senttts
bool : Whether this should be text-to-speechembed
discord.Embed : The rich embed for the content.flags
MessageFlags : This can either beNone
to send normal messages, orMessageFlags().EPHEMERAL
to send ephemeral messages.
- Normal
- Ephemeral