2020-08-06 14:22:13 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use serenity::{
|
|
|
|
client::Context,
|
|
|
|
framework::Framework,
|
|
|
|
model::channel::Message,
|
|
|
|
};
|
|
|
|
|
2020-08-06 19:39:05 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fmt,
|
|
|
|
};
|
2020-08-06 14:22:13 +00:00
|
|
|
|
|
|
|
use serenity::framework::standard::CommandFn;
|
|
|
|
|
2020-08-06 19:39:05 +00:00
|
|
|
#[derive(Debug)]
|
2020-08-06 14:22:13 +00:00
|
|
|
pub enum PermissionLevel {
|
|
|
|
Unrestricted,
|
|
|
|
Managed,
|
|
|
|
Restricted,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Command {
|
2020-08-06 18:18:30 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
pub required_perms: PermissionLevel,
|
|
|
|
pub supports_dm: bool,
|
2020-08-06 19:39:05 +00:00
|
|
|
pub can_blacklist: bool,
|
2020-08-06 18:18:30 +00:00
|
|
|
pub func: CommandFn,
|
2020-08-06 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:39:05 +00:00
|
|
|
impl fmt::Debug for Command {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("Command")
|
|
|
|
.field("name", &self.name)
|
|
|
|
.field("required_perms", &self.required_perms)
|
|
|
|
.field("supports_dm", &self.supports_dm)
|
|
|
|
.field("can_blacklist", &self.can_blacklist)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:22:13 +00:00
|
|
|
// create event handler for bot
|
|
|
|
pub struct RegexFramework {
|
2020-08-06 18:18:30 +00:00
|
|
|
commands: HashMap<String, &'static Command>,
|
2020-08-06 14:22:13 +00:00
|
|
|
command_names: String,
|
|
|
|
default_prefix: String,
|
|
|
|
ignore_bots: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegexFramework {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2020-08-06 18:18:30 +00:00
|
|
|
commands: HashMap::new(),
|
2020-08-06 14:22:13 +00:00
|
|
|
command_names: String::new(),
|
|
|
|
default_prefix: String::from("$"),
|
|
|
|
ignore_bots: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_prefix(mut self, new_prefix: &str) -> Self {
|
|
|
|
self.default_prefix = new_prefix.to_string();
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore_bots(mut self, ignore_bots: bool) -> Self {
|
|
|
|
self.ignore_bots = ignore_bots;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:18:30 +00:00
|
|
|
pub fn add_command(mut self, name: String, command: &'static Command) -> Self {
|
|
|
|
self.commands.insert(name, command);
|
2020-08-06 14:22:13 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(mut self) -> Self {
|
|
|
|
self.command_names = self.commands
|
2020-08-06 18:18:30 +00:00
|
|
|
.keys()
|
|
|
|
.map(|k| &k[..])
|
|
|
|
.collect::<Vec<&str>>()
|
2020-08-06 14:22:13 +00:00
|
|
|
.join("|");
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Framework for RegexFramework {
|
|
|
|
async fn dispatch(&self, ctx: Context, msg: Message) {
|
|
|
|
println!("Message received! command_names=={}", self.command_names);
|
|
|
|
}
|
|
|
|
}
|