Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



   2714

Discord.py Buttons explanation

by Slugy - 02 June, 2021 - 06:20 PM
This post is by a banned member (Slugy) - Unhide
Slugy  
Registered
11
Posts
3
Threads
4 Years of service
#9
This is a bump
This post is by a banned member (Slugy) - Unhide
Slugy  
Registered
11
Posts
3
Threads
4 Years of service
#10
This is a bump
This post is by a banned member (Rade) - Unhide
Rade  
Infinity
175
Posts
90
Threads
4 Years of service
#11
(02 June, 2021 - 06:20 PM)Slugy Wrote: Show More
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!  pepeblush
tysm u such pro coder  pepeblush pepeblush pepeblush lol2 lol2

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)