moved around the consts so that default prefix env var isnt being read into the framework. made theme color configurable

This commit is contained in:
jude 2020-10-13 14:35:13 +01:00
parent cfdcab4f01
commit ceac7be083
6 changed files with 37 additions and 25 deletions

View File

@ -32,3 +32,4 @@ __Other Variables__
* `IGNORE_BOTS` - default `1`, if `1`, Reminder Bot will ignore all other bots * `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 * `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) * `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

View File

@ -45,7 +45,7 @@ async fn help(ctx: &Context, msg: &Message, _args: String) -> CommandResult {
msg.channel_id msg.channel_id
.send_message(ctx, |m| { .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?; .await?;
@ -75,7 +75,7 @@ async fn info(ctx: &Context, msg: &Message, _args: String) -> CommandResult {
msg.channel_id msg.channel_id
.send_message(ctx, |m| { .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?; .await?;
@ -97,7 +97,7 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) -> CommandResult {
msg.channel_id msg.channel_id
.send_message(ctx, |m| { .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?; .await?;
@ -111,7 +111,7 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) -> CommandResult
m.embed(move |e| { m.embed(move |e| {
e.title("Dashboard") e.title("Dashboard")
.description("https://reminder-bot.com/dashboard") .description("https://reminder-bot.com/dashboard")
.color(THEME_COLOR) .color(*THEME_COLOR)
}) })
}) })
.await?; .await?;

View File

@ -1,11 +1,11 @@
pub const PREFIX: &str = "$";
pub const DAY: u64 = 86_400; pub const DAY: u64 = 86_400;
pub const HOUR: u64 = 3_600; pub const HOUR: u64 = 3_600;
pub const MINUTE: u64 = 60; pub const MINUTE: u64 = 60;
pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
const THEME_COLOR_FALLBACK: u32 = 0x8fb677;
use std::{collections::HashSet, env, iter::FromIterator}; use std::{collections::HashSet, env, iter::FromIterator};
use regex::Regex; use regex::Regex;
@ -47,4 +47,10 @@ lazy_static! {
env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string()); env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string());
pub static ref STRINGS_TABLE: String = pub static ref STRINGS_TABLE: String =
env::var("STRINGS_TABLE").unwrap_or_else(|_| "strings".to_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)
);
} }

View File

@ -18,9 +18,9 @@ use log::{error, info, warn};
use regex::{Match, Regex}; 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; use serenity::futures::TryFutureExt;
type CommandFn = type CommandFn =
@ -203,7 +203,7 @@ impl RegexFramework {
commands: HashMap::new(), commands: HashMap::new(),
command_matcher: Regex::new(r#"^$"#).unwrap(), command_matcher: Regex::new(r#"^$"#).unwrap(),
dm_regex_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(), client_id: client_id.into(),
ignore_bots: true, ignore_bots: true,
} }
@ -315,7 +315,12 @@ impl Framework for RegexFramework {
}) })
} }
async fn check_prefix(ctx: &Context, guild: &Guild, prefix_opt: Option<Match<'_>>) -> bool { async fn check_prefix(
ctx: &Context,
guild: &Guild,
prefix_opt: Option<Match<'_>>,
default_prefix: &String,
) -> bool {
if let Some(prefix) = prefix_opt { if let Some(prefix) = prefix_opt {
let pool = ctx let pool = ctx
.data .data
@ -343,7 +348,7 @@ impl Framework for RegexFramework {
.execute(&pool) .execute(&pool)
.await; .await;
prefix.as_str() == "$" prefix.as_str() == default_prefix.as_str()
} }
Err(e) => { Err(e) => {
@ -371,7 +376,14 @@ impl Framework for RegexFramework {
let member = guild.member(&ctx, &msg.author).await.unwrap(); let member = guild.member(&ctx, &msg.author).await.unwrap();
if let Some(full_match) = self.command_matcher.captures(&msg.content[..]) { 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 { match check_self_permissions(&ctx, &guild, &channel).await {
Ok(perms) => match perms { Ok(perms) => match perms {
PermissionCheck::All => { PermissionCheck::All => {

View File

@ -30,7 +30,7 @@ use std::{env, sync::Arc};
use crate::{ use crate::{
commands::{info_cmds, moderation_cmds, reminder_cmds, todo_cmds}, 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, framework::RegexFramework,
}; };
@ -54,8 +54,6 @@ impl TypeMapKey for FrameworkCtx {
type Value = Arc<Box<dyn Framework + Send + Sync>>; type Value = Arc<Box<dyn Framework + Send + Sync>>;
} }
static THEME_COLOR: u32 = 0x8fb677;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
dotenv()?; dotenv()?;
@ -70,8 +68,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.await?; .await?;
let framework = RegexFramework::new(logged_in_id) let framework = RegexFramework::new(logged_in_id)
.default_prefix(DEFAULT_PREFIX.clone())
.ignore_bots(env::var("IGNORE_BOTS").map_or(true, |var| var == "1")) .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("ping", &info_cmds::PING_COMMAND)
.add_command("help", &info_cmds::HELP_COMMAND) .add_command("help", &info_cmds::HELP_COMMAND)
.add_command("info", &info_cmds::INFO_COMMAND) .add_command("info", &info_cmds::INFO_COMMAND)

View File

@ -3,8 +3,6 @@ use serenity::{
model::{channel::Channel, guild::Guild, id::GuildId, user::User}, model::{channel::Channel, guild::Guild, id::GuildId, user::User},
}; };
use std::env;
use sqlx::{Cursor, MySqlPool, Row}; use sqlx::{Cursor, MySqlPool, Row};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
@ -12,7 +10,7 @@ use chrono_tz::Tz;
use log::error; 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 struct GuildData {
pub id: u32, pub id: u32,
@ -37,12 +35,9 @@ SELECT prefix FROM guilds WHERE guild = ?
.fetch_one(pool) .fetch_one(pool)
.await; .await;
row.map_or_else( row.map_or_else(|_| DEFAULT_PREFIX.clone(), |r| r.prefix)
|_| env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()),
|r| r.prefix,
)
} else { } 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_id,
guild.name, guild.name,
env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()) *DEFAULT_PREFIX
) )
.execute(&pool.clone()) .execute(&pool.clone())
.await?; .await?;