trait to send message from database
This commit is contained in:
parent
e88e4cc4f1
commit
58298de0be
@ -12,10 +12,7 @@ use serenity::{
|
||||
|
||||
use chrono_tz::Tz;
|
||||
|
||||
use chrono::{
|
||||
DateTime,
|
||||
offset::Utc,
|
||||
};
|
||||
use chrono::offset::Utc;
|
||||
|
||||
use crate::{
|
||||
THEME_COLOR,
|
||||
|
@ -20,6 +20,7 @@ use crate::{
|
||||
UserData,
|
||||
},
|
||||
SQLPool,
|
||||
framework::SendFromDb,
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
@ -62,21 +63,26 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult {
|
||||
let pool = ctx.data.read().await
|
||||
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
|
||||
|
||||
match args.parse::<Tz>() {
|
||||
Ok(_) => {
|
||||
let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
|
||||
|
||||
if args.len() > 0 {
|
||||
match args.parse::<Tz>() {
|
||||
Ok(_) => {
|
||||
user_data.timezone = args;
|
||||
|
||||
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(_) => {
|
||||
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(())
|
||||
}
|
||||
@ -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();
|
||||
|
||||
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(())
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use serenity::{
|
||||
Result as SerenityResult,
|
||||
client::Context,
|
||||
framework::{
|
||||
Framework,
|
||||
standard::CommandResult,
|
||||
},
|
||||
model::{
|
||||
id::ChannelId,
|
||||
guild::{
|
||||
Guild,
|
||||
Member,
|
||||
@ -41,6 +43,33 @@ use crate::{
|
||||
|
||||
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)]
|
||||
pub enum PermissionLevel {
|
||||
Unrestricted,
|
||||
|
@ -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("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
|
||||
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
|
||||
.add_command("language", &moderation_cmds::LANGUAGE_COMMAND)
|
||||
.build();
|
||||
|
||||
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))
|
||||
|
Loading…
Reference in New Issue
Block a user