cleared up remaining warnings. messed with prefix so that is it now configured by the program + env vars rather than the DB + magic strings

This commit is contained in:
jude 2020-09-28 13:42:20 +01:00
parent 4f3789aeed
commit 5ad143511e
5 changed files with 33 additions and 22 deletions

View File

@ -335,7 +335,10 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER
} }
} }
else { 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(()) Ok(())

1
src/consts.rs Normal file
View File

@ -0,0 +1 @@
pub const PREFIX: &str = "$";

View File

@ -32,11 +32,13 @@ use regex::{
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt, fmt,
env,
}; };
use crate::{ use crate::{
models::ChannelData, models::ChannelData,
SQLPool, SQLPool,
consts::PREFIX,
}; };
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, CommandResult>; type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, CommandResult>;
@ -147,13 +149,13 @@ 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: String::from("$"), default_prefix: env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()),
client_id, client_id,
ignore_bots: true, ignore_bots: true,
} }
} }
pub fn default_prefix(mut self, new_prefix: &str) -> Self { pub fn default_prefix<T: ToString>(mut self, new_prefix: T) -> Self {
self.default_prefix = new_prefix.to_string(); self.default_prefix = new_prefix.to_string();
self self

View File

@ -5,6 +5,7 @@ mod models;
mod framework; mod framework;
mod commands; mod commands;
mod time_parser; mod time_parser;
mod consts;
use serenity::{ use serenity::{
http::CacheHttp, http::CacheHttp,
@ -34,12 +35,15 @@ use std::{
env, env,
}; };
use crate::framework::RegexFramework; use crate::{
use crate::commands::{ framework::RegexFramework,
consts::PREFIX,
commands::{
info_cmds, info_cmds,
reminder_cmds, reminder_cmds,
todo_cmds, todo_cmds,
moderation_cmds, moderation_cmds,
},
}; };
struct SQLPool; struct SQLPool;
@ -68,7 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let framework = RegexFramework::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID from environment").parse()?) let framework = RegexFramework::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID from environment").parse()?)
.ignore_bots(true) .ignore_bots(true)
.default_prefix("$") .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)

View File

@ -8,14 +8,17 @@ use serenity::{
} }
}; };
use std::env;
use sqlx::MySqlPool; use sqlx::MySqlPool;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use crate::consts::PREFIX;
pub struct GuildData { pub struct GuildData {
pub id: u32, pub id: u32,
guild: u64,
pub name: String, pub name: String,
pub prefix: String, pub prefix: String,
} }
@ -33,10 +36,10 @@ SELECT prefix FROM guilds WHERE guild = ?
.fetch_one(pool) .fetch_one(pool)
.await; .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 { 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, 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) ", guild_id)
.fetch_one(pool) .fetch_one(pool)
.await { .await {
@ -55,14 +58,14 @@ SELECT id, guild, name, prefix FROM guilds WHERE guild = ?
else { else {
sqlx::query!( sqlx::query!(
" "
INSERT INTO guilds (guild, name) VALUES (?, ?) INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?)
", guild_id, guild.name) ", guild_id, guild.name, env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()))
.execute(&pool.clone()) .execute(&pool.clone())
.await?; .await?;
Ok(sqlx::query_as!(Self, Ok(sqlx::query_as!(Self,
" "
SELECT id, guild, name, prefix FROM guilds WHERE guild = ? SELECT id, name, prefix FROM guilds WHERE guild = ?
", guild_id) ", guild_id)
.fetch_one(pool) .fetch_one(pool)
.await?) .await?)
@ -81,7 +84,6 @@ UPDATE guilds SET name = ?, prefix = ? WHERE id = ?
pub struct ChannelData { pub struct ChannelData {
pub id: u32, pub id: u32,
channel: u64,
pub name: String, pub name: String,
pub nudge: i16, pub nudge: i16,
pub blacklisted: bool, pub blacklisted: bool,
@ -89,14 +91,13 @@ pub struct ChannelData {
pub webhook_token: Option<String>, pub webhook_token: Option<String>,
pub paused: bool, pub paused: bool,
pub paused_until: Option<NaiveDateTime>, pub paused_until: Option<NaiveDateTime>,
guild_id: u32,
} }
impl ChannelData { impl ChannelData {
pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option<Self> { pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option<Self> {
sqlx::query_as_unchecked!(Self, 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) ", channel_id)
.fetch_one(pool) .fetch_one(pool)
.await.ok() .await.ok()
@ -109,7 +110,7 @@ SELECT * FROM channels WHERE channel = ?
if let Ok(c) = sqlx::query_as_unchecked!(Self, 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) ", channel_id)
.fetch_one(pool) .fetch_one(pool)
.await { .await {
@ -134,7 +135,7 @@ INSERT INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM gui
Ok(sqlx::query_as_unchecked!(Self, 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) ", channel_id)
.fetch_one(pool) .fetch_one(pool)
.await?) .await?)