1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::{
    BoxError,
    BuilderError,
    CheckFn,
    Command,
    HelpCommand,
};
use serenity::{
    builder::{
        CreateCommand,
        CreateInteractionResponse,
        CreateInteractionResponseMessage,
    },
    client::Context,
    model::{
        application::{
            Command as ApplicationCommand,
            CommandInteraction,
            Interaction,
        },
        prelude::GuildId,
    },
};
use std::{
    collections::HashMap,
    sync::Arc,
};
use tracing::{
    info,
    warn,
};

/// A wrapper for [`BoxError`] that impls error
struct WrapBoxError(BoxError);

impl WrapBoxError {
    /// Make a new [`WrapBoxError`] from an error
    fn new(e: BoxError) -> Self {
        Self(e)
    }
}

impl std::fmt::Debug for WrapBoxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::fmt::Display for WrapBoxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::error::Error for WrapBoxError {}

struct FmtOptionsHelper<'a>(&'a CommandInteraction);

impl std::fmt::Display for FmtOptionsHelper<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        let len = self.0.data.options.len();
        for (i, option) in self.0.data.options.iter().enumerate() {
            if i + 1 == len {
                write!(f, "'{}'={:?}", option.name, option.value)?;
            }
        }
        write!(f, "]")?;

        Ok(())
    }
}

/// A framework
#[derive(Clone)]
pub struct Framework {
    commands: Arc<HashMap<Box<str>, Command>>,
    help_command: Option<Arc<HelpCommand>>,
    checks: Arc<[CheckFn]>,
}

impl Framework {
    /// Register the framework.
    ///
    /// `test_guild_id` is an optional guild where the commands will be registered as guild commands,
    /// so they update faster for testing purposes.
    pub async fn register(
        &self,
        ctx: Context,
        test_guild_id: Option<GuildId>,
    ) -> Result<(), serenity::Error> {
        for framework_command in self.commands.values() {
            let mut command_builder = CreateCommand::new(framework_command.name());
            command_builder = framework_command.register(command_builder);
            ApplicationCommand::create_global_command(&ctx.http, command_builder).await?;
        }

        if let Some(framework_command) = self.help_command.as_deref() {
            let mut command_builder = CreateCommand::new("help");
            command_builder = framework_command.register(command_builder);
            ApplicationCommand::create_global_command(&ctx.http, command_builder).await?;
        }

        if let Some(guild_id) = test_guild_id {
            let mut create_commands = Vec::new();
            for framework_command in self.commands.values() {
                let mut command_builder = CreateCommand::new(framework_command.name());
                command_builder = framework_command.register(command_builder);
                create_commands.push(command_builder);
            }
            if let Some(framework_command) = self.help_command.as_deref() {
                let mut command_builder = CreateCommand::new("help");
                command_builder = framework_command.register(command_builder);
                create_commands.push(command_builder);
            }

            GuildId::set_commands(guild_id, &ctx.http, create_commands).await?;
        }

        Ok(())
    }

    /// Process an interaction create event
    pub async fn process_interaction_create(&self, ctx: Context, interaction: Interaction) {
        if let Interaction::Command(command) = interaction {
            self.process_interaction_create_application_command(ctx, command)
                .await
        }
    }

