Skip to main content

Module event

Module event 

Source
Expand description

Typed client events (D-222).

Every event is a zero-sized value on Events — Events::MessageCreate, Events::GuildMemberUpdate, … — whose type determines the payload a handler receives:

client.on(Events::MessageCreate, |_ctx, message| async move {
    // `message` is an `Arc<MessageCreate>`, which dereferences to `Message`.
    println!("{}", message.content);
    Ok(())
});

client.on(Events::MessageUpdate, |_ctx, update| async move {
    // `update.old` is the cached message before the edit, if the cache held it (D-221);
    // the payload dereferences to the new message.
    let before = update.old.as_ref().map(|old| old.content.as_str());
    println!("{before:?} -> {}", update.content);
    Ok(())
});

§What a handler receives

  • Most events hand their djs-model payload unchanged, behind an Arc (D-225).
  • Events whose discord.js counterpart hands an old or removed object (D-227) hand a type from payload with the state the cache held before the event in old and Discord’s data in new, and dereference to new (D-228). old is None without a cache, with the resource disabled in CacheConfig, or for something the cache never received.
  • Some events are derived by the client (D-223): GuildAvailable / GuildUnavailable / GuildCreate / GuildDelete are told apart with or without a cache; the per-emoji and per-sticker events need that resource in the cache.
  • Raw hands every gateway dispatch as its DispatchEvent before anything else.

§Order

For each gateway dispatch the client first applies it to the cache (D-171), then emits Raw, then the event itself, then the events derived from it (for example GuildEmojisUpdate, then one GuildEmojiCreate per new emoji).

§Intents

Each event lists the Intents Discord sends it with, also as Event::INTENTS; events without an intent are always sent. At login the client warns about handlers whose events none of its intents covers (D-271), and Client::required_intents tells which intents the registered handlers need (D-272). Message content in guilds also needs the privileged MESSAGE_CONTENT intent, which no event lists: without it the content, embeds, attachments and components of other users’ messages arrive empty.

§Partials (discord.js)

discord.js drops events about objects it has not cached unless the matching partials are enabled. djs-rs has no such option (D-273): every event arrives, and what the cache did not hold is simply missing — old is None, and payloads carry IDs where Discord sends IDs. Complete them with the managers when needed:

discord.js partialWhat djs-rs hands insteadTo complete it
Partials.MessageMessageUpdate / MessageDelete with old: None (MessageUpdate still has the new message)ctx.messages(channel).fetch(id) (not after a deletion)
Partials.ChannelDM events arrive without a cached channel; the payloads carry channel_idctx.channels().fetch(id)
Partials.ReactionMessageReactionAdd / MessageReactionRemove carry the message, channel and user IDsctx.messages(channel).fetch(message)
Partials.Useruser IDs (user_id) in reaction, typing and poll eventsctx.users().fetch(id)
Partials.GuildMemberGuildMemberUpdate / GuildMemberRemove with old: None (the new data or the user)ctx.members(guild).fetch(user)
Partials.ThreadMemberThreadMemberUpdate / ThreadMembersUpdate payloads as Discord sends themctx.thread_members(thread).fetch(user)
Partials.GuildScheduledEventGuildScheduledEventUserAdd / GuildScheduledEventUserRemove with IDs; update / delete with old: Nonectx.scheduled_events(guild).fetch(id)
Partials.SoundboardSoundsoundboard update / delete with old: Nonectx.soundboard_sounds(guild).fetch(id)
Partials.Poll, Partials.PollAnswerMessagePollVoteAdd / MessagePollVoteRemove with the message ID and answer IDctx.messages(channel).fetch(message) (its poll)

discord.js counterpart: the Events enum.

Modules§

payload
The payloads the client builds itself (D-225): events with the state before them (D-221, D-227), events derived from a list (D-223) and shard lifecycle events (D-226).

Structs§

