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-modelpayload unchanged, behind anArc(D-225). - Events whose discord.js counterpart hands an old or removed object (D-227) hand a type
from
payloadwith the state the cache held before the event inoldand Discord’s data innew, and dereference tonew(D-228).oldisNonewithout a cache, with the resource disabled inCacheConfig, or for something the cache never received. - Some events are derived by the client (D-223):
GuildAvailable/GuildUnavailable/GuildCreate/GuildDeleteare told apart with or without a cache; the per-emoji and per-sticker events need that resource in the cache. Rawhands every gateway dispatch as itsDispatchEventbefore 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 partial | What djs-rs hands instead | To complete it |
|---|---|---|
Partials.Message | MessageUpdate / MessageDelete with old: None (MessageUpdate still has the new message) | ctx.messages(channel).fetch(id) (not after a deletion) |
Partials.Channel | DM events arrive without a cached channel; the payloads carry channel_id | ctx.channels().fetch(id) |
Partials.Reaction | MessageReactionAdd / MessageReactionRemove carry the message, channel and user IDs | ctx.messages(channel).fetch(message) |
Partials.User | user IDs (user_id) in reaction, typing and poll events | ctx.users().fetch(id) |
Partials.GuildMember | GuildMemberUpdate / GuildMemberRemove with old: None (the new data or the user) | ctx.members(guild).fetch(user) |
Partials.ThreadMember | ThreadMemberUpdate / ThreadMembersUpdate payloads as Discord sends them | ctx.thread_members(thread).fetch(user) |
Partials.GuildScheduledEvent | GuildScheduledEventUserAdd / GuildScheduledEventUserRemove with IDs; update / delete with old: None | ctx.scheduled_events(guild).fetch(id) |
Partials.SoundboardSound | soundboard update / delete with old: None | ctx.soundboard_sounds(guild).fetch(id) |
Partials.Poll, Partials.PollAnswer | MessagePollVoteAdd / MessagePollVoteRemove with the message ID and answer ID | ctx.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§
- Application
Command Permissions Update - An application command’s permissions were updated (
APPLICATION_COMMAND_PERMISSIONS_UPDATE). - Auto
Moderation Action Execution - An auto moderation rule acted on a message or member
(
AUTO_MODERATION_ACTION_EXECUTION). Intent:AUTO_MODERATION_EXECUTION. - Auto
Moderation Rule Create - An auto moderation rule was created (
AUTO_MODERATION_RULE_CREATE). Intent:AUTO_MODERATION_CONFIGURATION. - Auto
Moderation Rule Delete - An auto moderation rule was deleted (
AUTO_MODERATION_RULE_DELETE). Intent:AUTO_MODERATION_CONFIGURATION. - Auto
Moderation Rule Update - An auto moderation rule was updated (
AUTO_MODERATION_RULE_UPDATE). Intent:AUTO_MODERATION_CONFIGURATION. - Channel
Create - A guild channel was created (
CHANNEL_CREATE). Intent:GUILDS. - Channel
Delete - A channel was deleted (
CHANNEL_DELETE), with the cached channel. Intent:GUILDS. - Channel
Info - The ephemeral data of a guild’s channels (
CHANNEL_INFO), the answer to aRequestChannelInfocommand. - Channel
Pins Update - A message was pinned or unpinned (
CHANNEL_PINS_UPDATE). Intents:GUILDS,DIRECT_MESSAGES. - Channel
Update - A channel was updated (
CHANNEL_UPDATE), with the cached channel before the update. Intent:GUILDS. - Client
Ready - The client is ready: fired once per
login, at theREADYof the last of its shards to become ready (D-224). - Entitlement
Create - A user subscribed to a SKU (
ENTITLEMENT_CREATE). - Entitlement
Delete - An entitlement was deleted (
ENTITLEMENT_DELETE). - Entitlement
Update - An entitlement was updated, for example renewed (
ENTITLEMENT_UPDATE). - Events
- The events a
Clientcan emit, as typed values (D-222). - Guild
Audit LogEntry Create - An audit log entry was created (
GUILD_AUDIT_LOG_ENTRY_CREATE). Intent:GUILD_MODERATION, and the bot needs theVIEW_AUDIT_LOGpermission. - Guild
Available - A guild became available (
GUILD_CREATEof a guild fromREADYor back from an outage). Intent:GUILDS. - Guild
BanAdd - A user was banned (
GUILD_BAN_ADD). Intent:GUILD_MODERATION. - Guild
BanRemove - A user was unbanned (
GUILD_BAN_REMOVE). Intent:GUILD_MODERATION. - Guild
Create - The bot joined a guild (
GUILD_CREATEof a guild it was not waiting for). Intent:GUILDS. - Guild
Delete - The bot left a guild, was removed, or the guild was deleted (
GUILD_DELETEwithoutunavailable), with the cached guild. Intent:GUILDS. - Guild
Emoji Create - An emoji was created, derived from
GuildEmojisUpdatewith emojis in the cache. - Guild
Emoji Delete - An emoji was deleted, derived from
GuildEmojisUpdatewith emojis in the cache. - Guild
Emoji Update - An emoji was updated, derived from
GuildEmojisUpdatewith emojis in the cache. - Guild
Emojis Update - A guild’s emojis changed (
GUILD_EMOJIS_UPDATE): the full new list. Intent:GUILD_EXPRESSIONS. - Guild
Integrations Update - A guild’s integrations changed (
GUILD_INTEGRATIONS_UPDATE). Intent:GUILD_INTEGRATIONS. - Guild
Member Add - A user joined a guild (
GUILD_MEMBER_ADD). Intent:GUILD_MEMBERS(privileged). - Guild
Member Remove - A user left or was removed from a guild (
GUILD_MEMBER_REMOVE), with the cached member. Intent:GUILD_MEMBERS(privileged). - Guild
Member Update - A member was updated (
GUILD_MEMBER_UPDATE), with the cached member before the update. Intent:GUILD_MEMBERS(privileged). - Guild
Members Chunk - A chunk of a guild’s members (
GUILD_MEMBERS_CHUNK), the answer to aRequestGuildMemberscommand. - Guild
Role Create - A role was created (
GUILD_ROLE_CREATE). Intent:GUILDS. - Guild
Role Delete - A role was deleted (
GUILD_ROLE_DELETE), with the cached role. Intent:GUILDS. - Guild
Role Update - A role was updated (
GUILD_ROLE_UPDATE), with the cached role before the update. Intent:GUILDS. - Guild
Scheduled Event Create - A scheduled event was created (
GUILD_SCHEDULED_EVENT_CREATE). Intent:GUILD_SCHEDULED_EVENTS. - Guild
Scheduled Event Delete - A scheduled event was deleted (
GUILD_SCHEDULED_EVENT_DELETE), with the cached event. Intent:GUILD_SCHEDULED_EVENTS. - Guild
Scheduled Event Update - A scheduled event was updated (
GUILD_SCHEDULED_EVENT_UPDATE), with the cached event before the update. Intent:GUILD_SCHEDULED_EVENTS. - Guild
Scheduled Event User Add - A user subscribed to a scheduled event (
GUILD_SCHEDULED_EVENT_USER_ADD). Intent:GUILD_SCHEDULED_EVENTS. - Guild
Scheduled Event User Remove - A user unsubscribed from a scheduled event (
GUILD_SCHEDULED_EVENT_USER_REMOVE). Intent:GUILD_SCHEDULED_EVENTS. - Guild
Soundboard Sound Create - A soundboard sound was created (
GUILD_SOUNDBOARD_SOUND_CREATE). Intent:GUILD_EXPRESSIONS. - Guild
Soundboard Sound Delete - A soundboard sound was deleted (
GUILD_SOUNDBOARD_SOUND_DELETE), with the cached sound. Intent:GUILD_EXPRESSIONS. - Guild
Soundboard Sound Update - A soundboard sound was updated (
GUILD_SOUNDBOARD_SOUND_UPDATE), with the cached sound before the update. Intent:GUILD_EXPRESSIONS. - Guild
Soundboard Sounds Update - Several soundboard sounds were updated at once (
GUILD_SOUNDBOARD_SOUNDS_UPDATE). Intent:GUILD_EXPRESSIONS. - Guild
Sticker Create - A sticker was created, derived from
GuildStickersUpdatewith stickers in the cache. - Guild
Sticker Delete - A sticker was deleted, derived from
GuildStickersUpdatewith stickers in the cache. - Guild
Sticker Update - A sticker was updated, derived from
GuildStickersUpdatewith stickers in the cache. - Guild
Stickers Update - A guild’s stickers changed (
GUILD_STICKERS_UPDATE): the full new list. Intent:GUILD_EXPRESSIONS. - Guild
Unavailable - A guild became unavailable because of an outage (
GUILD_DELETEwithunavailable), with the cached guild;GuildAvailablefollows when it is back. Intent:GUILDS. - Guild
Update - A guild was updated (
GUILD_UPDATE), with the cached guild before the update. Intent:GUILDS. - Integration
Create - An integration was created (
INTEGRATION_CREATE). Intent:GUILD_INTEGRATIONS. - Integration
Delete - An integration was deleted (
INTEGRATION_DELETE). Intent:GUILD_INTEGRATIONS. - Integration
Update - An integration was updated (
INTEGRATION_UPDATE). Intent:GUILD_INTEGRATIONS. - Interaction
Create - A user used a command, a component or a modal (
INTERACTION_CREATE). - Invalidated
- Discord invalidated a shard’s session (
INVALID_SESSIONthat cannot be resumed): the shard identifies again, and the events in between are lost. - Invite
Create - An invite was created (
INVITE_CREATE). Intent:GUILD_INVITES. - Invite
Delete - An invite was deleted (
INVITE_DELETE). Intent:GUILD_INVITES. - Message
Bulk Delete - Messages were deleted at once (
MESSAGE_DELETE_BULK), with the cached ones. Intent:GUILD_MESSAGES. - Message
Create - A message was sent (
MESSAGE_CREATE). Intents:GUILD_MESSAGES,DIRECT_MESSAGES. - Message
Delete - A message was deleted (
MESSAGE_DELETE), with the cached message. Intents:GUILD_MESSAGES,DIRECT_MESSAGES. - Message
Poll Vote Add - A user voted in a poll (
MESSAGE_POLL_VOTE_ADD). Intents:GUILD_MESSAGE_POLLS,DIRECT_MESSAGE_POLLS. - Message
Poll Vote Remove - A user removed a poll vote (
MESSAGE_POLL_VOTE_REMOVE). Intents:GUILD_MESSAGE_POLLS,DIRECT_MESSAGE_POLLS. - Message
Reaction Add - A reaction was added (
MESSAGE_REACTION_ADD). Intents:GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGE_REACTIONS. - Message
Reaction Remove - A reaction was removed (
MESSAGE_REACTION_REMOVE). Intents:GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGE_REACTIONS. - Message
Reaction Remove All - All reactions were removed from a message (
MESSAGE_REACTION_REMOVE_ALL). Intents:GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGE_REACTIONS. - Message
Reaction Remove Emoji - All reactions with one emoji were removed (
MESSAGE_REACTION_REMOVE_EMOJI). Intents:GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGE_REACTIONS. - Message
Update - A message was edited (
MESSAGE_UPDATE), with the cached message before the edit. Intents:GUILD_MESSAGES,DIRECT_MESSAGES. - Presence
Update - A user’s presence changed (
PRESENCE_UPDATE), with the cached presence before the change. Intent:GUILD_PRESENCES(privileged). - Rate
Limited - A gateway command sent by the bot was rate limited (
RATE_LIMITED), for example aRequestGuildMembers. - Raw
- Every gateway dispatch, decoded, before the cache’s events and the derived ones.
- Shard
Disconnect - A shard was closed by Discord with a code that forbids reconnecting (an invalid token,
disallowed intents, …); the
Clientstops andloginreturns the error. - Shard
Error - 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.
- Shard
Event - An event delivered by
Client::events: the shard it came from and the event. - Shard
Ready - A shard’s session is ready (
READY): after connecting, and again after every re-identify (not after a resume, seeShardResume). - Shard
Reconnecting - A shard lost its connection and is reconnecting (it resumes when it can, otherwise it identifies again).
- Shard
Resume - A shard resumed its session (
RESUMED) after a reconnect; Discord replayed the events it missed, counted inShardResume::replayed. - Soundboard
Sounds - A guild’s soundboard sounds (
SOUNDBOARD_SOUNDS), the answer to aRequestSoundboardSoundscommand. - Stage
Instance Create - A stage instance was created (
STAGE_INSTANCE_CREATE). Intent:GUILDS. - Stage
Instance Delete - A stage instance was deleted (
STAGE_INSTANCE_DELETE), with the cached instance. Intent:GUILDS. - Stage
Instance Update - A stage instance was updated (
STAGE_INSTANCE_UPDATE), with the cached instance before the update. Intent:GUILDS. - Subscription
Create - A premium subscription was created (
SUBSCRIPTION_CREATE). - Subscription
Delete - A premium subscription was deleted (
SUBSCRIPTION_DELETE). - Subscription
Update - A premium subscription was updated (
SUBSCRIPTION_UPDATE). - Thread
Create - A thread was created, or the bot was added to one (
THREAD_CREATE). Intent:GUILDS. - Thread
Delete - A thread was deleted (
THREAD_DELETE), with the cached thread. Intent:GUILDS. - Thread
List Sync - The bot gained access to channels: their active threads (
THREAD_LIST_SYNC). Intent:GUILDS. - Thread
Member Update - The bot’s own thread member was updated (
THREAD_MEMBER_UPDATE). Intent:GUILDS. - Thread
Members Update - Members were added to or removed from a thread (
THREAD_MEMBERS_UPDATE). Intents:GUILDS, andGUILD_MEMBERSfor other users than the bot. - Thread
Update - A thread was updated (
THREAD_UPDATE), with the cached thread before the update. Intent:GUILDS. - Typing
Start - A user started typing (
TYPING_START). Intents:GUILD_MESSAGE_TYPING,DIRECT_MESSAGE_TYPING. - User
Update - The bot’s user was updated (
USER_UPDATE), with the cached user before the update. - Voice
Channel Effect Send - A user sent an effect, such as an emoji, in a voice channel
(
VOICE_CHANNEL_EFFECT_SEND). Intent:GUILD_VOICE_STATES. - Voice
Channel Start Time Update - A voice channel’s start time changed (
VOICE_CHANNEL_START_TIME_UPDATE). Intent:GUILDS. - Voice
Channel Status Update - A voice channel’s status was set or cleared (
VOICE_CHANNEL_STATUS_UPDATE). Intent:GUILDS. - Voice
Server Update - The voice server of a guild the bot is connecting to (
VOICE_SERVER_UPDATE). - Voice
State Update - 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. - Webhooks
Update - A channel’s webhooks changed (
WEBHOOKS_UPDATE). Intent:GUILD_WEBHOOKS.
Enums§
- Dispatch
- An event as delivered by
Client::events: one variant per event ofEvents, with the payload its handlers receive.
Traits§
- Event
- A typed event that can be passed to
Client::on.