Welcome to discord.py-message-components’ documentation!¶


The Original discord.py Library made by Rapptz with implementation of the Discord-Message-Components by mccoderpy
Visit on PyPI here
You need help? Or have ideas/feedback?¶
Open a Issue/Pull request on GitHub, join the support-Server or send me a direct-message on Discord: mccuber04#2960
Installing:¶
Python 3.5.3 or higher is required
This Library overwrite the original discord.py Library so to be sure all will work fine first uninstall the original discord.py Library if it is installed:
# Linux/macOS
python3 -m pip uninstall discord.py
# Windows
py -3 -m pip uninstall discord.py
Then install this Library using:
# Linux/macOS
python3 -m pip install -U discord.py-message-components
# Windows
py -3 -m pip install -U discord.py-message-components
quickstart¶
Sending-buttons¶
import discord
from discord.ext import commands
from discord import Button, ButtonStyle
client = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
@client.command()
async def buttons(ctx):
await ctx.send('Hey here are some Buttons', components=[[
Button(label="Hey i\'m a red Button",
custom_id="this is an custom_id",
style=ButtonStyle.red),
Button(label="Hey i\'m a green Button",
custom_id="this is an custom_id",
style=ButtonStyle.green),
Button(label="Hey i\'m a blue Button",
custom_id="this is an custom_id",
style=ButtonStyle.blurple),
Button(label="Hey i\'m a grey Button",
custom_id="this is an custom_id",
style=ButtonStyle.grey),
Button(label="Hey i\'m a URL Button",
url="https://pypi.org/project/discord.py-message-components",
style=ButtonStyle.url)
]])
client.run('Your Bot-Token')
Interact when a button was pressed¶
import discord
from discord.ext import commands
from discord import Button, ButtonStyle
client = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
@client.command()
async def buttons(ctx):
msg_with_buttons = await ctx.send('Hey here are some Buttons', components=[[
Button(label="Hey i\'m a red Button",
custom_id="red",
style=ButtonStyle.red),
Button(label="Hey i\'m a green Button",
custom_id="green",
style=ButtonStyle.green),
Button(label="Hey i\'m a blue Button",
custom_id="blue",
style=ButtonStyle.blurple),
Button(label="Hey i\'m a grey Button",
custom_id="grey",
style=ButtonStyle.grey)
]])
def check_button(i: discord.Interaction, button):
return i.author == ctx.author and i.message == msg_with_buttons
interaction, button = await client.wait_for('button_click', check=check_button)
embed = discord.Embed(title='You pressed an Button',
description=f'You pressed a {button.custom_id} button.',
color=discord.Color.random())
await interaction.respond(embed=embed)
client.run('Your Bot-Token')
Note
You could set the parameter hidden
in the response to True
to make the message ephemeral.
See discord.Interaction.respond for more information about respond()
.
coro¶
A coroutine is a function that must be invoked with await
or yield from
.
When Python encounters an await
it stops the function’s execution at that point and works on other things until it comes back to that point and finishes off its work.
This allows for your program to be doing multiple things at the same time without using threads or complicated multiprocessing.
If you forget to await a coroutine then the coroutine will not run. Never forget to await a coroutine.