diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/discocaml.rs | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/commands/discocaml.rs b/src/commands/discocaml.rs index fb6b05b..a24276d 100644 --- a/src/commands/discocaml.rs +++ b/src/commands/discocaml.rs @@ -17,7 +17,7 @@ use serenity::{ model::Permissions, }; use sqlx::SqlitePool; -use std::process::Stdio; +use std::{collections::BTreeSet, process::Stdio}; use tokio::{io::AsyncWriteExt, process::Command}; use crate::utils::EnumAsArray; @@ -26,7 +26,7 @@ use crate::utils::EnumAsArray; #[serde(deny_unknown_fields)] pub struct DiscocamlConfig { pub command: Vec<String>, - pub role: RoleId, + pub roles: BTreeSet<RoleId>, } #[derive(Debug, PartialEq, Serialize)] @@ -117,8 +117,36 @@ pub fn command() -> CreateCommand { async fn check_permissions(config: &DiscocamlConfig, member: Option<&Member>) -> Result<()> { if let Some(member) = member { - if !member.roles.contains(&config.role) { - bail!("This command can only be used by <@&{}>.", config.role) + if !member.roles.iter().any(|role| config.roles.contains(role)) { + match config.roles.len() { + 0 => bail!("This command can not be used, because roles are not configured."), + 1 => bail!( + "This command can only be used by <@&{}>.", + config.roles.iter().next().unwrap() + ), + 2 => { + let mut iter = config.roles.iter(); + bail!( + "This command can only be used by <@&{}> or <@&{}>.", + iter.next().unwrap(), + iter.next().unwrap() + ) + } + _ => { + let mut iter = config.roles.iter(); + let last = iter.next().unwrap(); + let mut buf = String::new(); + for role in iter { + buf.push_str("<@&"); + buf.push_str(&role.to_string()); + buf.push_str(">, "); + } + buf.push_str(", or <@&"); + buf.push_str(&last.to_string()); + buf.push('>'); + bail!("This command can only be used by {}.", buf) + } + } } Ok(()) } else { @@ -186,7 +214,7 @@ where DiscocamlResponse::Error(err) => { let err = anyhow!("got an error from discocaml: `{}`", err); respond_with_error(&err, send).await; - return Err(err); + Err(err) } } } |