aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-19 17:33:46 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-19 17:33:46 -0600
commit401bc1c5485e26329598d59a140e51f70f58857c (patch)
tree6ddb4f146808550e2c70fe38deed0db59d5b8cce /src
parent87608eabbcbd105f5f5fecf7ab00a7bc93573477 (diff)
Fix diagrams, allow multiple roles.
Diffstat (limited to 'src')
-rw-r--r--src/commands/discocaml.rs38
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)
}
}
}