From b4db99934dcc6fb2214ca0af6b598038ba335634 Mon Sep 17 00:00:00 2001 From: jude-lafitteIII Date: Fri, 29 May 2020 16:13:20 +0100 Subject: [PATCH] moved create_from_guild to the prefix handler. --- src/guilddata.rs | 10 +++++----- src/main.rs | 34 ++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/guilddata.rs b/src/guilddata.rs index a4e2d7e..38203a8 100644 --- a/src/guilddata.rs +++ b/src/guilddata.rs @@ -36,19 +36,19 @@ SELECT id, name, prefix, volume, allow_greets pub async fn create_from_guild(guild: Guild, db_pool: MySqlPool) -> Result> { sqlx::query!( " -INSERT INTO roles (guild_id, role) +INSERT INTO servers (id, name) VALUES (?, ?) - ", - guild.id.as_u64(), guild.id.as_u64() + ", guild.id.as_u64(), guild.name ) .execute(&db_pool) .await?; sqlx::query!( " -INSERT INTO servers (id, name) +INSERT INTO roles (guild_id, role) VALUES (?, ?) - ", guild.id.as_u64(), guild.name + ", + guild.id.as_u64(), guild.id.as_u64() ) .execute(&db_pool) .await?; diff --git a/src/main.rs b/src/main.rs index 42a41a3..d61c2a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -339,10 +339,29 @@ async fn main() -> Result<(), Box> { let pool = ctx.data.read().await .get::().cloned().expect("Could not get SQLPool from data"); - match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool).await { - Some(guild) => Some(guild.prefix), + let guild = match msg.guild(&ctx.cache).await { + Some(guild) => guild, - None => Some(String::from("?")) + None => { + return Some(String::from("?")); + } + }; + + match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool.clone()).await { + Some(mut guild_data) => { + let name = Some(guild.name); + + if guild_data.name != name { + guild_data.name = name; + guild_data.commit(pool).await.unwrap(); + } + Some(guild_data.prefix) + }, + + None => { + GuildData::create_from_guild(guild, pool).await.unwrap(); + Some(String::from("?")) + } } })) .allow_dm(false) @@ -539,11 +558,6 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR .get::().cloned().expect("Could not get SQLPool from data"); let mut guild_data_opt = GuildData::get_from_id(*guild.id.as_u64(), pool.clone()).await; - - if guild_data_opt.is_none() { - guild_data_opt = Some(GuildData::create_from_guild(guild, pool.clone()).await.unwrap()) - } - let mut guild_data = guild_data_opt.unwrap(); if args.len() == 1 { @@ -590,10 +604,6 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR { let mut guild_data_opt = GuildData::get_from_id(*guild.id.as_u64(), pool.clone()).await; - if guild_data_opt.is_none() { - guild_data_opt = Some(GuildData::create_from_guild(guild, pool.clone()).await.unwrap()) - } - guild_data = guild_data_opt.unwrap(); }