Additions and adjustments to discord.py builtin-functions

class discord.Client
@on_click(custom_id=None)

A decorator with which you can assign a function to a specific Button (or its custom_id).

Important

The Function this decorator attached to must be an coroutine (means an awaitable) and take the same parameters as a on_raw_button_click

Parameters

custom_id (Optional[str]) – If the custom_id of the Button could not use as an function name or you want to give the function a different name then the custom_id use this one to set the custom_id.

Example

# the Button
Button(label='Hey im a cool blue Button',
        custom_id='cool blue Button',
        style=ButtonColor.blurple)

# function that's called when the Button pressed
@client.on_click(custom_id='cool blue Button')
async def cool_blue_button(i: discord.Interaction, button):
    await i.respond('Hey you pressed a `cool blue Button`!', hidden=True)
Raises

TypeError – The coroutine passed is not actually a coroutine.

@on_select(custom_id=None)

A decorator with which you can assign a function to a specific SelectMenu (or its custom_id).

Important

The Function this decorator attached to must be an coroutine(means an awaitable) and take the same parameters as a on_raw_selection_select!

Parameters

custom_id (Optional[str]) – If the custom_id of the SelectMenu could not use as an function name or you want to give the function a different name then the custom_id use this one to set the custom_id.

Example

# the SelectMenu
SelectMenu(custom_id='choose_your_gender',
           options=[
                select_option(label='Female', value='Female', emoji='♀️'),
                select_option(label='Male', value='Male', emoji='♂️'),
                select_option(label='Trans/Non Binary', value='Trans/Non Binary', emoji='⚧')
                ], placeholder='Choose your Gender')

# function that's called when the SelectMenu is used
@client.on_select()
async def choose_your_gender(i: discord.Interaction, select_menu):
    await i.respond(f'You selected `{select_menu.values[0]}`!', hidden=True)
Raises

TypeError – The coroutine passed is not actually a coroutine.

Events

on_button_click(interaction, button)

This Event will be triggered if a Button, that is attached to a Message wich is in the internal Cache, is pressed.

Parameters
  • interaction (discord.Interaction) – The Interaction-object with all his attributes and methods to respond to the interaction

  • button (Union[Button, ButtonClick]) – The ButtonClick if the message is ephemeral else Button. (this is also in the first parameter under component).

Example

@client.event
async def on_button_click(interaction: discord.Interaction, button):
    await interaction.respond('Hey you pressed an Button!', delete_after=10)

on_raw_button_click(interaction, button)

This Event will be triggered if a Button, that is attached to any Message of this Bot is pressed.

Parameters
  • interaction (discord.Interaction) – The discord.Interaction that contains all information about the Interaction.

  • button (Union[Button, ButtonClick]) –

    The ButtonClick if the message is ephemeral else Button. (this is also in the first parameter under component).

Example

@client.event
async def on_raw_button_click(interaction: discord.Interaction, button):
    await interaction.respond('Hey you pressed an Button!', delete_after=10)

on_selection_select(interaction, select_menu)

This Event will be triggered if a SelectMenu, that is attached to a Message of this Bot wich is in the internal Cache, is used.

Example

@client.event
async def on_selection_select(interaction: discord.Interaction, select_menu):
    await interaction.respond(f'Hey {interaction.author.mention} you select {", ".join(select_menu.values)}!', hidden=True)

on_raw_selection_select(interaction, select_menu)

This Event will be triggered if a SelectMenu, that is attached to any Message of this Bot is used.

Parameters
  • interaction (discord.Interaction) –

    The Interaction that contains all information about the Interaction.

  • select (Union[SelectMenu, SelectionSelect]) – The SelectionSelect if the message is ephemeral else SelectMenu but with the values wich contains a list of the selected options. (this is also in the first parameter under component).

Example

@client.event
async def on_raw_selection_select(interaction: discord.Interaction, select_menu):
    await interaction.edit(f'Hey {interaction.author.mention} you select {", ".join(select_menu.values)}!', hidden=True)

