mod dox;
mod help;
pub use dox::Dox;
use frakti::{client_cyper::Bot, types::Message};
pub use help::Help;
use log::info;
pub trait Command {
const TRIGGER: &'static str;
const HELP: &'static str;
fn execute(self, bot: &Bot, msg: Message, username: &str) -> impl Future<Output = String>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Commands {
Help(Help),
Dox(Dox),
}
impl Commands {
#[must_use]
pub fn parse(text: Option<&String>, username: &str) -> Option<Self> {
let text = text?.trim();
let (command, arg) = text.split_once(' ').unwrap_or((text, ""));
let slash = command.starts_with('/');
if !slash {
return None;
}
let command = &command[1..];
let (command, mention) = command.split_once('@').unwrap_or((command, ""));
if !mention.is_empty() && mention != username {
return None;
}
match command {
Dox::TRIGGER => {
let arg = arg.trim();
let doxee = if arg.is_empty() {
None
} else {
Some(arg.to_string())
};
Some(Self::Dox(Dox { doxee }))
}
Help::TRIGGER | "start" => Some(Self::Help(Help)),
_ => None,
}
}
pub async fn execute(self, bot: &Bot, msg: Message, username: &str) -> String {
info!("Executing command: {self:?}");
match self {
Self::Help(help) => help.execute(bot, msg, username).await,
Self::Dox(dox) => Box::pin(dox.execute(bot, msg, username)).await,
}
}
}
pub const LIST: [(&str, &str); 2] = [(Dox::TRIGGER, Dox::HELP), (Help::TRIGGER, Help::HELP)];