TDiscordcomponents Buttons
Discord.py
The Buttons in Discord.py are not added to the Docs so some 3rd party Docs have them included but not very good explained so i will try to make things easier for you
- How to Download:
pip install --upgrade discord-components or py -version -m pip install buttons
- Import: from discord_components import Button, ButtonStyle, InteractionType
- Example Code:
Code:
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"{bot.user.name} is ready.")
DiscordComponents(bot)
@bot.command()
async def test(ctx):
await ctx.send(
"Discord Buttons test",
components=[
[ # this Bracket is here, because else the Buttons would be among themselves and not side by side, so every row has like 5 or 6 places and then u would need to start a new row
Button(style=ButtonStyle.blue, label="Blue",),
Button(style=ButtonStyle.green, label="Green"),
Button(style=ButtonStyle.gray, label="Gray"),
Button(style=ButtonStyle.red, label="Red"),
Button(style=ButtonStyle.grey, label="Grey"),
],
[
Button(style=ButtonStyle.grey, label="Emoji", emoji="?"), # only standart emojis are possible
Button(style=ButtonStyle.URL, label="url", url="https://cracked.sh/Slugy"),
Button(style=ButtonStyle.red, label="Disabled", disabled=True)
]
],
)
y = await ctx.bot.wait_for("button_click")
await y.respond(type=InteractionType.ChannelMessageWithSource,
content=f'{y.component.label} clicked by {y.user}')
bot.run("") # your token
This is the simpliest way to explain it. In this code, for example i already did a Ticket System with it, that was much harder than this. The Button can be clicked just once. If u want, that the button can be clicked muliple times, put it in a loop where the message gets send over and over again or edit the message everytime with the new buttons also in a loop. You could also put the await ctx.bot.wait_for("button_click") in a complete own listener so your code looks cleaner!
If you need any Help/hace some questions feel free to DM me on Discord :
Slug#8888
I hope i was able to help you. You can also check out my Profile, soon more Discord.py things!
