Add button components to a message (discord.py)
Question:
I was wondering after seeing this (message components) on discord’s API reference if there was any way to implement it using python ?
I tried making a json array and passing it in my message but couldn’t make it work.
I also tried looking on the reference for python but couldn’t find anything.
Here’s my code
components= [
{
"type": 2,
"label": "Clear les kick",
"style": 4,
"custom_id": "clear_kick_button"
}
]
@slash.slash(name="kicked", description="Voir qui a été kick et combien de fois.", guild_ids=guild_id)
async def kicked(ctx):
await ctx.send("test", components= components)
If you have any informations thank you if you share it.
Answers:
Buttons are not yet implemented in the Discord.py Wrapper by Rapptz as of the currently newest version v1.7.2.
You can however look up here what is scheduled for the next update, and what the status of development is.
In the meantime you will have to either make own requests to the Discord API, or search for unofficial libraries.
Here is an example of the Alpha version from discord.py
as it is not implemented yet:
import discord
class Counter(discord.ui.View):
@discord.ui.button(label='0', style=discord.ButtonStyle.red)
async def counter(self, button: discord.ui.Button, interaction: discord.Interaction):
number = int(button.label)
button.label = str(number + 1)
if number + 1 >= 5:
button.style = discord.ButtonStyle.green
await interaction.message.edit(view=self)
view = Counter()
await ctx.send('Press to increment', view=view)
-
Another example can be seen here: Tic-Tac-Toe
-
To check the status I would take a look at the repository itself.
Here’s a little section of a code I wrote, I’m also new to this Discord components but hopefully this helped you with your issue.
await ctx.channel.send("Context",components=[Button(style=ButtonStyle.blue, label="Test")]) #Blue button with button label of "Test"
res = await self.client.wait_for("button_click") #Wait for button to be clicked
await res.respond(type=InteractionType.ChannelMessageWithSource, content=f'Button Clicked') #Responds to the button click by printing out a message only user can see #In our case, its "Button Clicked"
Also to remember to import the components first.
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
I’m assuming by your decorators you’re using discord-py-slash-command
If that assumption is correct, you can’t natively use components with that library. discord-py-components
adds that functionality while we wait for discord.py 2.0
to be released
New Answer
Discord.py 2.0 Allows for use of Buttons and Dropdowns, and has new added support for Slash Commands. A 3rd party repository is no longer necessary. However, if you don’t want to use the default one for some reason, you can checkout discord_slash
.
To upgrade to Discord.py 2.0:
Windows:
pip install -U git+https://github.com/Rapptz/discord.py
MacOS and Linux:
pip3 install -U git+https://github.com/Rapptz/discord.py
Old Answer:
(This Answer is OUTDATED.)
As of right now, you can get a library called discord_components
to use buttons.
To install this library, use pip install --upgrade discord-components
(Sometimes the command would be pip3 install --upgrade discord-components
).
To import Discord Component Buttons, use
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
Then just add this code in the bot’s on_ready()
:
DiscordComponents(bot, change_discord_methods=True)
(Make sure to replace bot
with the name of your bot, the same one you use for @something.command()
)
To add a button to a message, do this:
await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])
(A message is required)
To do something when the button is clicked, you can do something like this:
@bot.event
async def on_button_click(interaction):
if interaction.component.label.startswith("Default Button"):
await interaction.respond(type=InteractionType.ChannelMessageWithSource, content='Button Clicked')
This method even survives reboots!
Here’s an example I put together for you if you need it:
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
bot = commands.Bot(command_prefix=prefix, description="Desc", help_command=None)
@bot.event
async def on_ready():
DiscordComponents(bot, change_discord_methods=True)
await bot.change_presence(activity=discord.Game(name=f"{prefix}help"))
print("Bot has successfully logged in as: {}".format(bot.user))
print("Bot ID: {}n".format(bot.user.id))
@bot.command()
async def button(ctx):
await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])
bot.run("token")
Hope this Helps!
Tip: If you want the buttons to be in one row, use [[]] instead of just [] for Example: [[btn1, btn2],[btn3, btn4]] will result in:
[btn 1][btn 2]
[btn 3][btn 4]
Extra Tip: You can also set a variable as a button then send the variable
I would like to say that this page is 3 months old, discord_components has changed a lot, Interaction Type does not exist anymore, please follow this
https://gitlab.com/discord.py-components/discord.py-components/-/tree/master/examples
There is a Library named discord.py-message-components.
You can install that trough
python3 -m pip install -U git+https://github.com/mccoderpy/discord.py-message-components.git@developer
You can do there anything.
SlashCommands, SelectMenus, Button, Modals and much more.
You need to uninstall discord.py and install this library.
The Docs are here: https://docs.discord4py.dev/.
It will named discord4py soon.
To create a Button:
import discord
from discord import SlashCommand
from discord import ButtonStyle, Button
@bot.slash_command(name="kicked", description="Voir qui a été kick et combien de fois.", guild_ids=guild_id)
async def kicked(ctx: discord.ApplicationCommandInteraction):
components = [ActionRow(Button(
label="Clear les kick",
custom_id="clear_kick_button",
style=ButtonStyle.gray
))]
await ctx.send("test", components=components)
I was wondering after seeing this (message components) on discord’s API reference if there was any way to implement it using python ?
I tried making a json array and passing it in my message but couldn’t make it work.
I also tried looking on the reference for python but couldn’t find anything.
Here’s my code
components= [
{
"type": 2,
"label": "Clear les kick",
"style": 4,
"custom_id": "clear_kick_button"
}
]
@slash.slash(name="kicked", description="Voir qui a été kick et combien de fois.", guild_ids=guild_id)
async def kicked(ctx):
await ctx.send("test", components= components)
If you have any informations thank you if you share it.
Buttons are not yet implemented in the Discord.py Wrapper by Rapptz as of the currently newest version v1.7.2.
You can however look up here what is scheduled for the next update, and what the status of development is.
In the meantime you will have to either make own requests to the Discord API, or search for unofficial libraries.
Here is an example of the Alpha version from discord.py
as it is not implemented yet:
import discord
class Counter(discord.ui.View):
@discord.ui.button(label='0', style=discord.ButtonStyle.red)
async def counter(self, button: discord.ui.Button, interaction: discord.Interaction):
number = int(button.label)
button.label = str(number + 1)
if number + 1 >= 5:
button.style = discord.ButtonStyle.green
await interaction.message.edit(view=self)
view = Counter()
await ctx.send('Press to increment', view=view)
-
Another example can be seen here: Tic-Tac-Toe
-
To check the status I would take a look at the repository itself.
Here’s a little section of a code I wrote, I’m also new to this Discord components but hopefully this helped you with your issue.
await ctx.channel.send("Context",components=[Button(style=ButtonStyle.blue, label="Test")]) #Blue button with button label of "Test"
res = await self.client.wait_for("button_click") #Wait for button to be clicked
await res.respond(type=InteractionType.ChannelMessageWithSource, content=f'Button Clicked') #Responds to the button click by printing out a message only user can see #In our case, its "Button Clicked"
Also to remember to import the components first.
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
I’m assuming by your decorators you’re using discord-py-slash-command
If that assumption is correct, you can’t natively use components with that library. discord-py-components
adds that functionality while we wait for discord.py 2.0
to be released
New Answer
Discord.py 2.0 Allows for use of Buttons and Dropdowns, and has new added support for Slash Commands. A 3rd party repository is no longer necessary. However, if you don’t want to use the default one for some reason, you can checkout discord_slash
.
To upgrade to Discord.py 2.0:
Windows:
pip install -U git+https://github.com/Rapptz/discord.py
MacOS and Linux:
pip3 install -U git+https://github.com/Rapptz/discord.py
Old Answer:
(This Answer is OUTDATED.)
As of right now, you can get a library called discord_components
to use buttons.
To install this library, use pip install --upgrade discord-components
(Sometimes the command would be pip3 install --upgrade discord-components
).
To import Discord Component Buttons, use
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
Then just add this code in the bot’s on_ready()
:
DiscordComponents(bot, change_discord_methods=True)
(Make sure to replace bot
with the name of your bot, the same one you use for @something.command()
)
To add a button to a message, do this:
await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])
(A message is required)
To do something when the button is clicked, you can do something like this:
@bot.event
async def on_button_click(interaction):
if interaction.component.label.startswith("Default Button"):
await interaction.respond(type=InteractionType.ChannelMessageWithSource, content='Button Clicked')
This method even survives reboots!
Here’s an example I put together for you if you need it:
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
bot = commands.Bot(command_prefix=prefix, description="Desc", help_command=None)
@bot.event
async def on_ready():
DiscordComponents(bot, change_discord_methods=True)
await bot.change_presence(activity=discord.Game(name=f"{prefix}help"))
print("Bot has successfully logged in as: {}".format(bot.user))
print("Bot ID: {}n".format(bot.user.id))
@bot.command()
async def button(ctx):
await ctx.send(type=InteractionType.ChannelMessageWithSource, content="Message Here", components=[Button(style=ButtonStyle.URL, label="Example Invite Button", url="https://google.com"), Button(style=ButtonStyle.blue, label="Default Button", custom_id="button")])
bot.run("token")
Hope this Helps!
Tip: If you want the buttons to be in one row, use [[]] instead of just [] for Example: [[btn1, btn2],[btn3, btn4]] will result in:
[btn 1][btn 2]
[btn 3][btn 4]
Extra Tip: You can also set a variable as a button then send the variable
I would like to say that this page is 3 months old, discord_components has changed a lot, Interaction Type does not exist anymore, please follow this
https://gitlab.com/discord.py-components/discord.py-components/-/tree/master/examples
There is a Library named discord.py-message-components.
You can install that trough python3 -m pip install -U git+https://github.com/mccoderpy/discord.py-message-components.git@developer
You can do there anything.
SlashCommands, SelectMenus, Button, Modals and much more.
You need to uninstall discord.py and install this library.
The Docs are here: https://docs.discord4py.dev/.
It will named discord4py soon.
To create a Button:
import discord
from discord import SlashCommand
from discord import ButtonStyle, Button
@bot.slash_command(name="kicked", description="Voir qui a été kick et combien de fois.", guild_ids=guild_id)
async def kicked(ctx: discord.ApplicationCommandInteraction):
components = [ActionRow(Button(
label="Clear les kick",
custom_id="clear_kick_button",
style=ButtonStyle.gray
))]
await ctx.send("test", components=components)