class abc.Messageable

This has the same methods as a normal message as well except for a change to the send() method.

classmethod send(**kwargs)

These are the parameters that have been added by this, the others are still present and can be seen in the docs of discord.py

Parameters
  • components (Optional[List[Union[List[Button, SelectionSelect], ActionRow[Button, SelectionSelect]]]]) – A List of components that should send with the Message these can be either in ActionRow’s or in list’s.

  • embeds (Optional[List[discord.Embed]]) – A List of up to 10 discord.Embed’s that should send with the Message.


class ext.commands.Cog

This class has also the decorators for custom_id’s as the normal discord.Client Class.

@commands.Cog.on_click(custom_id=None)

This works like the discord.Client.on_click() decorator of the discord.Client but it give self (The Cog-Class) as the 1. Parameter.

Example

# The Button
Button(label='Hello', custom_id='my_button')

#The decorator
@commands.Cog.on_click()
async def my_button(i: discord.Interaction, button):
    await i.respond('Hey you pressed a Button!')
@commands.Cog.on_select(custom_id=None)

This works like the discord.Client.on_select() decorator of the discord.Client but it give self (The Cog-Class) as the 1. Parameter.

Example

# The SelectMenu
SelectMenu(custom_id='select_decorator_example',
           options=[
               SelectOption('The 1. Option', '1', 'The first option you have', '1️⃣'),
               SelectOption('The 2. Option', '2', 'The second option you have', '2️⃣')
           ], placeholder='Select a Option')

#The decorator
@commands.Cog.on_select()
async def select_decorator_example(i: discord.Interaction, select_menu):
    await i.edit(content=f'You choice was the {select_menu.values[0]}. Option. 😀')

class discord.Message

and

class discord.PartialMessage

This has the same attributes as a normal message but one is added in addition

components

A Optional[List[ActionRow]] containing the components of the message.

all_components

Returns all Button’s and SelectMenu’s that are contained in the message

Note

This is equal to:

for action_row in self.components:
    for component in action_row:
        yield component
Yields

Union[Button, SelectMenu]

all_buttons

Returns all Button’s that are contained in the message

Note

This is equal to:

for action_row in self.components:
    for component in action_row:
        yield component
Yields

Button

all_select_menus

Returns all SelectMenu’s that are contained in the message

Note

This is equal to:

for action_row in self.components:
    for component in action_row:
        if isinstance(component, SelectMenu):
            yield component
Yields

SelectMenu

This has the same methods as a normal message but one is added in addition.

edit(**kwargs)

This takes the same parameters as the normal edit() function except for two added parameters.

Parameters
  • components (Optional[List[Union[List[Button, SelectMenu], ActionRow[Button, SelectMenu]]]]) –

    A List of components that should replace the previous ones these can be either in ActionRow’s or in list’s.

  • embeds (Optional[List[discord.Embed]]) – A list of up to 10 discord.Embed’s to replace the previous ones(This is also usable for normal Messages).

utils

styled_timestamp(timestamp, style)

A small function that returns a styled timestamp for discord, this will be displayed accordingly in the Discord client depending on the style specified.

Timestamps will display the given timestamp in the user’s timezone and locale.

Parameters

Example

@client.command()
async def time(ctx):
    await ctx.send(discord.utils.styled_timestamp(datetime.now(), discord.TimestampStyle.long))
class TimestampStyle

Note

This is located in discord.enums but i place it here

The Styles you could use for the style of a styled_timestamp

See also in the Discord-Documentation

NAME

VALUE

DESCRIPTION

EXAMPLE

short_time

‘t’

Short Time

_images/short_time.png
long_time

‘T’

Long Time

_images/long_time.png
short_date

‘d’

Short Date

_images/short_date.png
long_date

‘D’

Long Date

_images/long_date.png
short

‘f’

Short Date/Time

_images/short.png
long

‘F’

Long Date/Time

_images/long.png
relative

‘R’

Relative Time

_images/relative.png

Indices and tables