From 5ad143511e79d1f1c0a997438afb67e541853043 Mon Sep 17 00:00:00 2001 From: jude Date: Mon, 28 Sep 2020 13:42:20 +0100 Subject: [PATCH] cleared up remaining warnings. messed with prefix so that is it now configured by the program + env vars rather than the DB + magic strings --- src/commands/moderation_cmds.rs | 5 ++++- src/consts.rs | 1 + src/framework.rs | 6 ++++-- src/main.rs | 18 +++++++++++------- src/models.rs | 25 +++++++++++++------------ 5 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 src/consts.rs diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index b518f54..3dbc01d 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -335,7 +335,10 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER } } else { - let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "alias/help").await).await; + let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; + let content = user_data.response(&pool, "alias/help").await.replace("{prefix}", &prefix); + + let _ = msg.channel_id.say(&ctx, content).await; } Ok(()) diff --git a/src/consts.rs b/src/consts.rs new file mode 100644 index 0000000..705edd3 --- /dev/null +++ b/src/consts.rs @@ -0,0 +1 @@ +pub const PREFIX: &str = "$"; diff --git a/src/framework.rs b/src/framework.rs index b8c1911..b009e61 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -32,11 +32,13 @@ use regex::{ use std::{ collections::HashMap, fmt, + env, }; use crate::{ models::ChannelData, SQLPool, + consts::PREFIX, }; type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, CommandResult>; @@ -147,13 +149,13 @@ impl RegexFramework { commands: HashMap::new(), command_matcher: Regex::new(r#"^$"#).unwrap(), dm_regex_matcher: Regex::new(r#"^$"#).unwrap(), - default_prefix: String::from("$"), + default_prefix: env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()), client_id, ignore_bots: true, } } - pub fn default_prefix(mut self, new_prefix: &str) -> Self { + pub fn default_prefix(mut self, new_prefix: T) -> Self { self.default_prefix = new_prefix.to_string(); self diff --git a/src/main.rs b/src/main.rs index 7cdf2e9..c608f33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod models; mod framework; mod commands; mod time_parser; +mod consts; use serenity::{ http::CacheHttp, @@ -34,12 +35,15 @@ use std::{ env, }; -use crate::framework::RegexFramework; -use crate::commands::{ - info_cmds, - reminder_cmds, - todo_cmds, - moderation_cmds, +use crate::{ + framework::RegexFramework, + consts::PREFIX, + commands::{ + info_cmds, + reminder_cmds, + todo_cmds, + moderation_cmds, + }, }; struct SQLPool; @@ -68,7 +72,7 @@ async fn main() -> Result<(), Box> { let framework = RegexFramework::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID from environment").parse()?) .ignore_bots(true) - .default_prefix("$") + .default_prefix(&env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string())) .add_command("ping", &info_cmds::PING_COMMAND) diff --git a/src/models.rs b/src/models.rs index 7e78d0c..327bbb6 100644 --- a/src/models.rs +++ b/src/models.rs @@ -8,14 +8,17 @@ use serenity::{ } }; +use std::env; + use sqlx::MySqlPool; use chrono::NaiveDateTime; use chrono_tz::Tz; +use crate::consts::PREFIX; + pub struct GuildData { pub id: u32, - guild: u64, pub name: String, pub prefix: String, } @@ -33,10 +36,10 @@ SELECT prefix FROM guilds WHERE guild = ? .fetch_one(pool) .await; - row.map_or("$".to_string(), |r| r.prefix) + row.map_or_else(|_| env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()), |r| r.prefix) } else { - "$".to_string() + env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()) } } @@ -45,7 +48,7 @@ SELECT prefix FROM guilds WHERE guild = ? if let Ok(g) = sqlx::query_as!(Self, " -SELECT id, guild, name, prefix FROM guilds WHERE guild = ? +SELECT id, name, prefix FROM guilds WHERE guild = ? ", guild_id) .fetch_one(pool) .await { @@ -55,14 +58,14 @@ SELECT id, guild, name, prefix FROM guilds WHERE guild = ? else { sqlx::query!( " -INSERT INTO guilds (guild, name) VALUES (?, ?) - ", guild_id, guild.name) +INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?) + ", guild_id, guild.name, env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string())) .execute(&pool.clone()) .await?; Ok(sqlx::query_as!(Self, " -SELECT id, guild, name, prefix FROM guilds WHERE guild = ? +SELECT id, name, prefix FROM guilds WHERE guild = ? ", guild_id) .fetch_one(pool) .await?) @@ -81,7 +84,6 @@ UPDATE guilds SET name = ?, prefix = ? WHERE id = ? pub struct ChannelData { pub id: u32, - channel: u64, pub name: String, pub nudge: i16, pub blacklisted: bool, @@ -89,14 +91,13 @@ pub struct ChannelData { pub webhook_token: Option, pub paused: bool, pub paused_until: Option, - guild_id: u32, } impl ChannelData { pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option { sqlx::query_as_unchecked!(Self, " -SELECT * FROM channels WHERE channel = ? +SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ? ", channel_id) .fetch_one(pool) .await.ok() @@ -109,7 +110,7 @@ SELECT * FROM channels WHERE channel = ? if let Ok(c) = sqlx::query_as_unchecked!(Self, " -SELECT * FROM channels WHERE channel = ? +SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ? ", channel_id) .fetch_one(pool) .await { @@ -134,7 +135,7 @@ INSERT INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM gui Ok(sqlx::query_as_unchecked!(Self, " -SELECT * FROM channels WHERE channel = ? +SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ? ", channel_id) .fetch_one(pool) .await?)