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 by AutoShardedClient to 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 by AutoShardedClient to 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.guilds and 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 by AutoShardedClient to denote when a particular shard ID has become ready.

Parameters:

shard_id (int) – The shard ID that is ready.

discord.on_resumed()

Called when the client has resumed a session.

discord.on_shard_resumed(shard_id)

Similar to on_resumed() except used by AutoShardedClient to 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 Client class you can define an on_error handler consisting of a single empty raise statement. Exceptions raised by on_error will not be handled in any way by Client.

Note

on_error will only be dispatched to Client.event().

It will not be received by Client.wait_for(), or, if used, Bots listeners such as listen() or listener().

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)

Called when a user’s subscription renews for the next billing period. The ends_at attribute will have an updated value with the new expiration date.

Attention

If a user’s subscription is cancelled, you will not receive an on_entitlement_delete() event. Instead, you will simply not receive an UPDATE event with a new ends_at date at the end of the billing period.

Added in version 2.0.

Parameters:

entitlement (Entitlement) – The entitlement that was updated.

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

Attention

Entitlements are not deleted when they expire.

Added in version 2.0.

Parameters:

entitlement (Entitlement) – The entitlement 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() fails

By default, this prints to sys.stderr however it could be overridden to have a different implementation.

Parameters:
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_log permission.

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.

Parameters:

msg (Union[bytes, str]) – The message passed in from the WebSocket library. Could be bytes for a binary message or str for a regular message.

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.

Parameters:

payload – The message that is about to be passed on to the WebSocket library. It can be bytes to denote a binary message or str to denote a regular text message.

discord.on_typing(channel, user, when)

Called when someone begins typing a message.

The channel parameter can be a abc.Messageable instance. Which could either be TextChannel, GroupChannel, or DMChannel.

If the channel is a TextChannel then the user parameter is a Member, otherwise it is a User.

This requires Intents.typing to be enabled.

Parameters:
  • channel (abc.Messageable) – The location where the typing originated from.

  • user (Union[User, Member]) – The user that started typing.

  • when (datetime.datetime) – When the typing started as a naive datetime in UTC.

discord.on_message(message)

Called when a Message is created and sent.

This requires Intents.messages to 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 Bot does 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_messages attribute or use the on_raw_message_delete() event instead.

This requires Intents.messages to 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_messages attribute or use the on_raw_bulk_message_delete() event instead.

This requires Intents.messages to 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_message

This requires Intents.messages to 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_messages

This requires Intents.messages to be enabled.

Parameters:

payload (RawBulkMessageDeleteEvent) – The raw event payload data.

discord.on_message_edit(before, after)

Called when a Message receives 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_messages attribute or use the on_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.messages to be enabled.

Parameters:
  • before (Message) – The previous version of the message.

  • after (Message) – The current version of the message.

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 the on_raw_message_edit() coroutine, the RawMessageUpdateEvent.cached_message will return a Message object 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.messages to 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 using on_raw_reaction_add() instead.

Note

To get the Message being reacted, access it via Reaction.message.

This requires Intents.reactions to be enabled.

Note

This doesn’t require Intents.members within 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 using on_raw_reaction_add() if you need this and do not otherwise want to enable the members intent.

Parameters:
  • reaction (Reaction) – The current state of the reaction.

  • user (Union[Member, User]) – The user who added the reaction.

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.reactions to 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.reactions and Intents.members to be enabled.

Note

Consider using on_raw_reaction_remove() if you need this and do not want to enable the members intent.

Parameters:
  • reaction (Reaction) – The current state of the reaction.

  • user (Union[Member, User]) – The user who added the reaction.

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.reactions to 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 using on_raw_reaction_clear() instead.

This requires Intents.reactions to be enabled.

Parameters:
  • message (Message) – The message that had its reactions cleared.

  • reactions (List[Reaction]) – The reactions that were removed.

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.reactions to 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 using on_raw_reaction_clear_emoji() instead.

This requires Intents.reactions to 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.reactions to 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.messages to 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.messages to 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 be None.

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.guilds to 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.guilds to be enabled.

Parameters:
discord.on_guild_channel_pins_update(channel, last_pin)

Called whenever a message is pinned or unpinned from a guild channel.

This requires Intents.guilds to 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 be None.

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.integrations to 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.webhooks to 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 Member leaves or joins a Guild.

This requires Intents.members to be enabled.

Parameters:

member (Member) – The member who joined or left.

discord.on_member_update(before, after)

Called when a Member updates their profile.

This is called when one or more of the following things change:

  • status

  • activity

  • nickname

  • roles

  • pending

This requires Intents.members to be enabled.

Parameters:
  • before (Member) – The updated member’s old info.

  • after (Member) – The updated member’s updated info.

discord.on_user_update(before, after)

Called when a User updates their profile.

This is called when one or more of the following things change:

  • avatar

  • username

  • discriminator

