prefix command

This commit is contained in:
jude 2020-08-29 20:57:11 +01:00
parent 58298de0be
commit 1f410899b2
2 changed files with 30 additions and 3 deletions

View File

@ -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::<SQLPool>().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(())
}

View File

@ -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<Self, Box<dyn std::error::Error + Sync + Send>> {
pub async fn from_guild(guild: Guild, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let guild_id = guild.id.as_u64().clone();
if let Ok(g) = sqlx::query_as!(Self,