discord-message-components
This is a discord.py message component extension made by 404kuso and RedstoneZockt, which you can use until discord.py v2.0 is out
We also added override support for the discord.ext.commands.Bot
client, so you don't have to initialize everything on your own
To override the standart Bot client, add the following to your code
from discord.ext import commands
from discord_message_components import override_client
override_client()
client = commands.Bot(...)
This will add .components
and .slash
to the client, and you don't need the
extension = Extension(client)
thing anymore
And the standart TextChannel.send
function is automatically overridden
We got some features for you like send buttons, send select menus, receive a press or selection and edit messages with buttons and selection menus and everything is compatible with discord.py
Installation
# windows
py -m install discord-message-components
# linux
python3 -m pip install discord-message-components
Docs
You can read the docs here
The docs can include some typos or issues, if so, plz let me know
License
This project is under MIT License
Note
If you want to use slash commands, in the oauth2 invite link generation,
you have to check both bot
and application.commands
fields
Example
Here is a small example, that will respond to the pressed button with the content of it or will respond with the content and value of the selected element in an select menu
import discord
from discord.ext import commands
from discord_message_components import *
client = commands.Bot(" ")
extension = Extension(client)
@extension.slash.subcommand_group(base_names=["generate", "link"], name="button", description="sends a button and a linkbutton", options=[
SlashOption(str, "message content", "the content of the message"),
SlashOption(str, "name", "the name of the button"),
SlashOption(str, "link", "the link for the button"),
SlashOption(str, "emoji", "a emoji appearing before the text")
], guild_ids=["785567635802816595"])
async def command(ctx, message_content="cool, right?", name="click me", link="https://github.com/KusoRedsto/discord-message-components", emoji=None):
if not link.startswith("http://") and not link.startswith("https://"):
return await ctx.respond("The link has to start with `http://` or `https://`", hidden=True)
await ctx.respond(content=message_content, components=[LinkButton(link, label=name, emoji=emoji)])
@client.listen("on_ready")
async def on_ready():
print("ready")
@client.listen("on_message"):
async def on_message(message: Message):
if message.content = "!test":
await message.channel.send("hello", components=[
[Button("custom", "hello"), Button("custom_2", "world", "green")]
Button("custom", "yeahhh", "red")
])
@client.listen('on_button_press')
async def on_button(btn: PressedButton, msg: ResponseMessage):
await msg.respond(btn.member.mention + ", you pressed on " + btn.content + " with the custom id of " + btn.custom_id)
@client.listen('on_menu_select')
async def on_select(menu: SelectedMenu, msg: ResponseMessage):
await msg.respond(menu.member.mention + ", you selected " + ', '.join([x.content for x in menu.values]) + " on the menu with the custom id " + menu.custom_id)
client.run(token)