This requires Intents.members to be enabled.

Parameters:
  • before (User) – The updated user’s old info.

  • after (User) – The updated user’s updated info.

discord.on_guild_join(guild)

Called when a Guild is either created by the Client or when the Client joins a guild.

This requires Intents.guilds to be enabled.

Parameters:

guild (Guild) – The guild that was joined.

discord.on_guild_remove(guild)

Called when a Guild is removed from the Client.

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 Client must have been part of the guild to begin with. (i.e. it is part of Client.guilds)

This requires Intents.guilds to be enabled.

Parameters:

guild (Guild) – The guild that got removed.

discord.on_guild_update(before, after)

Called when a Guild updates, for example:

  • Changed name

  • Changed AFK channel

  • Changed AFK timeout

  • etc

This requires Intents.guilds to be enabled.

Parameters:
  • before (Guild) – The guild prior to being updated.

  • after (Guild) – The guild after being updated.

discord.on_guild_role_create(role)
discord.on_guild_role_delete(role)

Called when a Guild creates or deletes a new Role.

To get the guild it belongs to, use Role.guild.

This requires Intents.guilds to be enabled.

Parameters:

role (Role) – The role that was created or deleted.

discord.on_guild_role_update(before, after)

Called when a Role is changed guild-wide.

This requires Intents.guilds to be enabled.

Parameters:
  • before (Role) – The updated role’s old info.

  • after (Role) – The updated role’s updated info.

discord.on_guild_emojis_update(guild, before, after)

Called when a Guild adds or removes Emoji.

This requires Intents.emojis to be enabled.

Parameters:
  • guild (Guild) – The guild who got their emojis updated.

  • before (Sequence[Emoji]) – A list of emojis before the update.

  • after (Sequence[Emoji]) – A list of emojis after the update.

discord.on_guild_available(guild)
discord.on_guild_unavailable(guild)

Called when a guild becomes available or unavailable. The guild must have existed in the Client.guilds cache.

This requires Intents.guilds to be enabled.

Parameters:

guild – The Guild that has changed availability.

discord.on_voice_state_update(member, before, after)

Called when a Member changes their VoiceState.

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_states to 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 status gets 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.bans to be enabled.

Parameters:
  • guild (Guild) – The guild the user got banned from.

  • user (Union[User, Member]) – The user that got banned. Can be either User or Member depending if the user was in the guild or not at the time of removal.

discord.on_member_unban(guild, user)

Called when a User gets unbanned from a Guild.

This requires Intents.bans to be enabled.

Parameters:
  • guild (Guild) – The guild the user got unbanned from.

  • user (User) – The user that got unbanned.

discord.on_invite_create(invite)

Called when an Invite is created. You must have the manage_channels permission to receive this.

Added in version 1.3.

Note

There is a rare possibility that the Invite.guild and Invite.channel attributes will be of Object rather than the respective models.

This requires Intents.invites to be enabled.

Parameters:

invite (Invite) – The invite that was created.

discord.on_invite_delete(invite)

Called when an Invite is deleted. You must have the manage_channels permission to receive this.

Added in version 1.3.

Note

There is a rare possibility that the Invite.guild and Invite.channel attributes will be of Object rather 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.invites to 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:
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 AutoModRule was 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 a Message wich 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 and on_raw_button_click() to make a callback for buttons

Parameters:
discord.on_raw_button_click(interaction, button)

Called when a Button, that is attached to any Message of the bot, is pressed.

Warning

This may be removed and be included in on_button_click() in a future release

Parameters:
discord.on_selection_select(interaction, select_menu)

Called when a SelectMenu, that is attached to a Message wich 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 and on_raw_selection_select() to make a callback for select menus

Parameters:
discord.on_raw_selection_select(interaction, select_menu)

Called when a SelectMenu, that is attached to any Message of the bot, is used.

Warning

This may be removed and be included in on_selection_select() in a future release

Parameters:
discord.on_modal_submit(interaction)

Called when a user press the Submit button in a Modal.

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 GuildScheduledEvent is created.

Parameters:

event (GuildScheduledEvent) – The event that was created.

discord.on_scheduled_event_update(before, after)

Called when a GuildScheduledEvent is updated.

Parameters:
discord.on_scheduled_event_delete(event)

Called when a GuildScheduledEvent is deleted.

Parameters:

event (GuildScheduledEvent) – The event that was deleted.

discord.on_stage_instance_create(stage_instance)

Called when a StageInstance is created.

Parameters:

stage_instance (StageInstance) – The stage instance that was created.

discord.on_stage_instance_update(before, after)

Called when a StageInstance is updated.

Parameters:
  • before (StageInstance) – The old stage instance.

  • after (StageInstance) – The updated stage instance.

discord.on_stage_instance_delete(stage_instance)

Called when a StageInstance is 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: