Skip to main content

fluxer/
event.rs

1//! The event handler trait for reacting to gateway events.
2//!
3//! Implement [`EventHandler`] on your own struct and pass it to the client builder.
4//! Every method has a default no-op implementation so you only need to override
5//! the ones you care about.
6//!
7//! Events are dispatched concurrently -- each one runs in its own spawned task.
8//! Because of this, the trait requires `Send + Sync`. If you need shared mutable
9//! state in your handler, wrap it in `Arc<Mutex<T>>`.
10
11use async_trait::async_trait;
12use crate::client::Context;
13use crate::model::*;
14
15/// Trait for handling gateway events. Implement the methods you need, ignore the rest.
16///
17/// ```rust,no_run
18/// use fluxer::prelude::*;
19/// use async_trait::async_trait;
20///
21/// struct Bot;
22///
23/// #[async_trait]
24/// impl EventHandler for Bot {
25///     async fn on_message(&self, ctx: Context, msg: Message) {
26///         if msg.content.as_deref() == Some("!hello") {
27///             let ch = msg.channel_id.as_deref().unwrap_or_default();
28///             let _ = ctx.http.send_message(ch, "Hello!").await;
29///         }
30///     }
31/// }
32/// ```
33#[async_trait]
34pub trait EventHandler: Send + Sync {
35    /// The bot is connected and ready. [`Ready`] contains the bot user, session ID,
36    /// and initial guild list.
37    async fn on_ready(&self, _ctx: Context, _ready: Ready) {}
38
39    async fn on_message(&self, _ctx: Context, _msg: Message) {}
40
41    /// Only the changed fields are populated in [`MessageUpdate`].
42    async fn on_message_update(&self, _ctx: Context, _update: MessageUpdate) {}
43
44    /// You only get the ID, not the content.
45    async fn on_message_delete(&self, _ctx: Context, _delete: MessageDelete) {}
46
47    async fn on_message_delete_bulk(&self, _ctx: Context, _delete: MessageDeleteBulk) {}
48
49    async fn on_reaction_add(&self, _ctx: Context, _reaction: ReactionAdd) {}
50
51    async fn on_reaction_remove(&self, _ctx: Context, _reaction: ReactionRemove) {}
52
53    async fn on_reaction_remove_all(&self, _ctx: Context, _event: ReactionRemoveAll) {}
54
55    async fn on_reaction_remove_emoji(&self, _ctx: Context, _event: ReactionRemoveEmoji) {}
56
57    async fn on_typing_start(&self, _ctx: Context, _event: TypingStart) {}
58
59    async fn on_channel_create(&self, _ctx: Context, _channel: Channel) {}
60
61    async fn on_channel_update(&self, _ctx: Context, _channel: Channel) {}
62
63    async fn on_channel_delete(&self, _ctx: Context, _channel: Channel) {}
64
65    async fn on_channel_pins_update(&self, _ctx: Context, _event: ChannelPinsUpdate) {}
66
67    /// Fired when the bot joins a guild or when a guild becomes available after an outage.
68    async fn on_guild_create(&self, _ctx: Context, _guild: Guild) {}
69
70    async fn on_guild_update(&self, _ctx: Context, _guild: Guild) {}
71
72    /// The bot was removed from the guild, or the guild went unavailable.
73    async fn on_guild_delete(&self, _ctx: Context, _guild: UnavailableGuild) {}
74
75    async fn on_guild_member_add(&self, _ctx: Context, _event: GuildMemberAdd) {}
76
77    async fn on_guild_member_update(&self, _ctx: Context, _event: GuildMemberUpdate) {}
78
79    async fn on_guild_member_remove(&self, _ctx: Context, _event: GuildMemberRemove) {}
80
81    async fn on_guild_ban_add(&self, _ctx: Context, _event: GuildBanAdd) {}
82
83    async fn on_guild_ban_remove(&self, _ctx: Context, _event: GuildBanRemove) {}
84
85    async fn on_guild_role_create(&self, _ctx: Context, _event: GuildRoleCreate) {}
86
87    async fn on_guild_role_update(&self, _ctx: Context, _event: GuildRoleUpdate) {}
88
89    async fn on_guild_role_delete(&self, _ctx: Context, _event: GuildRoleDelete) {}
90
91    async fn on_guild_emojis_update(&self, _ctx: Context, _event: GuildEmojisUpdate) {}
92
93    async fn on_guild_stickers_update(&self, _ctx: Context, _event: GuildStickersUpdate) {}
94
95    async fn on_guild_role_update_bulk(&self, _ctx: Context, _event: GuildRoleUpdateBulk) {}
96
97    async fn on_channel_update_bulk(&self, _ctx: Context, _event: ChannelUpdateBulk) {}
98
99    async fn on_invite_create(&self, _ctx: Context, _event: InviteCreate) {}
100
101    async fn on_invite_delete(&self, _ctx: Context, _event: InviteDelete) {}
102
103    async fn on_webhooks_update(&self, _ctx: Context, _event: WebhooksUpdate) {}
104}