#hi my name is Yahya and today im going to show you how to create a button with libarry discord.py
# First Install discord.py from cmd and run vsc and create anew file
# and type that commands
import discord
from discord.ext import commands
from discord.ui import Buttons,View # this command will allow to you to create and view buttons for members on discord
# run the bot
intents = disocrd.Intents().all()
intents.message_content = True
# the prefix is for run the command for example #
bot = commands.Bot(command_prefix="#", intents=intents)
# now we will start with create button:
@bot.command()
async def button(ctx):
button = Button(label="Click for lock channel", style=disocrd.ButtonStyle.red)
async def button_callback(interaction):
await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False)
await ctx.send("DOne")
# you can make 2 buttons for lock/unlock channel and if u want to send embeds or send messages from buttons just type : interaction.response.send_message(the message with "")
button.callback = button_callback
view = View()
view.add_item(button)
await ctx.send("this is for grepper", view=view)
bot.run("token")
We need the Libary: discord.py-message-components
You can download it so: py -m pip install -U git+https://github.com/mccoderpy/discord.py-message-components.git@developer
But first Uninstall discord.py
With this Libary you can make Selects, Buttons, Modals, Slash Commands or other stuff!
Docs: https://discordpy-message-components.readthedocs.io/en/latest/
And how to make a button?
With:
from discord import ActionRow, Button, ButtonStyle
@bot.command()
async def button(ctx):
components = [
ActionRow(
Button(label="Button", emoji="❤", custom_id="customid", style=ButtonStyle.green) # <= This is the Button
)
]
@bot.on_click() # <= This is the event if someone clicked on the button!
async def customid(i: discord.ComponentInteraction, button): # <= customid is the custom id of the button!
await i.respond(f'Thanks for clicking {i.author.mention}', hidden=True) # <= Its send a Ephermal Message with the Text: "Thanks for clicking User"