zaun_bot/bot.rs
1//! The [`Bot`] trait.
2
3use crate::types::{
4 ApplicationCommand, Channel, Choice, CommandData, CommunityProfile, ComponentData, Event, Id,
5 Member, MemberRoles, MemberTimeout, Message, ModalData, Presence, RawEvent, Reaction, Ready,
6 Role, Typing,
7};
8use crate::{Context, EventContext};
9
10/// The bot's configuration: the keys its manifest declares in `[config]`, each with the
11/// value the owner set or else its default. Keys with neither are absent. Given to
12/// [`Bot::on_start`] and [`Bot::commands`] at start and to [`Bot::on_config`] on a change.
13#[derive(Debug, Clone, Default, PartialEq, Eq)]
14pub struct Config(pub Vec<(String, String)>);
15
16impl Config {
17 /// The value of `key`, if set (by the owner, or the manifest's default).
18 #[must_use]
19 pub fn get(&self, key: &str) -> Option<&str> {
20 self.0
21 .iter()
22 .find(|(k, _)| k == key)
23 .map(|(_, v)| v.as_str())
24 }
25
26 /// The value of `key`, or `default` when it is not set.
27 #[must_use]
28 pub fn get_or<'a>(&'a self, key: &str, default: &'a str) -> &'a str {
29 self.get(key).unwrap_or(default)
30 }
31}
32
33/// A Zaun bot. Every handler has a no-op default; implement the ones you need.
34///
35/// The host calls one handler at a time, so `&mut self` state is safe. State lives only as
36/// long as the instance; use [`crate::storage`] for anything that must survive a restart.
37///
38/// Event handlers run only for events the manifest's `events` asks for (wire types such as
39/// `message.created`, groups such as `messages`, `members`, `dms`, or `*`); see
40/// `docs/events.md`. `on_ready` always runs first.
41#[allow(unused_variables)]
42pub trait Bot: Default + 'static {
43 /// The bot started (each instance start, before [`Bot::commands`]): a good place to set
44 /// its presence ([`crate::presence`]).
45 fn on_start(&mut self, config: &Config) {
46 let _ = config;
47 }
48
49 /// The owner changed the bot's configuration while it runs: every value again, as at
50 /// start. The bot is not restarted; refresh whatever it derived from the old values.
51 fn on_config(&mut self, config: &Config) {}
52
53 /// The application commands to register when the bot starts.
54 fn commands(&mut self, config: &Config) -> Vec<ApplicationCommand> {
55 Vec::new()
56 }
57
58 /// A slash, user or message command was used. Answer within 3 s (or call
59 /// [`Context::defer`]); the runtime defers automatically at 2.5 s.
60 fn on_command(&mut self, ctx: Context, command: CommandData) {}
61
62 /// A button was clicked or a select changed on one of the bot's messages.
63 fn on_component(&mut self, ctx: Context, component: ComponentData) {}
64
65 /// A modal the bot opened was submitted.
66 fn on_modal(&mut self, ctx: Context, modal: ModalData) {}
67
68 /// The invoker is typing in an option with `autocomplete`. Return up to 25 choices.
69 fn on_autocomplete(&mut self, ctx: Context, command: CommandData) -> Vec<Choice> {
70 Vec::new()
71 }
72
73 /// A timer from [`crate::timers`] fired.
74 fn on_timer(&mut self, id: u64, tag: String) {}
75
76 /// Every event; the default calls the typed handlers below. Override it to see events
77 /// raw, before (or instead of) them.
78 fn on_event(&mut self, ctx: EventContext, event: Event) {
79 crate::events::dispatch(self, ctx, event);
80 }
81
82 /// The bot's connection is up: its ids and community. Always the first event.
83 fn on_ready(&mut self, ctx: EventContext, me: Ready) {}
84 /// A message was posted (`messages`). `is_dm`: in a direct message with the bot.
85 fn on_message_create(&mut self, ctx: EventContext, message: Message, is_dm: bool) {}
86 /// A message was edited (`messages`).
87 fn on_message_update(&mut self, ctx: EventContext, message: Message, is_dm: bool) {}
88 /// A message was deleted (`messages`).
89 fn on_message_delete(&mut self, ctx: EventContext, channel_id: Id, message_id: Id) {}
90 /// Messages were deleted together, by a ban (`messages`).
91 fn on_message_delete_bulk(&mut self, ctx: EventContext, channel_id: Id, ids: Vec<Id>) {}
92 /// A reaction was added (`reactions`).
93 fn on_reaction_add(&mut self, ctx: EventContext, reaction: Reaction) {}
94 /// A reaction was removed (`reactions`).
95 fn on_reaction_remove(&mut self, ctx: EventContext, reaction: Reaction) {}
96 /// A member joined (`members`, capability `members`).
97 fn on_member_join(&mut self, ctx: EventContext, member: Member) {}
98 /// A member changed their name (`members`).
99 fn on_member_update(&mut self, ctx: EventContext, member: Member) {}
100 /// A member left, was kicked or banned (`members`).
101 fn on_member_leave(&mut self, ctx: EventContext, member_id: Id) {}
102 /// The bot itself was removed (`kicked`, `banned`); it stops right after.
103 fn on_removed(&mut self, ctx: EventContext, reason: String) {}
104 /// A member's roles changed (`members`).
105 fn on_member_roles_update(&mut self, ctx: EventContext, roles: MemberRoles) {}
106 /// A member was timed out, or their timeout cleared (`members`).
107 fn on_member_timeout(&mut self, ctx: EventContext, timeout: MemberTimeout) {}
108 /// A channel the bot can see was created (`channels`).
109 fn on_channel_create(&mut self, ctx: EventContext, channel: Channel) {}
110 /// A channel changed (`channels`).
111 fn on_channel_update(&mut self, ctx: EventContext, channel: Channel) {}
112 /// A channel was deleted, or the bot lost access (`channels`).
113 fn on_channel_delete(&mut self, ctx: EventContext, channel_id: Id) {}
114 /// A role was created (`roles`).
115 fn on_role_create(&mut self, ctx: EventContext, role: Role) {}
116 /// A role changed (`roles`).
117 fn on_role_update(&mut self, ctx: EventContext, role: Role) {}
118 /// A role was deleted (`roles`).
119 fn on_role_delete(&mut self, ctx: EventContext, role_id: Id) {}
120 /// Members are typing in a channel the bot can see (`typing`).
121 fn on_typing_start(&mut self, ctx: EventContext, typing: Typing) {}
122 /// A member's presence changed (`presence`, capability `presence`).
123 fn on_presence_update(&mut self, ctx: EventContext, presence: Presence) {}
124 /// The community's settings changed (`community`).
125 fn on_community_update(&mut self, ctx: EventContext, profile: CommunityProfile) {}
126 /// A bot's command list changed (`commands`); its bot id.
127 fn on_commands_update(&mut self, ctx: EventContext, bot_id: Id) {}
128 /// A DM with the bot was opened, by the bot or a member (`dms`, capability `dms`).
129 fn on_dm_open(&mut self, ctx: EventContext, channel: Channel) {}
130 /// The bot closed a DM (`dms`).
131 fn on_dm_close(&mut self, ctx: EventContext, channel_id: Id) {}
132 /// An event without a typed handler: its wire type and JSON body.
133 fn on_raw(&mut self, ctx: EventContext, event: RawEvent) {}
134
135 /// Something happened in the bot's voice stay or player ([`crate::Voice`]): a track
136 /// started, ended or failed, someone joined or left, or the stay ended.
137 fn on_voice_event(&mut self, event: crate::voice::VoiceEvent) {}
138}