Skip to main content

discord.py Libary with implementation of the Discord-Message-Components

Project description

Discord Server Invite PyPI version info PyPI supported Python versions Total downloads for the project

The Original discord.py Libary made by Rapptz with implementation of the Discord-Message-Components by mccoderpy

(discord-User mccuber04#2960)

Questions, Bugs or Ideas

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

first uninstall the original discord.py Libary:

# Linux/macOS
python3 -m pip uninstall discord.py

# Windows
py -3 -m pip uninstall discord.py

then install this Libary using:

# Linux/macOS
python3 -m pip install -U discord.py-message-components

# Windows
py -3 -m pip install -U discord.py-message-components

Examples

A Command that sends you a Message and edit it when you click a Button:

import typing
import discord
from discord.ext import commands
from discord import ActionRow, Button, ButtonColor

client = commands.Bot(command_prefix=commands.when_mentioned_or('.!'), intents=discord.Intents.all(), case_insensitive=True)


# A Command that sends you a Message and edit it when you click a Button

@client.command(name='buttons', description='sends you some nice Buttons')
async def buttons(ctx: commands.Context):
    components = [ActionRow(Button(label='Option Nr.1',
                                   custom_id='option1',
                                   emoji="🆒",
                                   style=ButtonColor.green
                                   ),
                            Button(label='Option Nr.2',
                                   custom_id='option2',
                                   emoji="🆗",
                                   style=ButtonColor.blurple)),
                  ActionRow(Button(label='A Other Row',
                                   custom_id='sec_row_1st option',
                                   style=ButtonColor.red,
                                   emoji='😀'),
                            Button(url='https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                                   label="This is an Link",
                                   emoji='🎬'))
                  ]
    an_embed = discord.Embed(title='Here are some Button\'s', description='Choose an option', color=discord.Color.random())
    msg = await ctx.send(embed=an_embed, components=components)

    def _check(i: discord.RawInteractionCreateEvent):
        return i.message == msg and i.member == ctx.author

    interaction: discord.RawInteractionCreateEvent = await client.wait_for('interaction_create', check=_check)
    button_id = interaction.button.custom_id

    # This sends the Discord-API that the interaction has been received and is being "processed"
    await interaction.defer()  # if this is not used and you also do not edit the message within 3 seconds as described below, Discord will indicate that the interaction has failed.

    # If you use interaction.edit instead of interaction.message.edit, you do not have to deffer the interaction if your response does not last longer than 3 seconds.
    await interaction.edit(embed=an_embed.add_field(name='Choose', value=f'Your Choose was `{button_id}`'),
                           components=[components[0].disable_all_buttons(), components[1].disable_all_buttons()])

    # The Discord API doesn't send an event when you press a link button so we can't "receive" that.


client.run('You Bot-Token here')

Another (complex) Example where a small Embed will be send; you can move a small white ⬜ with the Buttons:

@client.event
async def on_raw_interaction_create(interaction: discord.RawInteractionCreateEvent):
    await interaction.defer()
    pointer: Pointer = get_pointer(interaction.guild)
    if not (message := interaction.message):
        message: discord.Message = await interaction.channel.fetch_message(interaction.message_id)
    if interaction.button.custom_id == "up":
        pointer.set_y(1)
        await message.edit(embed=discord.Embed(title="Little Game",
                                               description=display(x=pointer.possition_x, y=pointer.possition_y)),
                           components=[discord.ActionRow(empty_button, arrow_button().set_label('↑').set_custom_id('up').disable_if(pointer.possition_y >= 9), empty_button),
                                       discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x <= 0),
                                                         arrow_button().set_label('↓').set_custom_id('down'),
                                                         arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x >= 9))]
                           )
    elif interaction.button.custom_id == "down":
        pointer.set_y(-1)
        await message.edit(embed=discord.Embed(title="Little Game",
                                               description=display(x=pointer.possition_x, y=pointer.possition_y)),
                           components=[discord.ActionRow(empty_button, arrow_button().set_label('↑').set_custom_id('up'), empty_button),
                                       discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x <= 0),
                                                         arrow_button().set_label('↓').set_custom_id('down').disable_if(pointer.possition_y <= 0),
                                                         arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x >= 9))]
                           )
    elif interaction.button.custom_id == "right":
        pointer.set_x(1)
        await message.edit(embed=discord.Embed(title="Little Game",
                                               description=display(x=pointer.possition_x, y=pointer.possition_y)),
                           components=[discord.ActionRow(empty_button, arrow_button().set_label('↑').set_custom_id('up'), empty_button),
                                       discord.ActionRow(arrow_button().set_label('←').set_custom_id('left'),
                                                         arrow_button().set_label('↓').set_custom_id('down'),
                                                         arrow_button().set_label('→').set_custom_id('right').disable_if(pointer.possition_x >= 9))]
                           )
    elif interaction.button.custom_id == "left":
        pointer.set_x(-1)
        await message.edit(embed=discord.Embed(title="Little Game",
                                               description=display(x=pointer.possition_x, y=pointer.possition_y)),
                           components=[discord.ActionRow(empty_button, arrow_button().set_label('↑').set_custom_id('up'), empty_button),
                                       discord.ActionRow(arrow_button().set_label('←').set_custom_id('left').disable_if(pointer.possition_x <= 0),
                                                         arrow_button().set_label('↓').set_custom_id('down'),
                                                         arrow_button().set_label('→').set_custom_id('right'))]
                           )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord.py-message-components-1.7.4.1.tar.gz (744.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file discord.py-message-components-1.7.4.1.tar.gz.

File metadata

  • Download URL: discord.py-message-components-1.7.4.1.tar.gz
  • Upload date:
  • Size: 744.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for discord.py-message-components-1.7.4.1.tar.gz
Algorithm Hash digest
SHA256 cf08ab7fb8ea95574f827dd2823d460b4aa7c30801d26f4077f23370ac50094a
MD5 c8d3f27f35ed6c49c177a04e4acc5ada
BLAKE2b-256 700df0c8b418b71164b622f0d6b312e350d3612a61fe193342eb4a1d5305ea32

See more details on using hashes here.

File details

Details for the file discord.py_message_components-1.7.4.1-py3-none-any.whl.

File metadata

  • Download URL: discord.py_message_components-1.7.4.1-py3-none-any.whl
  • Upload date:
  • Size: 795.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for discord.py_message_components-1.7.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3da42e66f7458ad13d4feb3a5b05b702d7052b1529bd511fa4832eb0f591c937
MD5 b21f50f002785a5f8a32e8296d2e0a71
BLAKE2b-256 e31566454a7afd1eecd8500a684d9ae03f4ed771b6554fc5c5bb2b92b35ead15

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page