ApplicationCommandPermissionsUpdate
An application command’s permissions were updated (APPLICATION_COMMAND_PERMISSIONS_UPDATE).
AutoModerationActionExecution
An auto moderation rule acted on a message or member (AUTO_MODERATION_ACTION_EXECUTION). Intent: AUTO_MODERATION_EXECUTION.
AutoModerationRuleCreate
An auto moderation rule was created (AUTO_MODERATION_RULE_CREATE). Intent: AUTO_MODERATION_CONFIGURATION.
AutoModerationRuleDelete
An auto moderation rule was deleted (AUTO_MODERATION_RULE_DELETE). Intent: AUTO_MODERATION_CONFIGURATION.
AutoModerationRuleUpdate
An auto moderation rule was updated (AUTO_MODERATION_RULE_UPDATE). Intent: AUTO_MODERATION_CONFIGURATION.
ChannelCreate
A guild channel was created (CHANNEL_CREATE). Intent: GUILDS.
ChannelDelete
A channel was deleted (CHANNEL_DELETE), with the cached channel. Intent: GUILDS.
ChannelInfo
The ephemeral data of a guild’s channels (CHANNEL_INFO), the answer to a RequestChannelInfo command.
ChannelPinsUpdate
A message was pinned or unpinned (CHANNEL_PINS_UPDATE). Intents: GUILDS, DIRECT_MESSAGES.
ChannelUpdate
A channel was updated (CHANNEL_UPDATE), with the cached channel before the update. Intent: GUILDS.
ClientReady
The client is ready: fired once per login, at the READY of the last of its shards to become ready (D-224).
EntitlementCreate
A user subscribed to a SKU (ENTITLEMENT_CREATE).
EntitlementDelete
An entitlement was deleted (ENTITLEMENT_DELETE).
EntitlementUpdate
An entitlement was updated, for example renewed (ENTITLEMENT_UPDATE).
Events
The events a Client can emit, as typed values (D-222).
GuildAuditLogEntryCreate
An audit log entry was created (GUILD_AUDIT_LOG_ENTRY_CREATE). Intent: GUILD_MODERATION, and the bot needs the VIEW_AUDIT_LOG permission.
GuildAvailable
A guild became available (GUILD_CREATE of a guild from READY or back from an outage). Intent: GUILDS.
GuildBanAdd
A user was banned (GUILD_BAN_ADD). Intent: GUILD_MODERATION.
GuildBanRemove
A user was unbanned (GUILD_BAN_REMOVE). Intent: GUILD_MODERATION.
GuildCreate
The bot joined a guild (GUILD_CREATE of a guild it was not waiting for). Intent: GUILDS.
GuildDelete
The bot left a guild, was removed, or the guild was deleted (GUILD_DELETE without unavailable), with the cached guild. Intent: GUILDS.
GuildEmojiCreate
An emoji was created, derived from GuildEmojisUpdate with emojis in the cache.
GuildEmojiDelete
An emoji was deleted, derived from GuildEmojisUpdate with emojis in the cache.
GuildEmojiUpdate
An emoji was updated, derived from GuildEmojisUpdate with emojis in the cache.
GuildEmojisUpdate
A guild’s emojis changed (GUILD_EMOJIS_UPDATE): the full new list. Intent: GUILD_EXPRESSIONS.
GuildIntegrationsUpdate
A guild’s integrations changed (GUILD_INTEGRATIONS_UPDATE). Intent: GUILD_INTEGRATIONS.
GuildMemberAdd
A user joined a guild (GUILD_MEMBER_ADD). Intent: GUILD_MEMBERS (privileged).
GuildMemberRemove
A user left or was removed from a guild (GUILD_MEMBER_REMOVE), with the cached member. Intent: GUILD_MEMBERS (privileged).
GuildMemberUpdate
A member was updated (GUILD_MEMBER_UPDATE), with the cached member before the update. Intent: GUILD_MEMBERS (privileged).
GuildMembersChunk
A chunk of a guild’s members (GUILD_MEMBERS_CHUNK), the answer to a RequestGuildMembers command.
GuildRoleCreate
A role was created (GUILD_ROLE_CREATE). Intent: GUILDS.
GuildRoleDelete
A role was deleted (GUILD_ROLE_DELETE), with the cached role. Intent: GUILDS.
GuildRoleUpdate
A role was updated (GUILD_ROLE_UPDATE), with the cached role before the update. Intent: GUILDS.
GuildScheduledEventCreate
A scheduled event was created (GUILD_SCHEDULED_EVENT_CREATE). Intent: GUILD_SCHEDULED_EVENTS.
GuildScheduledEventDelete
A scheduled event was deleted (GUILD_SCHEDULED_EVENT_DELETE), with the cached event. Intent: GUILD_SCHEDULED_EVENTS.
GuildScheduledEventUpdate
A scheduled event was updated (GUILD_SCHEDULED_EVENT_UPDATE), with the cached event before the update. Intent: GUILD_SCHEDULED_EVENTS.
GuildScheduledEventUserAdd
A user subscribed to a scheduled event (GUILD_SCHEDULED_EVENT_USER_ADD). Intent: GUILD_SCHEDULED_EVENTS.
GuildScheduledEventUserRemove
A user unsubscribed from a scheduled event (GUILD_SCHEDULED_EVENT_USER_REMOVE). Intent: GUILD_SCHEDULED_EVENTS.
GuildSoundboardSoundCreate
A soundboard sound was created (GUILD_SOUNDBOARD_SOUND_CREATE). Intent: GUILD_EXPRESSIONS.
GuildSoundboardSoundDelete
A soundboard sound was deleted (GUILD_SOUNDBOARD_SOUND_DELETE), with the cached sound. Intent: GUILD_EXPRESSIONS.
GuildSoundboardSoundUpdate
A soundboard sound was updated (GUILD_SOUNDBOARD_SOUND_UPDATE), with the cached sound before the update. Intent: GUILD_EXPRESSIONS.
GuildSoundboardSoundsUpdate
Several soundboard sounds were updated at once (GUILD_SOUNDBOARD_SOUNDS_UPDATE). Intent: GUILD_EXPRESSIONS.
GuildStickerCreate
A sticker was created, derived from GuildStickersUpdate with stickers in the cache.
GuildStickerDelete
A sticker was deleted, derived from GuildStickersUpdate with stickers in the cache.
GuildStickerUpdate
A sticker was updated, derived from GuildStickersUpdate with stickers in the cache.
GuildStickersUpdate
A guild’s stickers changed (GUILD_STICKERS_UPDATE): the full new list. Intent: GUILD_EXPRESSIONS.
GuildUnavailable
A guild became unavailable because of an outage (GUILD_DELETE with unavailable), with the cached guild; GuildAvailable follows when it is back. Intent: GUILDS.
GuildUpdate
A guild was updated (GUILD_UPDATE), with the cached guild before the update. Intent: GUILDS.
IntegrationCreate
An integration was created (INTEGRATION_CREATE). Intent: GUILD_INTEGRATIONS.
IntegrationDelete
An integration was deleted (INTEGRATION_DELETE). Intent: GUILD_INTEGRATIONS.
IntegrationUpdate
An integration was updated (INTEGRATION_UPDATE). Intent: GUILD_INTEGRATIONS.
InteractionCreate
A user used a command, a component or a modal (INTERACTION_CREATE).
Invalidated
Discord invalidated a shard’s session (INVALID_SESSION that cannot be resumed): the shard identifies again, and the events in between are lost.
InviteCreate
An invite was created (INVITE_CREATE). Intent: GUILD_INVITES.
InviteDelete
An invite was deleted (INVITE_DELETE). Intent: GUILD_INVITES.
MessageBulkDelete
Messages were deleted at once (MESSAGE_DELETE_BULK), with the cached ones. Intent: GUILD_MESSAGES.
MessageCreate
A message was sent (MESSAGE_CREATE). Intents: GUILD_MESSAGES, DIRECT_MESSAGES.
MessageDelete
A message was deleted (MESSAGE_DELETE), with the cached message. Intents: GUILD_MESSAGES, DIRECT_MESSAGES.
MessagePollVoteAdd
A user voted in a poll (MESSAGE_POLL_VOTE_ADD). Intents: GUILD_MESSAGE_POLLS, DIRECT_MESSAGE_POLLS.
MessagePollVoteRemove
A user removed a poll vote (MESSAGE_POLL_VOTE_REMOVE). Intents: GUILD_MESSAGE_POLLS, DIRECT_MESSAGE_POLLS.
MessageReactionAdd
A reaction was added (MESSAGE_REACTION_ADD). Intents: GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS.
MessageReactionRemove
A reaction was removed (MESSAGE_REACTION_REMOVE). Intents: GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS.
MessageReactionRemoveAll
All reactions were removed from a message (MESSAGE_REACTION_REMOVE_ALL). Intents: GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS.
MessageReactionRemoveEmoji
All reactions with one emoji were removed (MESSAGE_REACTION_REMOVE_EMOJI). Intents: GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS.
MessageUpdate
A message was edited (MESSAGE_UPDATE), with the cached message before the edit. Intents: GUILD_MESSAGES, DIRECT_MESSAGES.
PresenceUpdate
A user’s presence changed (PRESENCE_UPDATE), with the cached presence before the change. Intent: GUILD_PRESENCES (privileged).
RateLimited
A gateway command sent by the bot was rate limited (RATE_LIMITED), for example a RequestGuildMembers.
Raw
Every gateway dispatch, decoded, before the cache’s events and the derived ones.
ShardDisconnect
A shard was closed by Discord with a code that forbids reconnecting (an invalid token, disallowed intents, …); the Client stops and login returns the error.
ShardError
A shard hit an error it recovers from (a failed connection, a payload that could not be decoded, …); it reconnects or skips the payload by itself.
ShardEvent
An event delivered by Client::events: the shard it came from and the event.
ShardReady
A shard’s session is ready (READY): after connecting, and again after every re-identify (not after a resume, see ShardResume).
ShardReconnecting
A shard lost its connection and is reconnecting (it resumes when it can, otherwise it identifies again).
ShardResume
A shard resumed its session (RESUMED) after a reconnect; Discord replayed the events it missed, counted in ShardResume::replayed.
SoundboardSounds
A guild’s soundboard sounds (SOUNDBOARD_SOUNDS), the answer to a RequestSoundboardSounds command.
StageInstanceCreate
A stage instance was created (STAGE_INSTANCE_CREATE). Intent: GUILDS.
StageInstanceDelete
A stage instance was deleted (STAGE_INSTANCE_DELETE), with the cached instance. Intent: GUILDS.
StageInstanceUpdate
A stage instance was updated (STAGE_INSTANCE_UPDATE), with the cached instance before the update. Intent: GUILDS.
SubscriptionCreate
A premium subscription was created (SUBSCRIPTION_CREATE).
SubscriptionDelete
A premium subscription was deleted (SUBSCRIPTION_DELETE).
SubscriptionUpdate
A premium subscription was updated (SUBSCRIPTION_UPDATE).
ThreadCreate
A thread was created, or the bot was added to one (THREAD_CREATE). Intent: GUILDS.
ThreadDelete
A thread was deleted (THREAD_DELETE), with the cached thread. Intent: GUILDS.
ThreadListSync
The bot gained access to channels: their active threads (THREAD_LIST_SYNC). Intent: GUILDS.
ThreadMemberUpdate
The bot’s own thread member was updated (THREAD_MEMBER_UPDATE). Intent: GUILDS.
ThreadMembersUpdate
Members were added to or removed from a thread (THREAD_MEMBERS_UPDATE). Intents: GUILDS, and GUILD_MEMBERS for other users than the bot.
ThreadUpdate
A thread was updated (THREAD_UPDATE), with the cached thread before the update. Intent: GUILDS.
TypingStart
A user started typing (TYPING_START). Intents: GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING.
UserUpdate
The bot’s user was updated (USER_UPDATE), with the cached user before the update.
VoiceChannelEffectSend
A user sent an effect, such as an emoji, in a voice channel (VOICE_CHANNEL_EFFECT_SEND). Intent: GUILD_VOICE_STATES.
VoiceChannelStartTimeUpdate
A voice channel’s start time changed (VOICE_CHANNEL_START_TIME_UPDATE). Intent: GUILDS.
VoiceChannelStatusUpdate
A voice channel’s status was set or cleared (VOICE_CHANNEL_STATUS_UPDATE). Intent: GUILDS.
VoiceServerUpdate
The voice server of a guild the bot is connecting to (VOICE_SERVER_UPDATE).
VoiceStateUpdate
A user joined, left or moved between voice channels, or changed their voice state (VOICE_STATE_UPDATE), with the cached state before the change. Intent: GUILD_VOICE_STATES.
WebhooksUpdate
A channel’s webhooks changed (WEBHOOKS_UPDATE). Intent: GUILD_WEBHOOKS.

Enums§

Dispatch
An event as delivered by Client::events: one variant per event of Events, with the payload its handlers receive.

Traits§

Event
A typed event that can be passed to Client::on.