trait to send message from database

This commit is contained in:
jude 2020-08-29 18:07:15 +01:00
parent e88e4cc4f1
commit 58298de0be
4 changed files with 66 additions and 15 deletions

View File

@ -12,10 +12,7 @@ use serenity::{
use chrono_tz::Tz; use chrono_tz::Tz;
use chrono::{ use chrono::offset::Utc;
DateTime,
offset::Utc,
};
use crate::{ use crate::{
THEME_COLOR, THEME_COLOR,

View File

@ -20,6 +20,7 @@ use crate::{
UserData, UserData,
}, },
SQLPool, SQLPool,
framework::SendFromDb,
}; };
lazy_static! { lazy_static! {
@ -62,21 +63,26 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
match args.parse::<Tz>() { let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
Ok(_) => {
let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
user_data.timezone = args; if args.len() > 0 {
match args.parse::<Tz>() {
Ok(_) => {
user_data.timezone = args;
user_data.commit_changes(pool).await; user_data.commit_changes(pool).await;
let _ = msg.channel_id.say(&ctx, "Timezone changed").await; let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/set_p").await;
} }
Err(_) => { Err(_) => {
let _ = msg.channel_id.say(&ctx, "Unrecognised timezone").await; let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/no_timezone").await;
}
} }
} }
else {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "timezone/no_argument").await;
}
Ok(()) Ok(())
} }
@ -88,7 +94,25 @@ async fn language(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
user_data.commit_changes(pool).await; match sqlx::query!(
"
SELECT code FROM languages WHERE code = ? OR name = ?
", args, args)
.fetch_one(&pool)
.await {
Ok(row) => {
user_data.language = row.code;
user_data.commit_changes(pool).await;
let _ = msg.channel_id.say_named(&ctx, user_data.language, "lang/set_p").await;
},
Err(_) => {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "lang/invalid").await;
},
}
Ok(()) Ok(())
} }

View File

@ -1,12 +1,14 @@
use async_trait::async_trait; use async_trait::async_trait;
use serenity::{ use serenity::{
Result as SerenityResult,
client::Context, client::Context,
framework::{ framework::{
Framework, Framework,
standard::CommandResult, standard::CommandResult,
}, },
model::{ model::{
id::ChannelId,
guild::{ guild::{
Guild, Guild,
Member, Member,
@ -41,6 +43,33 @@ use crate::{
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>;
#[async_trait]
pub trait SendFromDb {
async fn say_named(&self, ctx: &&Context, language: String, name: &str) -> SerenityResult<Message>;
}
struct Value {
value: String,
}
#[async_trait]
impl SendFromDb for ChannelId {
async fn say_named(&self, ctx: &&Context, language: String, name: &str) -> SerenityResult<Message> {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let row = sqlx::query_as!(Value,
"
SELECT value FROM strings WHERE (language = ? OR language = 'EN') AND name = ? ORDER BY language = 'EN'
", language, name)
.fetch_one(&pool)
.await
.expect("No string with that name");
self.say(ctx, row.value).await
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum PermissionLevel { pub enum PermissionLevel {
Unrestricted, Unrestricted,

View File

@ -64,6 +64,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("todo", &todo_cmds::TODO_PARSE_COMMAND) .add_command("todo", &todo_cmds::TODO_PARSE_COMMAND)
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND) .add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
.add_command("language", &moderation_cmds::LANGUAGE_COMMAND)
.build(); .build();
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment")) let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))