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 thecustom_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.
# 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 thecustom_id
of theSelectMenu
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 interactionbutton (Union[
Button
,ButtonClick
]) – The ButtonClick if the message is ephemeral else Button. (this is also in the first parameter undercomponent
).
@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
) – Thediscord.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
).
@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 thevalues
wich contains a list of the selected options. (this is also in the first parameter undercomponent
).
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 thediscord.Client
but it giveself
(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 thediscord.Client
but it giveself
(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
- all_components¶
Returns all
Button
’s andSelectMenu
’s that are contained in the messageNote
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 messageNote
This is equal to:
for action_row in self.components: for component in action_row: yield component
- Yields
Returns all
SelectMenu
’s that are contained in the messageNote
This is equal to:
for action_row in self.components: for component in action_row: if isinstance(component, SelectMenu): yield component
- Yields
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 10discord.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
timestamp – Union[datetime.datetime,
int
] The timestamp; Adatetime.datetime
object or an already completed timestamp.style –
Optional[Union[
TimestampStyle
,str
]] How the timestamp should be displayed in Discord; this can either be aTimestampStyle
or directly the associated value.- default
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 astyled_timestamp
See also in the Discord-Documentation
NAME
VALUE
DESCRIPTION
EXAMPLE
- short_time¶
‘t’
Short Time
- long_time¶
‘T’
Long Time
- short_date¶
‘d’
Short Date
- long_date¶
‘D’
Long Date
- short¶
‘f’
Short Date/Time
- long¶
‘F’
Long Date/Time
- relative¶
‘R’
Relative Time