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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use crate::{
    ArgumentParam,
    BoxError,
    BoxFuture,
    BuilderError,
    CheckFn,
    DataType,
    FromOptions,
};
use serenity::{
    builder::{
        CreateCommand,
        CreateCommandOption,
    },
    client::Context,
    model::application::{
        CommandInteraction,
        CommandOptionType,
    },
};
use std::{
    collections::HashMap,
    future::Future,
    sync::Arc,
};

type OnProcessResult = Result<(), BoxError>;
pub type OnProcessFuture = BoxFuture<'static, OnProcessResult>;

// Keep these types in sync.
type OnProcessFutureFn = Box<dyn Fn(Context, CommandInteraction) -> OnProcessFuture + Send + Sync>;
type OnProcessFutureFnPtr<F, A> = fn(Context, CommandInteraction, A) -> F;

type HelpOnProcessFutureFn = Box<
    dyn Fn(Context, CommandInteraction, Arc<HashMap<Box<str>, Command>>) -> OnProcessFuture
        + Send
        + Sync,
>;
type HelpOnProcessFutureFnPtr<F, A> =
    fn(Context, CommandInteraction, Arc<HashMap<Box<str>, Command>>, A) -> F;

/// A slash framework command
pub struct Command {
    /// The name of the command
    name: Box<str>,

    /// Description
    description: Box<str>,

    /// Arguments
    arguments: Box<[ArgumentParam]>,

    /// The main "process" func
    on_process: OnProcessFutureFn,

    /// Checks that must pass before this command is run
    checks: Vec<CheckFn>,
}

impl Command {
    /// Get the command name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the command description
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Get the command arguments
    pub fn arguments(&self) -> &[ArgumentParam] {
        &self.arguments
    }

    /// Fire the on_process hook
    pub async fn fire_on_process(
        &self,
        ctx: Context,
        interaction: CommandInteraction,
    ) -> Result<(), BoxError> {
        (self.on_process)(ctx, interaction).await
    }

    /// Get the inner checks
    pub fn checks(&self) -> &[CheckFn] {
        &self.checks
    }

    /// Register this command
    pub fn register(&self, mut command: CreateCommand) -> CreateCommand {
        command = command.name(self.name()).description(self.description());

        for argument in self.arguments().iter() {
            let option_kind = match argument.kind() {
                DataType::Boolean => CommandOptionType::Boolean,
                DataType::String => CommandOptionType::String,
                DataType::Integer => CommandOptionType::Integer,
            };
            let option =
                CreateCommandOption::new(option_kind, argument.name(), argument.description())
                    .required(argument.required());
            command = command.add_option(option);
        }

        command
    }
}

impl std::fmt::Debug for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Command")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("arguments", &self.arguments)
            .field("on_process", &"<func>")
            .finish()
    }
}

/// A builder for a [`Command`].
pub struct CommandBuilder<'a, 'b> {
    name: Option<&'a str>,
    description: Option<&'b str>,
    arguments: Vec<ArgumentParam>,

    on_process: Option<OnProcessFutureFn>,
    checks: Vec<CheckFn>,
}

impl<'a, 'b> CommandBuilder<'a, 'b> {
    /// Make a new [`CommandBuilder`].
    pub fn new() -> Self {
        Self {
            name: None,
            description: None,
            arguments: Vec::new(),

            on_process: None,
            checks: Vec::new(),
        }
    }

    /// The command name
    pub fn name(&mut self, name: &'a str) -> &mut Self {
        self.name = Some(name);
        self
    }

    /// The command description
    pub fn description(&mut self, description: &'b str) -> &mut Self {
        self.description = Some(description);
        self
    }

    /// Add an argument
    pub fn argument(&mut self, argument: ArgumentParam) -> &mut Self {
        self.arguments.push(argument);
        self
    }

    /// Add many arguments
    pub fn arguments(&mut self, arguments: impl Iterator<Item = ArgumentParam>) -> &mut Self {
        for argument in arguments {
            self.argument(argument);
        }
        self
    }

    /// The on_process hook
    pub fn on_process<F, A>(&mut self, on_process: OnProcessFutureFnPtr<F, A>) -> &mut Self
    where
        F: Future<Output = Result<(), BoxError>> + Send + 'static,
        A: FromOptions + 'static,
    {
        // Trampoline so user does not have to box manually and parse their args manually
        self.on_process = Some(Box::new(move |ctx, interaction| {
            Box::pin(async move {
                let args = A::from_options(&interaction)?;
                (on_process)(ctx, interaction, args).await
            })
        }));

        self
    }

    /// Add a check to this specific command
    pub fn check(&mut self, check: CheckFn) -> &mut Self {
        self.checks.push(check);
        self
    }

