command macros that will hopefully work

This commit is contained in:
jude
2020-08-06 19:18:30 +01:00
parent 784a72dd08
commit 0e3c514f35
10 changed files with 938 additions and 69 deletions

View File

@ -6,13 +6,7 @@ use serenity::{
model::channel::Message,
};
use std::{
collections::HashSet,
hash::{
Hash,
Hasher
},
};
use std::collections::HashMap;
use serenity::framework::standard::CommandFn;
@ -23,63 +17,25 @@ pub enum PermissionLevel {
}
pub struct Command {
name: String,
required_perms: PermissionLevel,
can_blacklist: bool,
supports_dm: bool,
func: CommandFn,
pub name: &'static str,
pub required_perms: PermissionLevel,
pub can_blacklist: bool,
pub supports_dm: bool,
pub func: CommandFn,
}
impl Hash for Command {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
impl PartialEq for Command {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Command {}
// create event handler for bot
pub struct RegexFramework {
commands: HashSet<Command>,
commands: HashMap<String, &'static Command>,
command_names: String,
default_prefix: String,
ignore_bots: bool,
}
impl Command {
pub fn from(name: &str, required_perms: PermissionLevel, func: CommandFn) -> Self {
Command {
name: name.to_string(),
required_perms,
can_blacklist: true,
supports_dm: false,
func,
}
}
pub fn can_blacklist(&mut self, can_blacklist: bool) -> &mut Self {
self.can_blacklist = can_blacklist;
self
}
pub fn supports_dm(&mut self, supports_dm: bool) -> &mut Self {
self.supports_dm = supports_dm;
self
}
}
impl RegexFramework {
pub fn new() -> Self {
Self {
commands: HashSet::new(),
commands: HashMap::new(),
command_names: String::new(),
default_prefix: String::from("$"),
ignore_bots: true,
@ -98,17 +54,17 @@ impl RegexFramework {
self
}
pub fn add_command(mut self, command: Command) -> Self {
self.commands.insert(command);
pub fn add_command(mut self, name: String, command: &'static Command) -> Self {
self.commands.insert(name, command);
self
}
pub fn build(mut self) -> Self {
self.command_names = self.commands
.iter()
.map(|c| c.name.clone())
.collect::<Vec<String>>()
.keys()
.map(|k| &k[..])
.collect::<Vec<&str>>()
.join("|");
self

View File

@ -12,13 +12,12 @@ use serenity::{
},
framework::standard::{
Args, CommandResult,
macros::{
command,
}
},
prelude::TypeMapKey,
};
use regex_command_attr::command;
use sqlx::{
Pool,
mysql::{
@ -33,7 +32,7 @@ use std::{
env,
};
use crate::framework::{RegexFramework, Command, PermissionLevel};
use crate::framework::RegexFramework;
struct SQLPool;
@ -56,13 +55,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let framework = RegexFramework::new()
.ignore_bots(true)
.default_prefix("$")
.add_command(Command::from("help", PermissionLevel::Unrestricted, help_command))
.add_command("help".to_string(), &HELP_COMMAND)
.add_command("h".to_string(), &HELP_COMMAND)
.build();
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))
.intents(GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILDS | GatewayIntents::DIRECT_MESSAGES)
.framework(framework)
.await.expect("Error occured creating client");
.await.expect("Error occurred creating client");
client.start_autosharded().await?;
@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
#[command]
async fn help_command(_ctx: &Context, _msg: &Message, _args: Args) -> CommandResult {
async fn help(_ctx: &Context, _msg: &Message, _args: Args) -> CommandResult {
println!("Help command called");
Ok(())