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 {
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(())

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::{
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<T: ToString>(mut self, new_prefix: T) -> Self {
self.default_prefix = new_prefix.to_string();
self

View File

@ -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<dyn std::error::Error + Send + Sync>> {
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)

View File

@ -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<String>,
pub paused: bool,
pub paused_until: Option<NaiveDateTime>,
guild_id: u32,
}
impl ChannelData {
pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option<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)
.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?)