Make Command system more flexible

This commit is contained in:
Joscha 2023-01-26 17:51:04 +01:00
parent 2719ab3301
commit ff886efd78
6 changed files with 289 additions and 261 deletions

View file

@ -1,4 +1,6 @@
mod bang;
mod clap;
mod hidden;
use std::future::Future;
@ -7,45 +9,13 @@ use async_trait::async_trait;
use crate::api::{self, Message, MessageId};
use crate::conn::{self, ConnTx, Joined};
pub use self::clap::{Clap, ClapCommand};
pub use self::bang::*;
pub use self::clap::*;
pub use self::hidden::*;
use super::instance::InstanceConfig;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
/// Global commands always respond. They override any specific or general
/// commands of the same name.
Global,
/// General commands only respond if no nick is specified.
General,
/// Specific commands only respond if the bot's current nick is specified.
Specific,
}
impl Kind {
pub fn global_and_general_usage(cmd_name: &str) -> String {
format!("!{cmd_name}")
}
pub fn specific_nick(nick: &str) -> String {
nick.replace(char::is_whitespace, "")
}
pub fn specific_usage(cmd_name: &str, nick: &str) -> String {
format!("!{cmd_name} @{}", Self::specific_nick(nick))
}
pub fn usage(self, cmd_name: &str, nick: &str) -> String {
match self {
Self::Global | Self::General => Self::global_and_general_usage(cmd_name),
Self::Specific => Self::specific_usage(cmd_name, nick),
}
}
}
pub struct Context {
pub name: String,
pub kind: Kind,
pub config: InstanceConfig,
pub conn_tx: ConnTx,
pub joined: Joined,
@ -75,9 +45,10 @@ impl Context {
}
}
#[allow(unused_variables)]
#[async_trait]
pub trait Command<B, E> {
fn description(&self) -> Option<String> {
fn description(&self, ctx: &Context) -> Option<String> {
None
}