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
-
PressedButton
The Button which was pressed
-
Message
The 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
-
PressedButton
The button which was pressed
-
ResponseMessage
The 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.client
The 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.TextChannel
The textchannel where the message should be sent
Required
-
content:
str
The text content of the message
-
tts:
bool
If the message should be text-to-speech
-
embed:
discord.Embed
The embed included in the message
-
embeds:
List[discord.Embed]
The embeds included in the message
-
file:
discord.File
A 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:
float
The numbers of seconds after which the message will be deleted in the background
-
nonce:
int
The nonce to use for sending this message
-
allowed_mentions:
discord.Allowed_mentions
Mentions allowed in this message
-
reference:
discord.MessageReference or discord.Message
The message to which the message replies
-
mention_author:
bool
Whether the author should be mentioned
-
buttons:
List[Button]
A list of buttons in this message
Returns
Message
The 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:
str
A customID for identifying the button, max 100 characters
-
label:
str
The text that appears on the button, max 80 characters
-
color:
str or int
The 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 str
A emoji appearing before the label
-
new_line:
bool
Whether a new line should be added before the button
-
disabled:
bool
Whether the button should be clickable (disabled = False) or not (disabled=True)
Attributes
-
content:
str
The content of the button (emoji + " " + label)
-
custom_id:
str
The customID of the button
-
label:
str
The text that appears on the button
-
color:
str or int
The color of the button
-
emoji:
discord.Emoji or str
The emoji appearing before the label
-
new_line:
bool
Whether a new line was added before the button
-
disabled:
bool
Whether the button is disabled
Methods
-
to_dict:
function -> dict
def 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:
str
The url which will be opened when clicking the button
-
label:
str
A text that appears on the button, max 80 characters
-
emoji:
discord.Emoji or str
A emoji appearing before the label
-
new_line:
bool
Whether a new line should be added before the button
-
disabled:
bool
Whether the button should be clickable (disabled = False) or not (disabled=True)
Attributes
-
content:
str
The content of the button (emoji + " " + label)
-
url:
str
The link which will be opened when clicking the button
-
label:
str
The text that appears on the button, max 80 characters
-
color:
str or int
The color of the button
This will always be
5
(linkButton) -
emoji:
discord.Emoji or str
The emoji appearing before the label
-
new_line:
bool
Whether a new line was added before the button
-
disabled:
bool
Whether the button is disabled
Methods
-
to_dict:
function -> dict
def 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:
Button
The button which was pressed
Methods
-
acknowledge:
function
Acknowledges 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 interactionfunction
def 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:
str
The text content of the message
-
tts:
bool
If the message should be text-to-speech
-
embed:
discord.Embed
The embed included in the message
-
embeds:
List[discord.Embed]
The embeds included in the message
-
file:
discord.File
A 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:
int
The nonce to use for sending this message
-
allowed_mentions:
discord.Allowed_mentions
Mentions allowed in this message
-
reference:
discord.MessageReference or discord.Message
The message to which the message replies
-
mention_author:
bool
Whether the author should be mentioned
-
buttons:
List[Button]
A list of buttons in this message
-
ninjaMode:
bool
Whether 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
Hashes for discord_py_buttons-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dafcaad9af68af95af183aac1f541799f07f7e5146e0cbc91ddac14412644c7a |
|
MD5 | 9bff4c46a88644da881a8aed2e6730b4 |
|
BLAKE2b-256 | f0c55780625b7de372023c57d5ea3057cfdce3bf21e9862b845da969df9cd0c9 |