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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
pub mod help_commands;
pub mod macros {
    pub use command_attr::{command, group, help, check};
}

mod args;
mod configuration;
mod parse;
mod structures;

pub use args::{Args, Delimiter, Error as ArgError, Iter, RawArguments};
pub use configuration::{Configuration, WithWhiteSpace};
pub use structures::*;

use structures::buckets::{Bucket, Ratelimit};
pub use structures::buckets::BucketBuilder;

use parse::{ParseError, Invoke};
use parse::map::{CommandMap, GroupMap, Map};

use super::Framework;
use crate::client::Context;
use crate::model::{
    channel::{Channel, Message},
    permissions::Permissions,
};

use std::collections::HashMap;
use std::sync::Arc;

use threadpool::ThreadPool;
use uwl::Stream;

#[cfg(feature = "cache")]
use crate::cache::CacheRwLock;
#[cfg(feature = "cache")]
use crate::model::guild::{Guild, Member};
#[cfg(feature = "cache")]
use crate::internal::RwLockExt;

/// An enum representing all possible fail conditions under which a command won't
/// be executed.
#[derive(Debug)]
pub enum DispatchError {
    /// When a custom function check has failed.
    CheckFailed(&'static str, Reason),
    /// When the command requester has exceeded a ratelimit bucket. The attached
    /// value is the time a requester has to wait to run the command again.
    Ratelimited(i64),
    /// When the requested command is disabled in bot configuration.
    CommandDisabled(String),
    /// When the user is blocked in bot configuration.
    BlockedUser,
    /// When the guild or its owner is blocked in bot configuration.
    BlockedGuild,
    /// When the channel blocked in bot configuration.
    BlockedChannel,
    /// When the requested command can only be used in a direct message or group
    /// channel.
    OnlyForDM,
    /// When the requested command can only be ran in guilds, or the bot doesn't
    /// support DMs.
    OnlyForGuilds,
    /// When the requested command can only be used by bot owners.
    OnlyForOwners,
    /// When the requested command requires one role.
    LackingRole,
    /// When the command requester lacks specific required permissions.
    LackingPermissions(Permissions),
    /// When there are too few arguments.
    NotEnoughArguments { min: u16, given: usize },
    /// When there are too many arguments.
    TooManyArguments { max: u16, given: usize },
    /// When the command was requested by a bot user when they are set to be
    /// ignored.
    IgnoredBot,
    /// When the bot ignores webhooks and a command was issued by one.
    WebhookAuthor,
    #[doc(hidden)]
    __Nonexhaustive,
}

pub type DispatchHook = dyn Fn(&mut Context, &Message, DispatchError) + Send + Sync + 'static;
type BeforeHook = dyn Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static;
type AfterHook = dyn Fn(&mut Context, &Message, &str, Result<(), CommandError>) + Send + Sync + 'static;
type UnrecognisedHook = dyn Fn(&mut Context, &Message, &str) + Send + Sync + 'static;
type NormalMessageHook = dyn Fn(&mut Context, &Message) + Send + Sync + 'static;
type PrefixOnlyHook = dyn Fn(&mut Context, &Message) + Send + Sync + 'static;

/// A utility for easily managing dispatches to commands.
///
/// Refer to the [module-level documentation] for more information.
///
/// [module-level documentation]: index.html
#[derive(Default)]
pub struct StandardFramework {
    groups: Vec<(&'static CommandGroup, Map)>,
    buckets: HashMap<String, Bucket>,
    before: Option<Arc<BeforeHook>>,
    after: Option<Arc<AfterHook>>,
    dispatch: Option<Arc<DispatchHook>>,
    unrecognised_command: Option<Arc<UnrecognisedHook>>,
    normal_message: Option<Arc<NormalMessageHook>>,
    prefix_only: Option<Arc<PrefixOnlyHook>>,
    config: Configuration,
    help: Option<&'static HelpCommand>,
    /// Whether the framework has been "initialized".
    ///
    /// The framework is initialized once one of the following occurs:
    ///
    /// - configuration has been set;
    /// - a command handler has been set;
    /// - a command check has been set.
    ///
    /// This is used internally to determine whether or not - in addition to
    /// dispatching to the [`EventHandler::message`] handler - to have the
    /// framework check if a [`Event::MessageCreate`] should be processed by
    /// itself.
    ///
    /// [`EventHandler::message`]: ../../client/trait.EventHandler.html#method.message
    /// [`Event::MessageCreate`]: ../../model/event/enum.Event.html#variant.MessageCreate
    pub initialized: bool,
}

impl StandardFramework {
    #[inline]
    pub fn new() -> Self {
        StandardFramework::default()
    }

