A discord button handler for discord.py
Project description
discord.py button docs
This is a discord.py button extension made by 404kuso and RedstoneZockt
- Initializing
- Client Events
- Sending Butttons
- Receiving a button press
- Complete example
- Code docs
- Future added
Get started
At first, you need a discord.ext.commands.Bot client
import discord
from disicord.ext import commands
client = commands.Bot()
To initialize the Button extension, you need to import the Buttons class
from discord_py_buttons import Buttons
client.buttons = Buttons(client)
This will add a listener to button presses and will grant you acces to the button tools
Events
These events can be received trought client.listen('eventName')
on_button_press
This event will be dispatched if a user pressed a button (a new interaction was created)
The parameters passed to your function will be
-
PressedButtonThe Button which was pressed
-
MessageThe message on which the button was pressed
Your function should look something like this
@client.listen('on_button_press')
async def on_button(btn: PressedButton, message: ResponseMessage)
# code goes here
Sending
To send a button message, you need the send function from a Buttons instance
Example:
from discord_py_buttons import Button
@client.listen('on_message')
async def on_message(message: discord.Message):
if message.content == "!btn":
client.buttons.send(message.channel, "here you go", buttons=[Button("myID", "Press me", emoji="😀")])
Receiving
If you want to receive a button press, you need to listen to the on_button_press event with client.listen('on_button_press')
There will be two parameters which will be passed to your function
-
PressedButtonThe button which was pressed
-
ResponseMessageThe message on which the button was pressed
To respond to the interaction, you should use msg.respond()
Example:
@client.listen('on_button_press')
async def on_button(btn: PressedButton, msg: ResponseMsg):
btn.respond("You pressed " + btn.content)
Example
Here is an example code, which sends a button everytime a user sends a message with the content !btn and replies to every button press with the content of the button
import discord
from discord.ext import commands
from discord_py_buttons import Buttons, Button, PressedButton, ResponseMessage
client.buttons = Buttons(client)
@client.listen('on_message')
async def on_message(message: discord.Message):
if message.content == "!btn":
client.buttons.send("Here ya go!", buttons=[Button("custom_id", "PRESS ME")])
@client.listen('on_button_press')
async def on_button(btn: PressedButton, msg: ResponseMessage):
msg.respond("You pressed on " + btn.content + " with the customID " + btn.custom_id)
client.run("Your secret token")
CodeDocs
Buttons: class
A button instance for using buttons
Initialization
Buttons(client: discord.ext.commands.client)
- client:
discord.ext.commands.clientThe bot client
Methods
-
send
async def send(self, channel, content = None, *, tts = False, embed = None, embeds = None, file = None, files = None, delete_after = None, nonce = None, allowed_mentions = None, reference = None, mention_author = None, buttons = None ) -> Message:
| coroutine |
Parameters
-
channel:
discord.TextChannelThe textchannel where the message should be sent
Required
-
content:
strThe text content of the message
-
tts:
boolIf the message should be text-to-speech
-
embed:
discord.EmbedThe embed included in the message
-
embeds:
List[discord.Embed]The embeds included in the message
-
file:
discord.FileA file that will be sent as an attachment to the message
-
file:
List[discord.File]A list of files which will be sent as an attachment
-
delete_after:
floatThe numbers of seconds after which the message will be deleted in the background
-
nonce:
intThe nonce to use for sending this message
-
allowed_mentions:
discord.Allowed_mentionsMentions allowed in this message
-
reference:
discord.MessageReference or discord.MessageThe message to which the message replies
-
mention_author:
boolWhether the author should be mentioned
-
buttons:
List[Button]A list of buttons in this message
Returns
MessageThe sent message
-
Button: class
Represents a message component button
Initialization
Button(custom_id, label = None, color = None, emoji = None, new_line = False, disabled = True)
-
custom_id:
strA customID for identifying the button, max 100 characters
-
label:
strThe text that appears on the button, max 80 characters
-
color:
str or intThe color of the button, one of:
[("blurple", "primary", 1), ("gray", "secondary", 2), ("green", "succes", 3), ("red", "danger", 4)]Things in () are the same color
-
emoji:
discord.Emoji or strA emoji appearing before the label
-
new_line:
boolWhether a new line should be added before the button
-
disabled:
boolWhether the button should be clickable (disabled = False) or not (disabled=True)
Attributes
-
content:
strThe content of the button (emoji + " " + label)
-
custom_id:
strThe customID of the button
-
label:
strThe text that appears on the button
-
color:
str or intThe color of the button
-
emoji:
discord.Emoji or strThe emoji appearing before the label
-
new_line:
boolWhether a new line was added before the button
-
disabled:
boolWhether the button is disabled
Methods
-
to_dict:
function -> dictdef to_dict() -> dict:
Converts the button to a python dictionary
LinkButton: class
Represents a message component button that will open a link on click
This type of button will not trigger the on_button_press event
Initialization
```py LinkButton(url: str, label: str, emoji: discord.Emoji or str, new_line: bool, disabled: bool) ```-
url:
strThe url which will be opened when clicking the button
-
label:
strA text that appears on the button, max 80 characters
-
emoji:
discord.Emoji or strA emoji appearing before the label
-
new_line:
boolWhether a new line should be added before the button
-
disabled:
boolWhether the button should be clickable (disabled = False) or not (disabled=True)
Attributes
-
content:
strThe content of the button (emoji + " " + label)
-
url:
strThe link which will be opened when clicking the button
-
label:
strThe text that appears on the button, max 80 characters
-
color:
str or intThe color of the button
This will always be
5(linkButton) -
emoji:
discord.Emoji or strThe emoji appearing before the label
-
new_line:
boolWhether a new line was added before the button
-
disabled:
boolWhether the button is disabled
Methods
-
to_dict:
function -> dictdef to_dict() -> dict:
Converts the button to a python dictionary
Message: class
Extends the discord.Message object
Attributes
- buttons:
List[Button or LinkButton]A list of buttons included in the message
ResponseMessage: class
A class for responding to a (interaction) message
Extends the Message object
Attributes
- pressedButton:
ButtonThe button which was pressed
Methods
-
acknowledge:
functionAcknowledges that the interaction was received
def acknowledge():
This function should be used if your client needs more than 15 seconds to responod
-
respond:
Responds to the interactionfunctiondef respond(self, content=None, *, tts=False, embed = None, embeds=None, file=None, files=None, nonce=None, allowed_mentions=None, reference=None, mention_author=None, buttons=None, ninjaMode = False):
Parameters
-
content:
strThe text content of the message
-
tts:
boolIf the message should be text-to-speech
-
embed:
discord.EmbedThe embed included in the message
-
embeds:
List[discord.Embed]The embeds included in the message
-
file:
discord.FileA file which will be sent as an attachment to the message
-
files:
List[discord.File]A list of files that will be sent as attachment to the message
-
nonce:
intThe nonce to use for sending this message
-
allowed_mentions:
discord.Allowed_mentionsMentions allowed in this message
-
reference:
discord.MessageReference or discord.MessageThe message to which the message replies
-
mention_author:
boolWhether the author should be mentioned
-
buttons:
List[Button]A list of buttons in this message
-
ninjaMode:
boolWhether the client should respond silent like a ninja to the interaction
(User will see nothing)
-
Super
To-Do
- file sending complete support
- inline => new_line
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters