clove/allInOne
src/commands/mod.rs
// src/commands/mod.rs
mod autoproxy;
mod edit;
mod explain;
mod fban;
mod help;
mod log;
mod member;
mod ping;
mod reproxy;
mod switch;
use serde_json::json;
use serenity::all::{Context, GuildId, Message};
use crate::{
Bot, discord,
proxy::{self, Member},
};
impl Bot {
pub async fn command(
&self,
ctx: &Context,
message: &Message,
guild: GuildId,
) -> anyhow::Result<bool> {
let Some((name, args, rest)) = proxy::command(&message.content, &self.command_prefix)
else {
return Ok(false);
};
let reply = match name.as_str() {
"help" => Some(self.command_text(help::text())),
"version" => Some(format!("proxyBot v{}", env!("CARGO_PKG_VERSION"))),
"explain" | "ex" => Some(self.explain()),
"autoproxy" | "ap" => Some(self.autoproxy(guild, &args).await?),
"edit" | "e" => self.edit(ctx, message, rest).await?.map(str::to_owned),
"fban" => self.fake_ban(ctx, message, rest).await?,
"log" => Some(self.log_config(ctx, message, guild, &args).await?),
"logclean" => {
let args = ["cleanup".to_owned()]
.into_iter()
.chain(args)
.collect::<Vec<_>>();
Some(self.log_config(ctx, message, guild, &args).await?)
}
"member" | "m" => self.member(ctx, message, &args).await?,
"ping" => {
self.ping(ctx, message).await?;
None
}
"reproxy" | "rp" => self.reproxy(ctx, message, &args).await?,
"switch" | "sw" => Some(self.switch(&args).await),
_ => Some(format!(
"Unknown command `{name}`. Try `{}help`.",
self.command_prefix
)),
};
if let Some(reply) = reply {
discord::reply(ctx, message, json!({ "content": reply })).await?;
}
Ok(true)
}
fn command_text(&self, template: &str) -> String {
template.replace("{prefix}", &self.command_prefix)
}
async fn command_members(&self, args: &[String]) -> Result<Vec<Member>, String> {
let state = self.system.read().await;
let mut found = Vec::new();
let mut missing = Vec::new();
for name in args {
match state
.members
.iter()
.find(|member| member.name.to_lowercase() == *name)
{
Some(member) => found.push(member.clone()),
None => missing.push(format!("`{name}`")),
}
}
if missing.is_empty() {
Ok(found)
} else {
Err(format!(
"No member called {}. Use their short name, like `c1`.",
missing.join(", ")
))
}
}
}