rustacean_roulette/commands/
mod.rs1mod peek;
2mod roulette;
3
4use super::Roulette;
5use frankenstein::{client_reqwest::Bot, types::{BotCommand, Message}};
6use peek::PeekCommand;
7use roulette::RouletteCommand;
8use tokio::sync::Mutex;
9
10pub trait Command {
12 const TRIGGER: &'static str;
14 const HELP: &'static str;
16 async fn execute(
18 bot: &Bot,
19 msg: Message,
20 roulette: &Mutex<Roulette>,
21 ) -> Option<String>;
22}
23
24#[non_exhaustive]
26pub enum Commands {
27 Peek,
28 Roulette,
29}
30
31impl Commands {
32 pub fn parse(text: Option<&String>, username: &str) -> Option<Commands> {
39 let Some(text) = text else {
40 return None;
41 };
42 let text = text.trim();
43 let (command, _arg) = text.split_once(' ').unwrap_or((text, ""));
44
45 let slash = command.starts_with('/');
51 if !slash {
52 return None;
53 }
54 let command = &command[1..];
55
56 let (command, mention) = command.split_once('@').unwrap_or((command, ""));
58 if !mention.is_empty() && mention != username {
59 return None;
60 }
61
62 match command {
64 PeekCommand::TRIGGER => Some(Commands::Peek),
65 RouletteCommand::TRIGGER => Some(Commands::Roulette),
66 _ => None,
67 }
68 }
69
70 pub async fn execute(
72 &self,
73 bot: &Bot,
74 msg: Message,
75 roulette: &Mutex<Roulette>,
76 ) -> Option<String> {
77 match self {
78 Self::Peek => PeekCommand::execute(bot, msg, roulette).await,
79 Self::Roulette => RouletteCommand::execute(bot, msg, roulette).await,
80 }
81 }
82
83 pub fn list() -> Vec<BotCommand> {
85 vec![
86 BotCommand {
87 command: PeekCommand::TRIGGER.to_string(),
88 description: PeekCommand::HELP.to_string(),
89 },
90 BotCommand {
91 command: RouletteCommand::TRIGGER.to_string(),
92 description: RouletteCommand::HELP.to_string(),
93 },
94 ]
95 }
96}