    /// Configures the framework, setting non-default values. All fields are
    /// optional. Refer to [`Configuration::default`] for more information on
    /// the default values.
    ///
    /// # Examples
    ///
    /// Configuring the framework for a [`Client`], [allowing whitespace between prefixes], and setting the [`prefix`] to `"~"`:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::EventHandler;
    /// # struct Handler;
    /// # impl EventHandler for Handler {}
    /// use serenity::Client;
    /// use serenity::framework::StandardFramework;
    /// use std::env;
    ///
    /// let token = env::var("DISCORD_TOKEN").unwrap();
    /// let mut client = Client::new(&token, Handler).unwrap();
    /// client.with_framework(StandardFramework::new()
    ///     .configure(|c| c
    ///         .with_whitespace(true)
    ///         .prefix("~")));
    /// ```
    ///
    /// [`Client`]: ../../client/struct.Client.html
    /// [`Configuration::default`]: struct.Configuration.html#method.default
    /// [`prefix`]: struct.Configuration.html#method.prefix
    /// [allowing whitespace between prefixes]: struct.Configuration.html#method.with_whitespace
    pub fn configure<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&mut Configuration) -> &mut Configuration,
    {
        f(&mut self.config);

        self
    }

    /// Defines a bucket with `delay` between each command, and the `limit` of uses
    /// per `time_span`.
    ///
    /// # Examples
    ///
    /// Create and use a bucket that limits a command to 3 uses per 10 seconds with
    /// a 2 second delay inbetween invocations:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::standard::macros::command;
    /// use serenity::framework::standard::{StandardFramework, CommandResult};
    ///
    /// #[command]
    /// // Registers the bucket `basic` to this command.
    /// #[bucket = "basic"]
    /// fn nothing() -> CommandResult {
    ///     Ok(())
    /// }
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .bucket("basic", |b| b.delay(2).time_span(10).limit(3)));
    /// ```
    #[inline]
    pub fn bucket<F>(mut self, name: &str, f: F) -> Self
    where
        F: FnOnce(&mut BucketBuilder) -> &mut BucketBuilder
    {
        let mut builder = BucketBuilder::default();

        f(&mut builder);

        let BucketBuilder {
            delay,
            time_span,
            limit,
            check,
        } = builder;

        self.buckets.insert(
            name.to_string(),
            Bucket {
                ratelimit: Ratelimit {
                    delay,
                    limit: Some((time_span, limit)),
                },
                users: HashMap::new(),
                check,
            },
        );

        self
    }

    fn should_fail_common(&self, msg: &Message) -> Option<DispatchError> {
        if self.config.ignore_bots && msg.author.bot {
            return Some(DispatchError::IgnoredBot);
        }

        if self.config.ignore_webhooks && msg.webhook_id.is_some() {
            return Some(DispatchError::WebhookAuthor);
        }

        None
    }

    fn should_fail(
        &mut self,
        ctx: &mut Context,
        msg: &Message,
        args: &mut Args,
        command: &'static CommandOptions,
        group: &'static GroupOptions,
    ) -> Option<DispatchError> {
        if let Some(min) = command.min_args {
            if args.len() < min as usize {
                return Some(DispatchError::NotEnoughArguments {
                    min,
                    given: args.len(),
                });
            }
        }

        if let Some(max) = command.max_args {
            if args.len() > max as usize {
                return Some(DispatchError::TooManyArguments {
                    max,
                    given: args.len(),
                });
            }
        }

        if (group.owner_privilege && command.owner_privilege)
            && self.config.owners.contains(&msg.author.id)
        {
            return None;
        }

        if self.config.blocked_users.contains(&msg.author.id) {
            return Some(DispatchError::BlockedUser);
        }

        #[cfg(feature = "cache")]
        {
            if let Some(Channel::Guild(chan)) = msg.channel_id.to_channel_cached(&ctx.cache) {
                let guild_id = chan.with(|c| c.guild_id);

                if self.config.blocked_guilds.contains(&guild_id) {
                    return Some(DispatchError::BlockedGuild);
                }

                if let Some(guild) = guild_id.to_guild_cached(&ctx.cache) {
                    if self.config.blocked_users.contains(&guild.with(|g| g.owner_id)) {
                        return Some(DispatchError::BlockedGuild);
                    }
                }
            }
        }

        if !self.config.allowed_channels.is_empty() &&
           !self.config.allowed_channels.contains(&msg.channel_id) {
            return Some(DispatchError::BlockedChannel);
        }

        if let Some(ref mut bucket) = command.bucket.as_ref().and_then(|b| self.buckets.get_mut(*b)) {
            let rate_limit = bucket.take(msg.author.id.0);

            let apply = bucket.check.as_ref().map_or(true, |check| {
                (check)(ctx, msg.guild_id, msg.channel_id, msg.author.id)
            });

            if apply && rate_limit > 0 {
                return Some(DispatchError::Ratelimited(rate_limit));
            }
        }

        for check in group.checks.iter().chain(command.checks.iter()) {
            let res = (check.function)(ctx, msg, args, command);

            if let CheckResult::Failure(r) = res {
                return Some(DispatchError::CheckFailed(check.name, r));
            }
        }

        None
    }

    /// Adds a group which can organize several related commands.
    /// Groups are taken into account when using
    /// `serenity::framework::standard::help_commands`.
    ///
    /// # Examples
    ///
    /// Add a group with ping and pong commands:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # use std::error::Error as StdError;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// #
    /// use serenity::client::{Client, Context};
    /// use serenity::model::channel::Message;
    /// use serenity::framework::standard::{
    ///     StandardFramework,
    ///     CommandResult,
    ///     macros::{command, group},
    /// };
    ///
    /// // For information regarding this macro, learn more about it in its documentation in `command_attr`.
    /// #[command]
    /// fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
    ///     msg.channel_id.say(&ctx.http, "pong!")?;
    ///
    ///     Ok(())
    /// }
    ///
    /// #[command]
    /// fn pong(ctx: &mut Context, msg: &Message) -> CommandResult {
    ///     msg.channel_id.say(&ctx.http, "ping!")?;
    ///
    ///     Ok(())
    /// }
    ///
    /// #[group("bingbong")]
    /// #[commands(ping, pong)]
    /// struct BingBong;
    ///
    /// # fn main() -> Result<(), Box<dyn StdError>> {
    /// #   let mut client = Client::new("token", Handler)?;
    /// client.with_framework(StandardFramework::new()
    ///     // Groups' names are changed to all uppercase, plus appended with `_GROUP`.
    ///     .group(&BINGBONG_GROUP));
    /// #   Ok(())
    /// # }
    /// ```
    pub fn group(mut self, group: &'static CommandGroup) -> Self {
        self.group_add(group);
        self.initialized = true;

        self
    }

    /// Adds a group to be used by the framework. Primary use-case is runtime modification
    /// of groups in the framework; will _not_ mark the framework as initialized. Refer to
    /// [`group`] for adding groups in initial configuration.
    ///
    /// Note: does _not_ return `Self` like many other commands. This is because
    /// it's not intended to be chained as the other commands are.
    ///
    /// [`group`]: #method.group
    pub fn group_add(&mut self, group: &'static CommandGroup) {
        let map = if group.options.prefixes.is_empty() {
            Map::Prefixless(
                GroupMap::new(&group.options.sub_groups, &self.config),
                CommandMap::new(&group.options.commands, &self.config),
            )
        } else {
            Map::WithPrefixes(GroupMap::new(&[group], &self.config))
        };

        self.groups.push((group, map));
    }

    /// Removes a group from being used in the framework. Primary use-case is runtime modification
    /// of groups in the framework.
    ///
    /// Note: does _not_ return `Self` like many other commands. This is because
    /// it's not intended to be chained as the other commands are.
    pub fn group_remove(&mut self, group: &'static CommandGroup) {
        // Iterates through the vector and if a given group _doesn't_ match, we retain it
        self.groups.retain(|&(g, _)| g != group)
    }

    /// Specify the function that's called in case a command wasn't executed for one reason or
    /// another.
    ///
    /// DispatchError represents all possible fail conditions.
    ///
    /// # Examples
    ///
    /// Making a simple argument error responder:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// use serenity::framework::standard::DispatchError::{NotEnoughArguments,
    /// TooManyArguments};
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .on_dispatch_error(|context, msg, error| {
    ///         match error {
    ///             NotEnoughArguments { min, given } => {
    ///                 let s = format!("Need {} arguments, but only got {}.", min, given);
    ///
    ///                 let _ = msg.channel_id.say(&context.http, &s);
    ///             },
    ///             TooManyArguments { max, given } => {
    ///                 let s = format!("Max arguments allowed is {}, but got {}.", max, given);
    ///
    ///                 let _ = msg.channel_id.say(&context.http, &s);
    ///             },
    ///             _ => println!("Unhandled dispatch error."),
    ///         }
    ///     }));
    /// ```
    pub fn on_dispatch_error<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message, DispatchError) + Send + Sync + 'static,
    {
        self.dispatch = Some(Arc::new(f));

        self
    }

    /// Specify the function to be called on messages comprised of only the prefix.
    pub fn prefix_only<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message) + Send + Sync + 'static
    {
        self.prefix_only = Some(Arc::new(f));

        self
    }

    /// Specify the function to be called prior to every command's execution.
    /// If that function returns true, the command will be executed.
    ///
    /// # Examples
    ///
    /// Using `before` to log command usage:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .before(|ctx, msg, cmd_name| {
    ///         println!("Running command {}", cmd_name);
    ///         true
    ///     }));
    /// ```
    ///
    /// Using before to prevent command usage:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .before(|ctx, msg, cmd_name| {
    ///         if let Ok(channel) = msg.channel_id.to_channel(ctx) {
    ///             //  Don't run unless in nsfw channel
    ///             if !channel.is_nsfw() {
    ///                 return false;
    ///             }
    ///         }
    ///
    ///         println!("Running command {}", cmd_name);
    ///
    ///         true
    ///     }));
    /// ```
    ///
    pub fn before<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static,
    {
        self.before = Some(Arc::new(f));

        self
    }

    /// Specify the function to be called after every command's execution.
    /// Fourth argument exists if command returned an error which you can handle.
    ///
    /// # Examples
    ///
    /// Using `after` to log command usage:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .after(|ctx, msg, cmd_name, error| {
    ///         //  Print out an error if it happened
    ///         if let Err(why) = error {
    ///             println!("Error in {}: {:?}", cmd_name, why);
    ///         }
    ///     }));
    /// ```
    pub fn after<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message, &str, Result<(), CommandError>) + Send + Sync + 'static,
    {
        self.after = Some(Arc::new(f));

        self
    }

    /// Specify the function to be called if no command could be dispatched.
    ///
    /// # Examples
    ///
    /// Using `unrecognised_command`:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .unrecognised_command(|_ctx, msg, unrecognised_command_name| {
    ///        println!("A user named {:?} tried to executute an unknown command: {}", msg.author.name, unrecognised_command_name);
    ///     }));
    /// ```
    pub fn unrecognised_command<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message, &str) + Send + Sync + 'static,
    {
        self.unrecognised_command = Some(Arc::new(f));

        self
    }

    /// Specify the function to be called if a message contains no command.
    ///
    /// # Examples
    ///
    /// Using `normal_message`:
    ///
    /// ```rust,no_run
    /// # use serenity::prelude::*;
    /// # struct Handler;
    /// #
    /// # impl EventHandler for Handler {}
    /// # let mut client = Client::new("token", Handler).unwrap();
    /// #
    /// use serenity::framework::StandardFramework;
    ///
    /// client.with_framework(StandardFramework::new()
    ///     .normal_message(|ctx, msg| {
    ///         println!("Received a generic message: {:?}", msg.content);
    ///     }));
    /// ```
    pub fn normal_message<F>(mut self, f: F) -> Self
    where
        F: Fn(&mut Context, &Message) + Send + Sync + 'static,
    {
        self.normal_message = Some(Arc::new(f));

        self
    }

    /// Sets what code should be executed when a user sends `(prefix)help`.
    ///
    /// If a [`command`] named `help` in a group was set, then this takes precedence first.
    ///
    /// [`command`]: #method.command
    pub fn help(mut self, h: &'static HelpCommand) -> Self {
        self.help = Some(h);

        self
    }
}

impl Framework for StandardFramework {
    fn dispatch(&mut self, mut ctx: Context, msg: Message, threadpool: &ThreadPool) {
        let mut stream = Stream::new(&msg.content);

        stream.take_while_char(|c| c.is_whitespace());

        let prefix = parse::prefix(&mut ctx, &msg, &mut stream, &self.config);

        if prefix.is_some() && stream.rest().is_empty() {

            if let Some(prefix_only) = &self.prefix_only {
                let prefix_only = Arc::clone(&prefix_only);
                let msg = msg.clone();

                threadpool.execute(move || {
                    prefix_only(&mut ctx, &msg);
                });
            }

            return;
        }

        if prefix.is_none() && !(self.config.no_dm_prefix && msg.is_private()) {

            if let Some(normal) = &self.normal_message {
                let normal = Arc::clone(&normal);
                let msg = msg.clone();

                threadpool.execute(move || {
                    normal(&mut ctx, &msg);
                });
            }

            return;
        }

        if let Some(error) = self.should_fail_common(&msg) {

            if let Some(dispatch) = &self.dispatch {
                dispatch(&mut ctx, &msg, error);
            }

            return;
        }

        let invocation = parse::command(
            &ctx,
            &msg,
            &mut stream,
            &self.groups,
            &self.config,
            self.help.as_ref().map(|h| h.options.names),
        );

        let invoke = match invocation {
            Ok(i) => i,
            Err(ParseError::UnrecognisedCommand(unreg)) => {
                if let Some(unreg) = unreg {
                    if let Some(unrecognised_command) = &self.unrecognised_command {
                        let unrecognised_command = Arc::clone(&unrecognised_command);
                        let mut ctx = ctx.clone();
                        let msg = msg.clone();
                        threadpool.execute(move || {
                            unrecognised_command(&mut ctx, &msg, &unreg);
                        });
                    }
                }

                if let Some(normal) = &self.normal_message {
                    let normal = Arc::clone(&normal);
                    let msg = msg.clone();

                    threadpool.execute(move || {
                        normal(&mut ctx, &msg);
                    });
                }

                return;
            }
            Err(ParseError::Dispatch(error)) => {
                if let Some(dispatch) = &self.dispatch {
                    dispatch(&mut ctx, &msg, error);
                }

                return;
            }
        };

        match invoke {
            Invoke::Help(name) => {
                let args = Args::new(stream.rest(), &self.config.delimiters);

                let before = self.before.clone();
                let after = self.after.clone();
                let owners = self.config.owners.clone();

                let groups = self.groups.iter().map(|(g, _)| *g).collect::<Vec<_>>();

                let msg = msg.clone();

                // `parse_command` promises to never return a help invocation if `StandardFramework::help` is `None`.
                let help = self.help.unwrap();

                threadpool.execute(move || {
                    if let Some(before) = before {
                        if !before(&mut ctx, &msg, name) {
                            return;
                        }
                    }

                    let res = (help.fun)(&mut ctx, &msg, args, help.options, &groups, owners);

                    if let Some(after) = after {
                        after(&mut ctx, &msg, name, res);
                    }
                });
            }
            Invoke::Command { command, group } => {
                let mut args = {
                    use std::borrow::Cow;

                    let mut delims = Cow::Borrowed(&self.config.delimiters);

                    // If user has configured the command's own delimiters, use those instead.
                    if !command.options.delimiters.is_empty() {
                        // FIXME: Get rid of this allocation.
                        let mut v = Vec::with_capacity(command.options.delimiters.len());

                        for delim in command.options.delimiters {
                            if delim.len() == 1 {
                                v.push(Delimiter::Single(delim.chars().next().unwrap()));
                            } else {
                                // This too.
                                v.push(Delimiter::Multiple(delim.to_string()));
                            }
                        }

                        delims = Cow::Owned(v);
                    }

                    Args::new(stream.rest(), &delims)
                };

                if let Some(error) =
                    self.should_fail(&mut ctx, &msg, &mut args, &command.options, &group.options)
                {
                    if let Some(dispatch) = &self.dispatch {
                        dispatch(&mut ctx, &msg, error);
                    }

                    return;
                }

                let before = self.before.clone();
                let after = self.after.clone();
                let msg = msg.clone();
                let name = &command.options.names[0];
                threadpool.execute(move || {
                    if let Some(before) = before {
                        if !before(&mut ctx, &msg, name) {
                            return;
                        }
                    }

                    let res = (command.fun)(&mut ctx, &msg, args);

                    if let Some(after) = after {
                        after(&mut ctx, &msg, name, res);
                    }
                });
            }
        }
    }
}

pub trait CommonOptions {
    fn required_permissions(&self) -> &Permissions;
    fn allowed_roles(&self) -> &'static [&'static str];
    fn checks(&self) -> &'static [&'static Check];
    fn only_in(&self) -> OnlyIn;
    fn help_available(&self) -> bool;
    fn owners_only(&self) -> bool;
    fn owner_privilege(&self) -> bool;
}

impl CommonOptions for &GroupOptions {
    fn required_permissions(&self) -> &Permissions {
        &self.required_permissions
    }

    fn allowed_roles(&self) -> &'static [&'static str] {
        &self.allowed_roles
    }

    fn checks(&self) -> &'static [&'static Check] {
        &self.checks
    }

    fn only_in(&self) -> OnlyIn {
        self.only_in
    }

    fn help_available(&self) -> bool {
        self.help_available
    }

    fn owners_only(&self) -> bool {
        self.owners_only
    }

    fn owner_privilege(&self) -> bool {
        self.owner_privilege
    }
}

impl CommonOptions for &CommandOptions {
    fn required_permissions(&self) -> &Permissions {
        &self.required_permissions
    }

    fn allowed_roles(&self) -> &'static [&'static str] {
        &self.allowed_roles
    }

    fn checks(&self) -> &'static [&'static Check] {
        &self.checks
    }

    fn only_in(&self) -> OnlyIn {
        self.only_in
    }

    fn help_available(&self) -> bool {
        self.help_available
    }

    fn owners_only(&self) -> bool {
        self.owners_only
    }

    fn owner_privilege(&self) -> bool {
        self.owner_privilege
    }
}

#[cfg(feature = "cache")]
pub(crate) fn has_correct_permissions(
    cache: impl AsRef<CacheRwLock>,
    options: &impl CommonOptions,
    message: &Message,
) -> bool {
    if options.required_permissions().is_empty() {
        true
    } else if let Some(guild) = message.guild(&cache) {
        let perms = guild.with(|g| g.user_permissions_in(message.channel_id, message.author.id));

        perms.contains(*options.required_permissions())
    } else {
        false
    }
}

#[cfg(all(feature = "cache", feature = "http"))]
pub(crate) fn has_correct_roles(
    options: &impl CommonOptions,
    guild: &Guild,
    member: &Member)
-> bool {
    if options.allowed_roles().is_empty() {
        true
    } else {
        options.allowed_roles()
            .iter()
            .flat_map(|r| guild.role_by_name(r))
            .any(|g| member.roles.contains(&g.id))
    }
}