Skip to content

Commit

Permalink
Command prefix refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
1Git2Clone committed Oct 27, 2024
1 parent 00434ab commit cf2914b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
11 changes: 10 additions & 1 deletion src/data/bot_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::enums::schemas::DatabaseSchema;
use crate::{
enums::schemas::DatabaseSchema, utils::string_manipulation::upper_lowercase_permutations,
};
use std::collections::HashMap;

use lazy_static::lazy_static;
Expand All @@ -24,4 +26,11 @@ lazy_static! {
])
};
pub(crate) static ref XP_COOLDOWN_NUMBER_SECS: i64 = 60;
pub(crate) static ref BOT_PREFIXES: Vec<String> = {
let mut temp = vec![];
temp.append(&mut upper_lowercase_permutations("hu"));
temp.append(&mut upper_lowercase_permutations("ht"));

temp
};
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pub mod data;
pub mod enums;
pub mod event_handler;
pub mod extra_threads;
pub mod structs;
pub mod tests;
pub mod utils;
15 changes: 5 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod commands;
// use commands::general_commands::*;

mod data;
use data::bot_data::{BOT_TOKEN, START_TIME};
use data::bot_data::{BOT_PREFIXES, BOT_TOKEN, START_TIME};
use data::command_data::Data;

mod enums;
Expand All @@ -62,8 +62,7 @@ use extra_threads::xp_command_cooldown::periodically_clean_users_on_diff_thread;
mod event_handler;
use event_handler::handler::event_handler;

mod structs;
use structs::CmdPrefixes;
mod utils;

mod tests;

Expand All @@ -80,8 +79,6 @@ use std::sync::Arc;
async fn main() {
let _ = START_TIME.elapsed().as_secs(); // Dummy data to get the time elapsing started

let bot_prefixes = CmdPrefixes::set();

dotenv::dotenv().ok();
periodically_clean_users_on_diff_thread();
let token = BOT_TOKEN.to_string();
Expand Down Expand Up @@ -154,10 +151,9 @@ async fn main() {

prefix_options: poise::PrefixFrameworkOptions {
prefix: None,
additional_prefixes: bot_prefixes
.prefixes
additional_prefixes: BOT_PREFIXES
.iter()
.map(|x| poise::Prefix::Literal(x.to_owned()))
.map(|x| poise::Prefix::Literal(x.as_str()))
.collect::<Vec<poise::Prefix>>(),
mention_as_prefix: true,
case_insensitive_commands: true,
Expand Down Expand Up @@ -214,8 +210,7 @@ async fn main() {
// )
.activity(serenity::ActivityData::custom(format!(
"Usable prefixes: [ {} ]",
bot_prefixes
.prefixes
BOT_PREFIXES
.iter()
.filter(|&x| x.chars().all(|c| c.is_lowercase()))
.map(|x| x.to_string())
Expand Down
11 changes: 0 additions & 11 deletions src/structs/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ fn normal_types() {
use crate::data::command_data::Data;
use crate::enums::command_enums::EmbedType;
use crate::enums::schemas::DatabaseSchema;
use crate::structs::CmdPrefixes;

_is_normal::<Data>();
_is_normal::<EmbedType>();
_is_normal::<DatabaseSchema>();
_is_normal::<CmdPrefixes>();
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod string_manipulation;
19 changes: 19 additions & 0 deletions src/utils/string_manipulation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub fn upper_lowercase_permutations(data: &str) -> Vec<String> {
if data.is_empty() {
return vec![String::new()];
}

let first = data.chars().next().unwrap();
let rest = &data[1..];

let permutations = upper_lowercase_permutations(rest);

let mut result: Vec<String> = Vec::new();

for perm in permutations {
result.push(format!("{}{}", first.to_ascii_lowercase(), perm));
result.push(format!("{}{}", first.to_ascii_uppercase(), perm));
}

result
}

0 comments on commit cf2914b

Please sign in to comment.