made strings table name configurable

This commit is contained in:
jude 2020-10-12 23:17:47 +01:00
parent caf581083e
commit 03e5578dcb
3 changed files with 24 additions and 11 deletions

View File

@ -5,7 +5,7 @@ authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
serenity = { version = "0.9.0-rc.2", features = ["collector"] } serenity = { version = "0.9.0-rc.2", features = ["collector", "rustls_backend"] }
dotenv = "0.15" dotenv = "0.15"
tokio = { version = "0.2.19", features = ["process"] } tokio = { version = "0.2.19", features = ["process"] }
reqwest = "0.10.6" reqwest = "0.10.6"

View File

@ -45,4 +45,6 @@ lazy_static! {
env::var("LOCAL_LANGUAGE").unwrap_or_else(|_| "EN".to_string()); env::var("LOCAL_LANGUAGE").unwrap_or_else(|_| "EN".to_string());
pub static ref PYTHON_LOCATION: String = pub static ref PYTHON_LOCATION: String =
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 =
env::var("STRINGS_TABLE").unwrap_or_else(|_| "strings".to_string());
} }

View File

@ -5,12 +5,12 @@ use serenity::{
use std::env; use std::env;
use sqlx::MySqlPool; use sqlx::{Cursor, MySqlPool, Row};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use crate::consts::{LOCAL_LANGUAGE, PREFIX}; use crate::consts::{LOCAL_LANGUAGE, PREFIX, STRINGS_TABLE};
pub struct GuildData { pub struct GuildData {
pub id: u32, pub id: u32,
@ -249,16 +249,27 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
} }
pub async fn response(&self, pool: &MySqlPool, name: &str) -> String { pub async fn response(&self, pool: &MySqlPool, name: &str) -> String {
let row = sqlx::query!( let query_str = &format!(
" "
SELECT value FROM strings WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ? SELECT value FROM {} WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ?
", self.language, *LOCAL_LANGUAGE, name, *LOCAL_LANGUAGE) ",
.fetch_one(pool) *STRINGS_TABLE
.await );
.unwrap_or_else(|_| panic!("No string with that name: {}", name));
row.value let mut query = sqlx::query(&query_str)
.unwrap_or_else(|| panic!("Null string with that name: {}", name)) .bind(&self.language)
.bind(&*LOCAL_LANGUAGE)
.bind(name)
.bind(&*LOCAL_LANGUAGE)
.fetch(pool);
let row = query
.next()
.await
.unwrap_or_else(|e| panic!("Database error: {:?}", e))
.unwrap_or_else(|| panic!("No string with that name: {}", name));
row.get::<String, &str>("value").clone()
} }
pub fn timezone(&self) -> Tz { pub fn timezone(&self) -> Tz {