    #[tracing::instrument(skip(self, ctx, command), fields(id = %command.id, author = %command.user.id, guild = ?command.guild_id, channel_id = %command.channel_id))]
    async fn process_interaction_create_application_command(
        &self,
        ctx: Context,
        command: CommandInteraction,
    ) {
        if command.data.name.as_str() == "help" {
            // Keep comments
            #[allow(clippy::single_match)]
            match self.help_command.as_ref() {
                Some(framework_command) => {
                    info!(
                        "processing help command, options={}",
                        FmtOptionsHelper(&command)
                    );
                    if let Err(error) = framework_command
                        .fire_on_process(ctx, command, self.commands.clone())
                        .await
                        .map_err(WrapBoxError::new)
                    {
                        // TODO: handle error with handler
                        warn!("{error}");
                    }
                }
                None => {
                    // Don't log, as we assume the user does not want to provide help.
                    // Logging would be extra noise.
                }
            }

            return;
        }

        let framework_command = match self.commands.get(command.data.name.as_str()) {
            Some(command) => command,
            None => {
                // TODO: Unknown command handler
                let command_name = command.data.name.as_str();
                warn!("unknown command \"{command_name}\"");
                return;
            }
        };

        // TODO: Consider making parallel
        let mut check_result = Ok(());
        for check in self.checks.iter().chain(framework_command.checks().iter()) {
            check_result = check_result.and(check(&ctx, &command, framework_command).await);
        }

        match check_result {
            Ok(()) => {
                let command_name = framework_command.name();
                info!(
                    "processing command \"{command_name}\", options={}",
                    FmtOptionsHelper(&command)
                );
                if let Err(error) = framework_command
                    .fire_on_process(ctx, command)
                    .await
                    .map_err(WrapBoxError::new)
                {
                    // TODO: handle error with handler
                    warn!("{error}");
                }
            }
            Err(error) => {
                let content = if let Some(user) = error.user.as_deref() {
                    user
                } else {
                    "check failed for unknown reason"
                };

                if let Some(log) = error.log {
                    warn!("{log}");
                }

                let response = CreateInteractionResponseMessage::new().content(content);
                if let Err(error) = command
                    .create_response(&ctx.http, CreateInteractionResponse::Message(response))
                    .await
                {
                    warn!("{error}");
                }
            }
        }
    }
}

impl std::fmt::Debug for Framework {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Framework")
            .field("commands", &self.commands)
            .finish()
    }
}

/// A FrameworkBuilder for slash commands.
pub struct FrameworkBuilder {
    commands: HashMap<Box<str>, Command>,
    help_command: Option<HelpCommand>,
    checks: Vec<CheckFn>,

    error: Option<BuilderError>,
}

impl FrameworkBuilder {
    /// Make a new [`FrameworkBuilder`].
    pub fn new() -> Self {
        Self {
            commands: HashMap::new(),
            help_command: None,
            checks: Vec::new(),

            error: None,
        }
    }

    /// Add a command
    pub fn command(&mut self, command: Command) -> &mut Self {
        if self.error.is_some() {
            return self;
        }

        let command_name: Box<str> = command.name().into();

        // A help command cannot be registered like this
        if &*command_name == "help" {
            self.error = Some(BuilderError::Duplicate(command_name));
            return self;
        }

        // Don't overwrite commands
        if self.commands.get(&command_name).is_some() {
            self.error = Some(BuilderError::Duplicate(command_name));
            return self;
        }

        self.commands.insert(command_name, command);

        self
    }

    /// Add a help command
    pub fn help_command(&mut self, command: HelpCommand) -> &mut Self {
        if self.error.is_some() {
            return self;
        }

        // Don't overwrite commands
        if self.help_command.is_some() {
            self.error = Some(BuilderError::Duplicate("help".into()));
            return self;
        }

        self.help_command = Some(command);

        self
    }

    /// Add a check
    pub fn check(&mut self, check: CheckFn) -> &mut Self {
        if self.error.is_some() {
            return self;
        }

        self.checks.push(check);
        self
    }

    /// Build a framework
    pub fn build(&mut self) -> Result<Framework, BuilderError> {
        if let Some(error) = self.error.take() {
            return Err(error);
        }

        Ok(Framework {
            commands: Arc::new(std::mem::take(&mut self.commands)),
            help_command: self.help_command.take().map(Arc::new),

            checks: std::mem::take(&mut self.checks).into(),
        })
    }
}

impl std::fmt::Debug for FrameworkBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FrameworkBuilder")
            .field("commands", &self.commands)
            .finish()
    }
}

impl Default for FrameworkBuilder {
    fn default() -> Self {
        Self::new()
    }
}