A "fork" of discord.py library made by Rapptz with implemation of the Discord-Message-Components by mccoderpy
- NOTE:
- This library will be further developed independently of discord.py. New features are also implemented. It's not an extension! The name only comes from the fact that the original purpose of the library was to add support for message components and we haven't found a better one yet.
.. centered:: **Visit on** |PyPI| **PyPI** `here <https://pypi.org/project/discord.py-message-components>`_
Open a issue/pull request, join the support-server or send me a direct-message on Discord: mccuber04#2960
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
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')
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 :attr:`hidden` in the response to True
to make the message ephemeral.
See discord.Interaction.respond for more information about :meth:`respond()`.
import discord
from discord.ext import commands
from discord import Button, SelectMenu, SelectOption
client = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
@client.command()
async def select(ctx):
msg_with_selects = await ctx.send('Hey here is an nice Select-Menu', components=[
[
SelectMenu(custom_id='_select_it', options=[
SelectOption(emoji='1️⃣', label='Option Nr° 1', value='1', description='The first option'),
SelectOption(emoji='2️⃣', label='Option Nr° 2', value='2', description='The second option'),
SelectOption(emoji='3️⃣', label='Option Nr° 3', value='3', description='The third option'),
SelectOption(emoji='4️⃣', label='Option Nr° 4', value='4', description='The fourth option')],
placeholder='Select some Options', max_values=3)
]])
def check_selection(i: discord.Interaction, select_menu):
return i.author == ctx.author and i.message == msg_with_selects
interaction, select_menu = await client.wait_for('selection_select', check=check_selection)
embed = discord.Embed(title='You have chosen:',
description=f"You have chosen "+'\n'.join([f'\nOption Nr° {o}' for o in select_menu.values]),
color=discord.Color.random())
await interaction.respond(embed=embed)
client.run('Your Bot-Token')
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.
.. toctree:: :maxdepth: 3 :caption: Contents: additions.rst components.rst interaction.rst