From ceac7be083d6b3419a7aea1a9dd88a27018b968d Mon Sep 17 00:00:00 2001 From: jude Date: Tue, 13 Oct 2020 14:35:13 +0100 Subject: [PATCH] moved around the consts so that default prefix env var isnt being read into the framework. made theme color configurable --- README.md | 1 + src/commands/info_cmds.rs | 8 ++++---- src/consts.rs | 10 ++++++++-- src/framework.rs | 24 ++++++++++++++++++------ src/main.rs | 6 ++---- src/models.rs | 13 ++++--------- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index df924df..4ed717b 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,4 @@ __Other Variables__ * `IGNORE_BOTS` - default `1`, if `1`, Reminder Bot will ignore all other bots * `PYTHON_LOCATION` - default `venv/bin/python3`. Can be changed if your Python executable is located somewhere else * `LOCAL_LANGUAGE` - default `EN`. Specifies the string set to fall back to if a string cannot be found (and to be used with new users) +* `THEME_COLOR` - default `8fb677`. Specifies the hex value of the color to use on info message embeds diff --git a/src/commands/info_cmds.rs b/src/commands/info_cmds.rs index e8964c0..7c8107c 100644 --- a/src/commands/info_cmds.rs +++ b/src/commands/info_cmds.rs @@ -45,7 +45,7 @@ async fn help(ctx: &Context, msg: &Message, _args: String) -> CommandResult { msg.channel_id .send_message(ctx, |m| { - m.embed(move |e| e.title("Help").description(desc).color(THEME_COLOR)) + m.embed(move |e| e.title("Help").description(desc).color(*THEME_COLOR)) }) .await?; @@ -75,7 +75,7 @@ async fn info(ctx: &Context, msg: &Message, _args: String) -> CommandResult { msg.channel_id .send_message(ctx, |m| { - m.embed(move |e| e.title("Info").description(desc).color(THEME_COLOR)) + m.embed(move |e| e.title("Info").description(desc).color(*THEME_COLOR)) }) .await?; @@ -97,7 +97,7 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) -> CommandResult { msg.channel_id .send_message(ctx, |m| { - m.embed(move |e| e.title("Donate").description(desc).color(THEME_COLOR)) + m.embed(move |e| e.title("Donate").description(desc).color(*THEME_COLOR)) }) .await?; @@ -111,7 +111,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) -> CommandResult m.embed(move |e| { e.title("Dashboard") .description("https://reminder-bot.com/dashboard") - .color(THEME_COLOR) + .color(*THEME_COLOR) }) }) .await?; diff --git a/src/consts.rs b/src/consts.rs index 927023f..f04cb7d 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,11 +1,11 @@ -pub const PREFIX: &str = "$"; - pub const DAY: u64 = 86_400; pub const HOUR: u64 = 3_600; pub const MINUTE: u64 = 60; pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; +const THEME_COLOR_FALLBACK: u32 = 0x8fb677; + use std::{collections::HashSet, env, iter::FromIterator}; use regex::Regex; @@ -47,4 +47,10 @@ lazy_static! { env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string()); pub static ref STRINGS_TABLE: String = env::var("STRINGS_TABLE").unwrap_or_else(|_| "strings".to_string()); + pub static ref DEFAULT_PREFIX: String = + env::var("DEFAULT_PREFIX").unwrap_or_else(|_| "$".to_string()); + pub static ref THEME_COLOR: u32 = env::var("THEME_COLOR").map_or( + THEME_COLOR_FALLBACK, + |inner| u32::from_str_radix(&inner, 16).unwrap_or(THEME_COLOR_FALLBACK) + ); } diff --git a/src/framework.rs b/src/framework.rs index f6c6e3a..212802e 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -18,9 +18,9 @@ use log::{error, info, warn}; use regex::{Match, Regex}; -use std::{collections::HashMap, env, fmt}; +use std::{collections::HashMap, fmt}; -use crate::{consts::PREFIX, models::ChannelData, SQLPool}; +use crate::{models::ChannelData, SQLPool}; use serenity::futures::TryFutureExt; type CommandFn = @@ -203,7 +203,7 @@ impl RegexFramework { commands: HashMap::new(), command_matcher: Regex::new(r#"^$"#).unwrap(), dm_regex_matcher: Regex::new(r#"^$"#).unwrap(), - default_prefix: env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()), + default_prefix: "".to_string(), client_id: client_id.into(), ignore_bots: true, } @@ -315,7 +315,12 @@ impl Framework for RegexFramework { }) } - async fn check_prefix(ctx: &Context, guild: &Guild, prefix_opt: Option>) -> bool { + async fn check_prefix( + ctx: &Context, + guild: &Guild, + prefix_opt: Option>, + default_prefix: &String, + ) -> bool { if let Some(prefix) = prefix_opt { let pool = ctx .data @@ -343,7 +348,7 @@ impl Framework for RegexFramework { .execute(&pool) .await; - prefix.as_str() == "$" + prefix.as_str() == default_prefix.as_str() } Err(e) => { @@ -371,7 +376,14 @@ impl Framework for RegexFramework { let member = guild.member(&ctx, &msg.author).await.unwrap(); if let Some(full_match) = self.command_matcher.captures(&msg.content[..]) { - if check_prefix(&ctx, &guild, full_match.name("prefix")).await { + if check_prefix( + &ctx, + &guild, + full_match.name("prefix"), + &self.default_prefix, + ) + .await + { match check_self_permissions(&ctx, &guild, &channel).await { Ok(perms) => match perms { PermissionCheck::All => { diff --git a/src/main.rs b/src/main.rs index 790c4a8..15a3148 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ use std::{env, sync::Arc}; use crate::{ commands::{info_cmds, moderation_cmds, reminder_cmds, todo_cmds}, - consts::{CNC_GUILD, PREFIX, SUBSCRIPTION_ROLES}, + consts::{CNC_GUILD, DEFAULT_PREFIX, SUBSCRIPTION_ROLES, THEME_COLOR}, framework::RegexFramework, }; @@ -54,8 +54,6 @@ impl TypeMapKey for FrameworkCtx { type Value = Arc>; } -static THEME_COLOR: u32 = 0x8fb677; - #[tokio::main] async fn main() -> Result<(), Box> { dotenv()?; @@ -70,8 +68,8 @@ async fn main() -> Result<(), Box> { .await?; let framework = RegexFramework::new(logged_in_id) + .default_prefix(DEFAULT_PREFIX.clone()) .ignore_bots(env::var("IGNORE_BOTS").map_or(true, |var| var == "1")) - .default_prefix(&env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string())) .add_command("ping", &info_cmds::PING_COMMAND) .add_command("help", &info_cmds::HELP_COMMAND) .add_command("info", &info_cmds::INFO_COMMAND) diff --git a/src/models.rs b/src/models.rs index bf03057..dbf7e78 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,8 +3,6 @@ use serenity::{ model::{channel::Channel, guild::Guild, id::GuildId, user::User}, }; -use std::env; - use sqlx::{Cursor, MySqlPool, Row}; use chrono::NaiveDateTime; @@ -12,7 +10,7 @@ use chrono_tz::Tz; use log::error; -use crate::consts::{LOCAL_LANGUAGE, LOCAL_TIMEZONE, PREFIX, STRINGS_TABLE}; +use crate::consts::{DEFAULT_PREFIX, LOCAL_LANGUAGE, LOCAL_TIMEZONE, STRINGS_TABLE}; pub struct GuildData { pub id: u32, @@ -37,12 +35,9 @@ SELECT prefix FROM guilds WHERE guild = ? .fetch_one(pool) .await; - row.map_or_else( - |_| env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()), - |r| r.prefix, - ) + row.map_or_else(|_| DEFAULT_PREFIX.clone(), |r| r.prefix) } else { - env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()) + DEFAULT_PREFIX.clone() } } @@ -70,7 +65,7 @@ INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?) ", guild_id, guild.name, - env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()) + *DEFAULT_PREFIX ) .execute(&pool.clone()) .await?;