moved create_from_guild to the prefix handler.

This commit is contained in:
jude-lafitteIII 2020-05-29 16:13:20 +01:00
parent d6d825e86d
commit b4db99934d
2 changed files with 27 additions and 17 deletions

View File

@ -36,19 +36,19 @@ SELECT id, name, prefix, volume, allow_greets
pub async fn create_from_guild(guild: Guild, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> {
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?;

View File

@ -339,10 +339,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pool = ctx.data.read().await
.get::<SQLPool>().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::<SQLPool>().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();
}