    /// Build the [`Command`]
    pub fn build(&mut self) -> Result<Command, BuilderError> {
        #[allow(clippy::or_fun_call)]
        let name = self.name.take().ok_or(BuilderError::MissingField("name"))?;
        #[allow(clippy::or_fun_call)]
        let description = self
            .description
            .take()
            .ok_or(BuilderError::MissingField("description"))?;
        #[allow(clippy::or_fun_call)]
        let on_process = self
            .on_process
            .take()
            .ok_or(BuilderError::MissingField("on_process"))?;
        let checks = std::mem::take(&mut self.checks);

        Ok(Command {
            name: name.into(),
            description: description.into(),
            arguments: std::mem::take(&mut self.arguments).into_boxed_slice(),

            on_process,
            checks,
        })
    }
}

impl std::fmt::Debug for CommandBuilder<'_, '_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommandBuilder")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("arguments", &self.arguments)
            .field("on_process", &self.on_process.as_ref().map(|_| "<func>"))
            .finish()
    }
}

impl Default for CommandBuilder<'_, '_> {
    fn default() -> Self {
        Self::new()
    }
}

/// A slash framework help command
pub struct HelpCommand {
    /// Description
    description: Box<str>,

    /// Arguments
    arguments: Box<[ArgumentParam]>,

    /// The main "process" func
    on_process: HelpOnProcessFutureFn,
}

impl HelpCommand {
    /// Get the help command description
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Get the help command arguments
    pub fn arguments(&self) -> &[ArgumentParam] {
        &self.arguments
    }

    /// Fire the on_process hook
    pub async fn fire_on_process(
        &self,
        ctx: Context,
        interaction: CommandInteraction,
        map: Arc<HashMap<Box<str>, Command>>,
    ) -> Result<(), BoxError> {
        (self.on_process)(ctx, interaction, map).await
    }

    /// Register this help command
    pub fn register(&self, mut command: CreateCommand) -> CreateCommand {
        command = command.name("help").description(self.description());

        for argument in self.arguments().iter() {
            let option_kind = match argument.kind() {
                DataType::Boolean => CommandOptionType::Boolean,
                DataType::String => CommandOptionType::String,
                DataType::Integer => CommandOptionType::Integer,
            };
            let option =
                CreateCommandOption::new(option_kind, argument.name(), argument.description())
                    .required(argument.required());
            command = command.add_option(option);
        }

        command
    }
}

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

/// A builder for a [`HelpCommand`].
pub struct HelpCommandBuilder<'a> {
    description: Option<&'a str>,
    arguments: Vec<ArgumentParam>,

    on_process: Option<HelpOnProcessFutureFn>,
}

impl<'a> HelpCommandBuilder<'a> {
    /// Make a new [`HelpCommandBuilder`].
    pub fn new() -> Self {
        Self {
            description: None,
            arguments: Vec::new(),

            on_process: None,
        }
    }

    /// The help command description
    pub fn description(&mut self, description: &'a str) -> &mut Self {
        self.description = Some(description);
        self
    }

    /// Add an argument
    pub fn argument(&mut self, argument: ArgumentParam) -> &mut Self {
        self.arguments.push(argument);
        self
    }

    /// The on_process hook
    pub fn on_process<F, A>(&mut self, on_process: HelpOnProcessFutureFnPtr<F, A>) -> &mut Self
    where
        F: Future<Output = Result<(), BoxError>> + Send + 'static,
        A: FromOptions + 'static,
    {
        // Trampoline so user does not have to box manually and parse their args manually
        self.on_process = Some(Box::new(move |ctx, interaction, map| {
            Box::pin(async move {
                let args = A::from_options(&interaction)?;
                (on_process)(ctx, interaction, map, args).await
            })
        }));

        self
    }

    /// Build the [`HelpCommand`]
    pub fn build(&mut self) -> Result<HelpCommand, BuilderError> {
        #[allow(clippy::or_fun_call)]
        let description = self
            .description
            .take()
            .ok_or(BuilderError::MissingField("description"))?;
        #[allow(clippy::or_fun_call)]
        let on_process = self
            .on_process
            .take()
            .ok_or(BuilderError::MissingField("on_process"))?;

        Ok(HelpCommand {
            description: description.into(),
            arguments: std::mem::take(&mut self.arguments).into_boxed_slice(),

            on_process,
        })
    }
}

impl std::fmt::Debug for HelpCommandBuilder<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HelpCommandBuilder")
            .field("description", &self.description)
            .field("arguments", &self.arguments)
            .field("on_process", &self.on_process.as_ref().map(|_| "<func>"))
            .finish()
    }
}

impl Default for HelpCommandBuilder<'_> {
    fn default() -> Self {
        Self::new()
    }
}