From 095062f17d3e2ebbcf041c99cb8d841d1b4c3ce3 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 16 Feb 2022 20:04:49 -0800 Subject: [PATCH] Fix scenario where guild exists in file, but with no submissionChannel set --- src/commands/challenge.rs | 7 ++++--- src/commands/owner.rs | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/commands/challenge.rs b/src/commands/challenge.rs index f1d7751..cd95181 100644 --- a/src/commands/challenge.rs +++ b/src/commands/challenge.rs @@ -125,13 +125,14 @@ async fn submit(ctx: &Context, msg: &Message) -> CommandResult { // there are large duplicated sections that need to be merged somehow. let guild_data = get_guild_data(); let guild = msg.guild_id.unwrap().as_u64().to_string(); - if !guild_data.contains_key(&guild) { + let current_guild_data = &guild_data[&guild].as_object().unwrap(); + if !guild_data.contains_key(&guild) || !current_guild_data.contains_key("submissionChannel") { msg.reply(&ctx.http, "Submissions aren't enabled for this server yet.").await?; return Ok(()); } - let submission_channel = &guild_data[&guild].as_object().unwrap()["submissionChannel"].as_str().unwrap(); + let submission_channel = current_guild_data["submissionChannel"].as_str().unwrap(); if submission_channel != &msg.channel_id.as_u64().to_string() { - msg.reply(&ctx.http, format!("Sorry, submissions aren't permitted here. Please go to <#{}>. Thanks!", submission_channel)).await?; + msg.reply(&ctx.http, format!("Sorry, submissions aren't permitted here. Please go to <#{}>. Thanks!", guild)).await?; return Ok(()); } if msg.attachments.len() == 0 { diff --git a/src/commands/owner.rs b/src/commands/owner.rs index c4045e2..2bfb477 100644 --- a/src/commands/owner.rs +++ b/src/commands/owner.rs @@ -77,7 +77,13 @@ async fn setSubmissionChannel(ctx: &Context, msg: &Message) -> CommandResult { let guild = msg.guild_id.unwrap().as_u64().to_string(); let channel = msg.channel_id.as_u64().to_string(); if guild_data.contains_key(&guild) { - guild_data[&guild].as_object_mut().unwrap()["submissionChannel"] = channel.into(); + let mut current_guild_data = guild_data[&guild].as_object().unwrap().clone(); + if current_guild_data.contains_key("submissionChannel") { + current_guild_data["submissionChannel"] = channel.into(); + } else { + current_guild_data.insert(String::from("submissionChannel"), channel.into()); + } + guild_data[&guild] = current_guild_data.into(); } else { guild_data.insert(guild, json!({ "submissionChannel": channel