Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python code button with discord.py

#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")
Comment

discord.py create button

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"
Comment

PREVIOUS NEXT
Code Example
Python :: xlrd documentation 
Python :: django raw without sql injection 
Python :: turn string into operator python 
Python :: variable globale python 
Python :: remove punctuation from a string 
Python :: python search a string in another string get last result 
Python :: adding numbers in python 
Python :: pivot index 
Python :: init array in numpy 
Python :: pyqt math 
Python :: defaultdict item count 
Python :: expand pandas dataframe into separate rows 
Python :: python pip 
Python :: get n largest values from 2D numpy array matrix 
Python :: qr code scanner using opencv 
Python :: zip() python 
Python :: list slice in python 
Python :: Python NumPy tile Function Example 
Python :: server in python 
Python :: Python DateTime Class Syntax 
Python :: push button raspberry pi 
Python :: ++ in python 
Python :: remove whitespace method 
Python :: docker remote 
Python :: python tuple example 
Python :: how to do more than or less than as a condition in pythonb 
Python :: numpy add 
Python :: remove all occurences 
Python :: how to separate numeric and categorical variables in python 
Python :: iterating string in python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =