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
pub mod help_commands; pub mod macros { pub use command_attr::{command, group, help, check, hook}; } 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::Message, permissions::Permissions, }; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; use tokio::sync::Mutex; use futures::future::BoxFuture; use uwl::Stream; use async_trait::async_trait; use tracing::instrument; #[cfg(feature = "cache")] use crate::model::channel::Channel; #[cfg(feature = "cache")] use crate::cache::Cache; #[cfg(feature = "cache")] use crate::model::guild::Member; #[cfg(all(feature = "cache", feature = "http", feature = "model"))] use crate::model::{guild::Role, id::RoleId}; /// An enum representing all possible fail conditions under which a command won't /// be executed. #[derive(Debug)] #[non_exhaustive] 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(Duration), /// 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 }, } type DispatchHook = for<'fut> fn(&'fut Context, &'fut Message, DispatchError) -> BoxFuture<'fut , ()>; type BeforeHook = for<'fut> fn(&'fut Context, &'fut Message, &'fut str) -> BoxFuture<'fut, bool>; type AfterHook = for<'fut> fn(&'fut Context, &'fut Message, &'fut str, Result<(), CommandError>) -> BoxFuture<'fut, ()>; type UnrecognisedHook = for<'fut> fn(&'fut Context, &'fut Message, &'fut str) -> BoxFuture<'fut, ()>; type NormalMessageHook = for<'fut> fn(&'fut Context, &'fut Message) -> BoxFuture<'fut, ()>; type PrefixOnlyHook = for<'fut> fn(&'fut Context, &'fut Message) -> BoxFuture<'fut, ()>; /// 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: Mutex<HashMap<String, Bucket>>, before: Option<BeforeHook>, after: Option<AfterHook>, dispatch: Option<DispatchHook>, unrecognised_command: Option<UnrecognisedHook>, normal_message: Option<NormalMessageHook>, prefix_only: Option<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; /// /// # async fn run() -> Result<(), Box<dyn std::error::Error>> { /// let token = std::env::var("DISCORD_TOKEN")?; /// let framework = StandardFramework::new() /// .configure(|c| c /// .with_whitespace(true) /// .prefix("~")); /// /// let mut client = Client::builder(&token).event_handler(Handler).framework(framework).await?; /// # Ok(()) /// # } /// ``` /// /// [`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::framework::standard::macros::command; /// use serenity::framework::standard::{StandardFramework, CommandResult}; /// /// #[command] /// // Registers the bucket `basic` to this command. /// #[bucket = "basic"] /// async fn nothing() -> CommandResult { /// Ok(()) /// } /// /// # async fn run() { /// let framework = StandardFramework::new() /// .bucket("basic", |b| b.delay(2).time_span(10).limit(3)) /// .await; /// # } /// ``` #[inline] pub async fn bucket<F>(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.lock().await.insert( name.to_string(), Bucket { ratelimit: Ratelimit { delay, limit: Some((time_span, limit)), }, users: HashMap::new(), check, }, ); self } /// Whether the message should be ignored because it is from a bot or webhook. fn should_ignore(&self, msg: &Message) -> bool { (self.config.ignore_bots && msg.author.bot) || (self.config.ignore_webhooks && msg.webhook_id.is_some()) } async fn should_fail<'a>( &'a self, ctx: &'a Context, msg: &'a Message, args: &'a 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(channel)) = msg.channel_id.to_channel_cached(&ctx).await { let guild_id = channel.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).await { if self.config.blocked_users.contains(&guild.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); } { let mut buckets = self.buckets.lock().await; if let Some(ref mut bucket) = command.bucket.as_ref().and_then(|b| buckets.get_mut(*b)) { let rate_limit = bucket.take(msg.author.id.0); let apply = match bucket.check.as_ref() { Some(check) => (check)(ctx, msg.guild_id, msg.channel_id, msg.author.id).await, None => true, }; if let Some(rate_limit)= rate_limit { if apply { return Some(DispatchError::Ratelimited(rate_limit)) } } } } for check in group.checks.iter().chain(command.checks.iter()) { let res = (check.function)(ctx, msg, args, command).await; 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] /// async fn ping(ctx: &Context, msg: &Message) -> CommandResult { /// msg.channel_id.say(&ctx.http, "pong!").await?; /// /// Ok(()) /// } /// /// #[command] /// async fn pong(ctx: &Context, msg: &Message) -> CommandResult { /// msg.channel_id.say(&ctx.http, "ping!").await?; /// /// Ok(()) /// } /// /// #[group("bingbong")] /// #[commands(ping, pong)] /// struct BingBong; /// /// let framework = StandardFramework::new() /// // Groups' names are changed to all uppercase, plus appended with `_GROUP`. /// .group(&BINGBONG_GROUP); /// ``` 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::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::standard::DispatchError; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn dispatch_error_hook(context: &Context, msg: &Message, error: DispatchError) { /// match error { /// DispatchError::NotEnoughArguments { min, given } => { /// let s = format!("Need {} arguments, but only got {}.", min, given); /// /// let _ = msg.channel_id.say(&context, &s).await; /// }, /// DispatchError::TooManyArguments { max, given } => { /// let s = format!("Max arguments allowed is {}, but got {}.", max, given); /// /// let _ = msg.channel_id.say(&context, &s).await; /// }, /// _ => println!("Unhandled dispatch error."), /// } /// } /// /// let framework = StandardFramework::new() /// .on_dispatch_error(dispatch_error_hook); /// ``` pub fn on_dispatch_error(mut self, f: DispatchHook) -> Self { self.dispatch = Some(f); self } /// Specify the function to be called on messages comprised of only the prefix. pub fn prefix_only(mut self, f: PrefixOnlyHook) -> Self { self.prefix_only = Some(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::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn before_hook(_: &Context, _: &Message, cmd_name: &str) -> bool { /// println!("Running command {}", cmd_name); /// true /// } /// let framework = StandardFramework::new() /// .before(before_hook); /// ``` /// /// Using before to prevent command usage: /// /// ```rust,no_run /// # use serenity::prelude::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn before_hook(ctx: &Context, msg: &Message, cmd_name: &str) -> bool { /// if let Ok(channel) = msg.channel_id.to_channel(ctx).await { /// // Don't run unless in nsfw channel /// if !channel.is_nsfw() { /// return false; /// } /// } /// /// println!("Running command {}", cmd_name); /// /// true /// } /// /// let framework = StandardFramework::new() /// .before(before_hook); /// ``` pub fn before(mut self, f: BeforeHook) -> Self { self.before = Some(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::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::standard::CommandError; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn after_hook(_: &Context, _: &Message, cmd_name: &str, error: Result<(), CommandError>) { /// // Print out an error if it happened /// if let Err(why) = error { /// println!("Error in {}: {:?}", cmd_name, why); /// } /// } /// /// let framework = StandardFramework::new() /// .after(after_hook); /// ``` pub fn after(mut self, f: AfterHook) -> Self { self.after = Some(f); self } /// Specify the function to be called if no command could be dispatched. /// /// # Examples /// /// Using `unrecognised_command`: /// /// ```rust,no_run /// # use serenity::prelude::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn unrecognised_command_hook(_: &Context, msg: &Message, unrecognised_command_name: &str) { /// println!("A user named {:?} tried to executute an unknown command: {}", /// msg.author.name, unrecognised_command_name /// ); /// } /// /// let framework = StandardFramework::new() /// .unrecognised_command(unrecognised_command_hook); /// ``` pub fn unrecognised_command(mut self, f: UnrecognisedHook) -> Self { self.unrecognised_command = Some(f); self } /// Specify the function to be called if a message contains no command. /// /// # Examples /// /// Using `normal_message`: /// /// ```rust,no_run /// # use serenity::prelude::*; /// # use serenity::model::prelude::*; /// use serenity::framework::standard::macros::hook; /// use serenity::framework::StandardFramework; /// /// #[hook] /// async fn normal_message_hook(_: &Context, msg: &Message) { /// println!("Received a generic message: {:?}", msg.content); /// } /// /// let framework = StandardFramework::new() /// .normal_message(normal_message_hook); /// ``` pub fn normal_message(mut self, f: NormalMessageHook) -> Self { self.normal_message = Some(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. pub fn help(mut self, h: &'static HelpCommand) -> Self { self.help = Some(h); self } } #[async_trait] impl Framework for StandardFramework { #[instrument(skip(self, ctx))] async fn dispatch(&self, mut ctx: Context, msg: Message) { if self.should_ignore(&msg) { return; } let mut stream = Stream::new(&msg.content); stream.take_while_char(|c| c.is_whitespace()); let prefix = parse::prefix(&ctx, &msg, &mut stream, &self.config).await; if prefix.is_some() && stream.rest().is_empty() { if let Some(prefix_only) = &self.prefix_only { prefix_only(&mut ctx, &msg).await; } return; } if prefix.is_none() && !(self.config.no_dm_prefix && msg.is_private()) { if let Some(normal) = &self.normal_message { normal(&mut ctx, &msg).await; } return; } let invocation = parse::command( &ctx, &msg, &mut stream, &self.groups, &self.config, self.help.as_ref().map(|h| h.options.names), ).await; let invoke = match invocation { Ok(i) => i, Err(ParseError::UnrecognisedCommand(unreg)) => { if let Some(unreg) = unreg { if let Some(unrecognised_command) = &self.unrecognised_command { unrecognised_command(&mut ctx, &msg, &unreg).await; } } if let Some(normal) = &self.normal_message { normal(&mut ctx, &msg).await; } return; } Err(ParseError::Dispatch(error)) => { if let Some(dispatch) = &self.dispatch { dispatch(&mut ctx, &msg, error).await; } return; } }; match invoke { Invoke::Help(name) => { let args = Args::new(stream.rest(), &self.config.delimiters); let owners = self.config.owners.clone(); let groups = self.groups.iter().map(|(g, _)| *g).collect::<Vec<_>>(); // `parse_command` promises to never return a help invocation if `StandardFramework::help` is `None`. let help = self.help.unwrap(); if let Some(before) = &self.before { if !before(&mut ctx, &msg, name).await { return; } } let res = (help.fun)(&mut ctx, &msg, args, help.options, &groups, owners).await; if let Some(after) = &self.after { after(&mut ctx, &msg, name, res).await; } } 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(&ctx, &msg, &mut args, &command.options, &group.options).await { if let Some(dispatch) = &self.dispatch { dispatch(&mut ctx, &msg, error).await; } return; } let name = command.options.names[0]; if let Some(before) = &self.before { if !before(&mut ctx, &msg, name).await { return; } } let res = (command.fun)(&mut ctx, &msg, args).await; if let Some(after) = &self.after { after(&mut ctx, &msg, name, res).await; } } } } } 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) async fn has_correct_permissions( cache: impl AsRef<Cache>, options: &impl CommonOptions, message: &Message, ) -> bool { if options.required_permissions().is_empty() { true } else if let Some(guild) = message.guild(&cache).await { let perms = guild.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, roles: &HashMap<RoleId, Role>, member: &Member) -> bool { if options.allowed_roles().is_empty() { true } else { options.allowed_roles() .iter() .flat_map(|r| roles.values().find(|role| *r == role.name)) .any(|g| member.roles.contains(&g.id)) } }