Event Reference
This section outlines the different types of events listened by Client.
There are three ways to register an event, the first way is through the use of
Client.event(). The second way is through subclassing Client and
overriding the specific events. For example:
import discord
class MyClient(discord.Client):
async def on_message(self, message):
if message.author == self.user:
return
# Note that for commands you should consider using the ext.commands framework or just slash commands
if message.content.startswith("$hello"):
await message.channel.send("Hello World!")
The third way is to use the Client.once() decorator. Which servers as a one-time event listener. For example:
import discord
client = discord.Client()
@client.once()
async def ready():
print("Hey there, I'm online!")
Hint
If you are using Cogs of the (prefix)-commands framework, then
– in addition to the above – you can use the commands.Cog.listener
decorator in them to listen to events.
If an event handler raises an exception, on_error() will be called
to handle it, which defaults to print a traceback and ignoring the exception.
Warning
All the events must be a coroutine. If they aren’t, then you might get unexpected
errors. In order to turn a function into a coroutine they must be async def
functions.
- discord.on_connect()
Called when the client has successfully connected to Discord. This is not the same as the client being fully prepared, see
on_ready()for that.The warnings on
on_ready()also apply.
- discord.on_shard_connect(shard_id)
Similar to
on_connect()except used byAutoShardedClientto denote when a particular shard ID has connected to Discord.Added in version 1.4.
- Parameters:
shard_id (
int) – The shard ID that has connected.
- discord.on_disconnect()
Called when the client has disconnected from Discord, or a connection attempt to Discord has failed. This could happen either through the internet being disconnected, explicit calls to close, or Discord terminating the connection one way or the other.
This function can be called many times without a corresponding
on_connect()call.
- discord.on_shard_disconnect(shard_id)
Similar to
on_disconnect()except used byAutoShardedClientto denote when a particular shard ID has disconnected from Discord.Added in version 1.4.
- Parameters:
shard_id (
int) – The shard ID that has disconnected.
- discord.on_ready()
Called when the client is done preparing the data received from Discord. Usually after login is successful and the
Client.guildsand co. are filled up.Warning
This function is not guaranteed to be the first event called. Likewise, this function is not guaranteed to only be called once. This library implements reconnection logic and thus will end up calling this event whenever a RESUME request fails.
- discord.on_shard_ready(shard_id)
Similar to
on_ready()except used byAutoShardedClientto denote when a particular shard ID has become ready.- Parameters:
shard_id (
int) – The shard ID that is ready.
- discord.on_shard_resumed(shard_id)
Similar to
on_resumed()except used byAutoShardedClientto denote when a particular shard ID has resumed a session.Added in version 1.4.
- Parameters:
shard_id (
int) – The shard ID that has resumed.
- discord.on_error(event, *args, **kwargs)
Usually when an event raises an uncaught exception, a traceback is printed to stderr and the exception is ignored. If you want to change this behaviour and handle the exception for whatever reason yourself, this event can be overridden. Which, when done, will suppress the default action of printing the traceback.
The information of the exception raised and the exception itself can be retrieved with a standard call to
sys.exc_info().If you want exception to propagate out of the
Clientclass you can define anon_errorhandler consisting of a single empty raise statement. Exceptions raised byon_errorwill not be handled in any way byClient.Note
on_errorwill only be dispatched toClient.event().It will not be received by
Client.wait_for(), or, if used, Bots listeners such aslisten()orlistener().- Parameters:
event (
str) – The name of the event that raised the exception.args – The positional arguments for the event that raised the exception.
kwargs – The keyword arguments for the event that raised the exception.
- discord.on_entitlement_create(entitlement)
Called when a user subscribes to a SKU.
Added in version 2.0.
- Parameters:
entitlement (
Entitlement) – The entitlement that was created.
- discord.on_entitlement_update(entitlement)
Sent when an entitlement is updated.
Attention
If a user’s subscription is cancelled, you will not receive an
on_entitlement_delete()event. When a user cancels, you will receive an ENTITLEMENT_UPDATE events with a validends_atvalue reflecting when their subscription endsAdded in version 2.0.
- param entitlement:
The entitlement that was updated.
- type entitlement:
- discord.on_entitlement_delete(entitlement)
Called when a user’s entitlement is deleted.
Entitlement deletions are infrequent, and occur when:
Discord issues a refund for a subscription
Discord removes an entitlement from a user via internal tooling
Discord deletes an app-managed entitlement they created via the API
Attention
Entitlements are not deleted when they expire.
Added in version 2.0.
- Parameters:
entitlement (
Entitlement) – The entitlement that was deleted.
- discord.on_subscription_create(subscription)
Called when a user subscribes to a SKU.
Note
A Subscription’s status can be either inactive or active when this event is received. You will receive subsequent
on_subscription_update()events if the status is updated to active.As a best practice, you should not grant any perks to users until the entitlements are created.
Added in version 2.0.
- Parameters:
subscription (
Subscription) – The subscription that was created.
- discord.on_subscription_update(subscription)
Sent when a subscription is updated.
Added in version 2.0.
- Parameters:
subscription (
Subscription) – The subscription that was updated.
- discord.on_subscription_delete(subscription)
Called when a user’s subscription is deleted.
Added in version 2.0.
- Parameters:
subscription (
Subscription) – The subscription that was deleted.
- discord.on_application_command_error(command, interaction, exception)
The default error handler when an exception was raised while invoking an application-command .
Note
This includes when a
check()failsBy default, this prints to
sys.stderrhowever it could be overridden to have a different implementation.- Parameters:
command (
ApplicationCommand) – TheSlashCommand/SubCommand,MessageCommandorUserCommandin which invocation the exception was raised.interaction (
ApplicationCommandInteraction) – The interaction that was invokedexception (
Exception) – The exception that was raised
- discord.on_audit_log_entry_create(guild, entry)
Called whenever a guild audit log entry is created.
Note
This event is only sent to bots with the
view_audit_logpermission.Added in version 2.0.
- Parameters:
guild (
Guild) – The guild that the audit log entry was created in.entry (
AuditLogEntry) – The audit log entry that was created.
- discord.on_socket_raw_receive(msg)
Called whenever a message is received from the WebSocket, before it’s processed. This event is always dispatched when a message is received and the passed data is not processed in any way.
This is only really useful for grabbing the WebSocket stream and debugging purposes.
Note
This is only for the messages received from the client WebSocket. The voice WebSocket will not trigger this event.
- discord.on_socket_raw_send(payload)
Called whenever a send operation is done on the WebSocket before the message is sent. The passed parameter is the message that is being sent to the WebSocket.
This is only really useful for grabbing the WebSocket stream and debugging purposes.
Note
This is only for the messages sent from the client WebSocket. The voice WebSocket will not trigger this event.
- discord.on_typing(channel, user, when)
Called when someone begins typing a message.
The
channelparameter can be aabc.Messageableinstance. Which could either beTextChannel,GroupChannel, orDMChannel.If the
channelis aTextChannelthen theuserparameter is aMember, otherwise it is aUser.This requires
Intents.typingto be enabled.- Parameters:
channel (
abc.Messageable) – The location where the typing originated from.when (
datetime.datetime) – When the typing started as a naive datetime in UTC.
- discord.on_message(message)
Called when a
Messageis created and sent.This requires
Intents.messagesto be enabled.Warning
Your bot’s own messages and private messages are sent through this event. This can lead cases of ‘recursion’ depending on how your bot was programmed. If you want the bot to not reply to itself, consider checking the user IDs. Note that
Botdoes not have this problem.- Parameters:
message (
Message) – The current message.
- discord.on_message_delete(message)
Called when a message is deleted. If the message is not found in the internal message cache, then this event will not be called. Messages might not be in cache if the message is too old or the client is participating in high traffic guilds.
If this occurs increase the
Client.max_messagesattribute or use theon_raw_message_delete()event instead.This requires
Intents.messagesto be enabled.- Parameters:
message (
Message) – The deleted message.
- discord.on_bulk_message_delete(messages)
Called when messages are bulk deleted. If none of the messages deleted are found in the internal message cache, then this event will not be called. If individual messages were not found in the internal message cache, this event will still be called, but the messages not found will not be included in the messages list. Messages might not be in cache if the message is too old or the client is participating in high traffic guilds.
If this occurs increase the
Client.max_messagesattribute or use theon_raw_bulk_message_delete()event instead.This requires
Intents.messagesto be enabled.- Parameters:
messages (List[
Message]) – The messages that have been deleted.
- discord.on_raw_message_delete(payload)
Called when a message is deleted. Unlike
on_message_delete(), this is called regardless of the message being in the internal message cache or not.If the message is found in the message cache, it can be accessed via
RawMessageDeleteEvent.cached_messageThis requires
Intents.messagesto be enabled.- Parameters:
payload (
RawMessageDeleteEvent) – The raw event payload data.
- discord.on_raw_bulk_message_delete(payload)
Called when a bulk delete is triggered. Unlike
on_bulk_message_delete(), this is called regardless of the messages being in the internal message cache or not.If the messages are found in the message cache, they can be accessed via
RawBulkMessageDeleteEvent.cached_messagesThis requires
Intents.messagesto be enabled.- Parameters:
payload (
RawBulkMessageDeleteEvent) – The raw event payload data.
- discord.on_message_edit(before, after)
Called when a
Messagereceives an update event. If the message is not found in the internal message cache, then these events will not be called. Messages might not be in cache if the message is too old or the client is participating in high traffic guilds.If this occurs increase the
Client.max_messagesattribute or use theon_raw_message_edit()event instead.The following non-exhaustive cases trigger this event:
A message has been pinned or unpinned.
The message content has been changed.
The message has received an embed.
For performance reasons, the embed server does not do this in a “consistent” manner.
The message’s embeds were suppressed or unsuppressed.
A call message has received an update to its participants or ending time.
This requires
Intents.messagesto be enabled.
- discord.on_raw_message_edit(payload)
Called when a message is edited. Unlike
on_message_edit(), this is called regardless of the state of the internal message cache.If the message is found in the message cache, it can be accessed via
RawMessageUpdateEvent.cached_message. The cached message represents the message before it has been edited. For example, if the content of a message is modified and triggers theon_raw_message_edit()coroutine, theRawMessageUpdateEvent.cached_messagewill return aMessageobject that represents the message before the content was modified.Due to the inherently raw nature of this event, the data parameter coincides with the raw data given by the gateway.
Since the data payload can be partial, care must be taken when accessing stuff in the dictionary. One example of a common case of partial data is when the
'content'key is inaccessible. This denotes an “embed” only edit, which is an edit in which only the embeds are updated by the Discord embed server.This requires
Intents.messagesto be enabled.- Parameters:
payload (
RawMessageUpdateEvent) – The raw event payload data.
- discord.on_reaction_add(reaction, user)
Called when a message has a reaction added to it. Similar to
on_message_edit(), if the message is not found in the internal message cache, then this event will not be called. Consider usingon_raw_reaction_add()instead.Note
To get the
Messagebeing reacted, access it viaReaction.message.This requires
Intents.reactionsto be enabled.Note
This doesn’t require
Intents.memberswithin a guild context, but due to Discord not providing updated user information in a direct message it’s required for direct messages to receive this event. Consider usingon_raw_reaction_add()if you need this and do not otherwise want to enable the members intent.
- discord.on_raw_reaction_add(payload)
Called when a message has a reaction added. Unlike
on_reaction_add(), this is called regardless of the state of the internal message cache.This requires
Intents.reactionsto be enabled.- Parameters:
payload (
RawReactionActionEvent) – The raw event payload data.
- discord.on_reaction_remove(reaction, user)
Called when a message has a reaction removed from it. Similar to on_message_edit, if the message is not found in the internal message cache, then this event will not be called.
Note
To get the message being reacted, access it via
Reaction.message.This requires both
Intents.reactionsandIntents.membersto be enabled.Note
Consider using
on_raw_reaction_remove()if you need this and do not want to enable the members intent.
- discord.on_raw_reaction_remove(payload)
Called when a message has a reaction removed. Unlike
on_reaction_remove(), this is called regardless of the state of the internal message cache.This requires
Intents.reactionsto be enabled.- Parameters:
payload (
RawReactionActionEvent) – The raw event payload data.
- discord.on_reaction_clear(message, reactions)
Called when a message has all its reactions removed from it. Similar to
on_message_edit(), if the message is not found in the internal message cache, then this event will not be called. Consider usingon_raw_reaction_clear()instead.This requires
Intents.reactionsto be enabled.
- discord.on_raw_reaction_clear(payload)
Called when a message has all its reactions removed. Unlike
on_reaction_clear(), this is called regardless of the state of the internal message cache.This requires
Intents.reactionsto be enabled.- Parameters:
payload (
RawReactionClearEvent) – The raw event payload data.
- discord.on_reaction_clear_emoji(reaction)
Called when a message has a specific reaction removed from it. Similar to
on_message_edit(), if the message is not found in the internal message cache, then this event will not be called. Consider usingon_raw_reaction_clear_emoji()instead.This requires
Intents.reactionsto be enabled.Added in version 1.3.
- Parameters:
reaction (
Reaction) – The reaction that got cleared.
- discord.on_raw_reaction_clear_emoji(payload)
Called when a message has a specific reaction removed from it. Unlike
on_reaction_clear_emoji()this is called regardless of the state of the internal message cache.This requires
Intents.reactionsto be enabled.Added in version 1.3.
- Parameters:
payload (
RawReactionClearEmojiEvent) – The raw event payload data.
- discord.on_private_channel_delete(channel)
- discord.on_private_channel_create(channel)
Called whenever a private channel is deleted or created.
This requires
Intents.messagesto be enabled.- Parameters:
channel (
abc.PrivateChannel) – The private channel that got created or deleted.
- discord.on_private_channel_update(before, after)
Called whenever a private group DM is updated. e.g. changed name or topic.
This requires
Intents.messagesto be enabled.- Parameters:
before (
GroupChannel) – The updated group channel’s old info.after (
GroupChannel) – The updated group channel’s new info.
- discord.on_private_channel_pins_update(channel, last_pin)
Called whenever a message is pinned or unpinned from a private channel.
- Parameters:
channel (
abc.PrivateChannel) – The private channel that had its pins updated.last_pin (Optional[
datetime.datetime]) – The latest message that was pinned as a naive datetime in UTC. Could beNone.
- discord.on_guild_channel_delete(channel)
- discord.on_guild_channel_create(channel)
Called whenever a guild channel is deleted or created.
Note that you can get the guild from
guild.This requires
Intents.guildsto be enabled.- Parameters:
channel (
abc.GuildChannel) – The guild channel that got created or deleted.
- discord.on_guild_channel_update(before, after)
Called whenever a guild channel is updated. e.g. changed name, topic, permissions.
This requires
Intents.guildsto be enabled.- Parameters:
before (
abc.GuildChannel) – The updated guild channel’s old info.after (
abc.GuildChannel) – The updated guild channel’s new info.
- discord.on_guild_channel_pins_update(channel, last_pin)
Called whenever a message is pinned or unpinned from a guild channel.
This requires
Intents.guildsto be enabled.- Parameters:
channel (
abc.GuildChannel) – The guild channel that had its pins updated.last_pin (Optional[
datetime.datetime]) – The latest message that was pinned as a naive datetime in UTC. Could beNone.
- discord.on_guild_integrations_update(guild)
Added in version 1.4.
Called whenever an integration is created, modified, or removed from a guild.
This requires
Intents.integrationsto be enabled.- Parameters:
guild (
Guild) – The guild that had its integrations updated.
- discord.on_webhooks_update(channel)
Called whenever a webhook is created, modified, or removed from a guild channel.
This requires
Intents.webhooksto be enabled.- Parameters:
channel (
abc.GuildChannel) – The channel that had its webhooks updated.
- discord.on_member_join(member)
- discord.on_member_remove(member)
Called when a
Memberleaves or joins aGuild.This requires
Intents.membersto be enabled.- Parameters:
member (
Member) – The member who joined or left.
- discord.on_member_update(before, after)
Called when a
Memberupdates their profile.This is called when one or more of the following things change:
status
activity
nickname
roles
pending
This requires
Intents.membersto be enabled.
- discord.on_user_update(before, after)
Called when a
Userupdates their profile.This is called when one or more of the following things change:
avatar
username
discriminator
This requires
Intents.membersto be enabled.
- discord.on_guild_join(guild)
Called when a
Guildis either created by theClientor when theClientjoins a guild.This requires
Intents.guildsto be enabled.- Parameters:
guild (
Guild) – The guild that was joined.
- discord.on_guild_remove(guild)
Called when a
Guildis removed from theClient.This happens through, but not limited to, these circumstances:
The client got banned.
The client got kicked.
The client left the guild.
The client or the guild owner deleted the guild.
In order for this event to be invoked then the
Clientmust have been part of the guild to begin with. (i.e. it is part ofClient.guilds)This requires
Intents.guildsto be enabled.- Parameters:
guild (
Guild) – The guild that got removed.
- discord.on_guild_update(before, after)
Called when a
Guildupdates, for example:Changed name
Changed AFK channel
Changed AFK timeout
etc
This requires
Intents.guildsto be enabled.
- discord.on_guild_role_create(role)
- discord.on_guild_role_delete(role)
Called when a
Guildcreates or deletes a newRole.To get the guild it belongs to, use
Role.guild.This requires
Intents.guildsto be enabled.- Parameters:
role (
Role) – The role that was created or deleted.
- discord.on_guild_role_update(before, after)
Called when a
Roleis changed guild-wide.This requires
Intents.guildsto be enabled.
- discord.on_guild_emojis_update(guild, before, after)
Called when a
Guildadds or removesEmoji.This requires
Intents.emojisto be enabled.
- discord.on_guild_available(guild)
Called when a guild becomes available or unavailable. The guild must have existed in the
Client.guildscache.This requires
Intents.guildsto be enabled.- Parameters:
guild – The
Guildthat has changed availability.
- discord.on_voice_state_update(member, before, after)
Called when a
Memberchanges theirVoiceState.The following, but not limited to, examples illustrate when this event is called:
A member joins a voice channel.
A member leaves a voice channel.
A member is muted or deafened by their own accord.
A member is muted or deafened by a guild administrator.
This requires
Intents.voice_statesto be enabled.- Parameters:
member (
Member) – The member whose voice states changed.before (
VoiceState) – The voice state prior to the changes.after (
VoiceState) – The voice state after the changes.
- discord.on_voice_channel_status_update(channel, before, after)
Called when the
statusgets changed by a member or cleared by discord automatically.Added in version 2.0.
- Parameters:
channel (
VoiceChannel) – The voice channel that had its status updated.before (
str) – The voice channel’s old status.after (
str) – The voice channel’s new status.
- discord.on_member_ban(guild, user)
Called when user gets banned from a
Guild.This requires
Intents.bansto be enabled.
- discord.on_member_unban(guild, user)
Called when a
Usergets unbanned from aGuild.This requires
Intents.bansto be enabled.
- discord.on_invite_create(invite)
Called when an
Inviteis created. You must have themanage_channelspermission to receive this.Added in version 1.3.
Note
There is a rare possibility that the
Invite.guildandInvite.channelattributes will be ofObjectrather than the respective models.This requires
Intents.invitesto be enabled.- Parameters:
invite (
Invite) – The invite that was created.
- discord.on_invite_delete(invite)
Called when an
Inviteis deleted. You must have themanage_channelspermission to receive this.Added in version 1.3.
Note
There is a rare possibility that the
Invite.guildandInvite.channelattributes will be ofObjectrather than the respective models.Outside of those two attributes, the only other attribute guaranteed to be filled by the Discord gateway for this event is
Invite.code.This requires
Intents.invitesto be enabled.- Parameters:
invite (
Invite) – The invite that was deleted.
- discord.on_group_join(channel, user)
- discord.on_group_remove(channel, user)
Called when someone joins or leaves a
GroupChannel.- Parameters:
channel (
GroupChannel) – The group that the user joined or left.user (
User) – The user that joined or left.
- discord.on_automod_rule_create(rule)
Called when a auto moderation rule is created.
- Parameters:
rule (
AutoModRule) – The rule that was created.
- discord.on_automod_rule_update(before, after)
Called when a auto moderation rule is updated.
- Parameters:
before (
AutoModRule) – The old rule.after (
AutoModRule) – The updated rule.
- discord.on_automod_rule_delete(rule)
Called when a auto moderation rule is deleted.
- Parameters:
rule (
AutoModRule) – The rule that was deleted.
- discord.on_automod_action(payload)
Called when a
AutoModRulewas triggered by a user- Parameters:
payload (
AutoModActionPayload) – The payload containing all the information
- discord.on_button_click(interaction, button)
Called when a
Button, that is attached to aMessagewhich is in the internal cache, is pressed.Note
In general it is more efficient to use
on_click()/ext.commands.Cog.on_click()instead of this andon_raw_button_click()to make a callback for buttons- Parameters:
interaction (
ComponentInteraction) – The Interaction-object with all his attributes and methods to respond to the interactionbutton (
Button) – The button that was pressed. (this is also available underdiscord.ComponentInteraction.component).
- discord.on_raw_button_click(interaction, button)
Called when a
Button, that is attached to anyMessageof the bot, is pressed.Warning
This may be removed and be included in
on_button_click()in a future release- Parameters:
interaction (
ComponentInteraction) – The Interaction-object with all his attributes and methods to respond to the interactionbutton (
Button) – The button that was pressed. (this is also available underdiscord.ComponentInteraction.component).
- discord.on_selection_select(interaction, select_menu)
Called when a
SelectMenu, that is attached to aMessagewhich is in the internal cache, is used.Note
In general it is more efficient to use
on_select()/ext.commands.Cog.on_select()instead of this andon_raw_selection_select()to make a callback for select menus- Parameters:
interaction (
ComponentInteraction) – The Interaction-object with all his attributes and methods to respond to the interactionselect_menu (
SelectMenu) – TheSelectMenubut with theSelectMenu.valuesset which contains a list of the selectedoptions. (this is also available underdiscord.ComponentInteraction.component).
- discord.on_raw_selection_select(interaction, select_menu)
Called when a
SelectMenu, that is attached to anyMessageof the bot, is used.Warning
This may be removed and be included in
on_selection_select()in a future release- Parameters:
interaction (
ComponentInteraction) – The Interaction-object with all his attributes and methods to respond to the interactionselect_menu (
SelectMenu) – TheSelectMenubut with theSelectMenu.valuesset which contains a list of the selectedoptions. (this is also available underdiscord.ComponentInteraction.component).
- discord.on_modal_submit(interaction)
Called when a user press the
Submitbutton in aModal.Note
In general it is more efficient to use
on_submit()/ext.commands.Cog.on_submit()instead of this to make a callback for modals- Parameters:
interaction (
ModalSubmitInteraction) – he Interaction-object with all his attributes and methods to respond to the interaction
- on_application_command_permissions_update(guild, command, new_permissions):
Called when the permissions for an application command are updated.
- Parameters:
guild (
Guild) – The guild where the permissions were updated.command (
ApplicationCommand) – The command that was updated.new_permissions (
ApplicationCommandPermissions) – The new permissions for the command.
- discord.on_scheduled_event_create(event)
Called when a
GuildScheduledEventis created.- Parameters:
event (
GuildScheduledEvent) – The event that was created.
- discord.on_scheduled_event_update(before, after)
Called when a
GuildScheduledEventis updated.- Parameters:
before (
GuildScheduledEvent) – The old event.after (
GuildScheduledEvent) – The updated event.
- discord.on_scheduled_event_delete(event)
Called when a
GuildScheduledEventis deleted.- Parameters:
event (
GuildScheduledEvent) – The event that was deleted.
- discord.on_stage_instance_create(stage_instance)
Called when a
StageInstanceis created.- Parameters:
stage_instance (
StageInstance) – The stage instance that was created.
- discord.on_stage_instance_update(before, after)
Called when a
StageInstanceis updated.- Parameters:
before (
StageInstance) – The old stage instance.after (
StageInstance) – The updated stage instance.
- discord.on_stage_instance_delete(stage_instance)
Called when a
StageInstanceis deleted.- Parameters:
stage_instance (
StageInstance) – The stage instance that was deleted.
- on_voice_channel_effect_send(channel, payload):
Called when a user uses a voice effect in a
VoiceChannel.- Parameters:
channel (
VoiceChannel) – The voice channel in which the effect was used.payload (
VoiceChannelEffectSendEvent) – The payload containing info about the effect.