From 1f410899b25abe4472af7ba3e84173d49d9b98b4 Mon Sep 17 00:00:00 2001 From: jude Date: Sat, 29 Aug 2020 20:57:11 +0100 Subject: [PATCH] prefix command --- src/commands/moderation_cmds.rs | 27 +++++++++++++++++++++++++++ src/models.rs | 6 +++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 0c521da..b91042c 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -18,6 +18,7 @@ use crate::{ models::{ ChannelData, UserData, + GuildData, }, SQLPool, framework::SendFromDb, @@ -116,3 +117,29 @@ SELECT code FROM languages WHERE code = ? OR name = ? Ok(()) } + +#[command] +async fn prefix(ctx: &Context, msg: &Message, args: String) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), pool.clone()).await.unwrap(); + let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); + + if args.len() > 5 { + let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/too_long").await; + + } + else if args.len() == 0 { + let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/no_argument").await; + } + else { + guild_data.prefix = args; + + guild_data.commit_changes(pool).await; + + let _ = msg.channel_id.say_named(&ctx, user_data.language, "prefix/success").await; + } + + Ok(()) +} diff --git a/src/models.rs b/src/models.rs index 80304ca..a74e6a2 100644 --- a/src/models.rs +++ b/src/models.rs @@ -14,12 +14,12 @@ use chrono::NaiveDateTime; pub struct GuildData { id: u32, guild: u64, - name: String, - prefix: String, + pub name: String, + pub prefix: String, } impl GuildData { - pub async fn from_id(guild: Guild, pool: MySqlPool) -> Result> { + pub async fn from_guild(guild: Guild, pool: MySqlPool) -> Result> { let guild_id = guild.id.as_u64().clone(); if let Ok(g) = sqlx::query_as!(Self,