From 15ab2de3651331a4b0096eab14f9dedbad829c5d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 16 Feb 2022 19:32:39 -0800 Subject: [PATCH] Add setSubmissionChannel command --- .gitignore | 3 +- src/commands/owner.rs | 67 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e8f034f..b0fe617 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk -.env \ No newline at end of file +.env +guilds.json \ No newline at end of file diff --git a/src/commands/owner.rs b/src/commands/owner.rs index 875c885..260a320 100644 --- a/src/commands/owner.rs +++ b/src/commands/owner.rs @@ -2,8 +2,55 @@ use serenity::framework::standard::{macros::command, CommandResult}; use serenity::model::prelude::*; use serenity::prelude::*; +use serde_json::Map; +use serde_json::Value; +use serde_json::json; +use std::fs::File; +use std::fs::OpenOptions; +use std::io::Read; +use std::io::Write; +use std::path::Path; +use std::process::Command; + use crate::ShardManagerContainer; +const GUILD_DATA_PATH: &str = "guilds.json"; + +fn get_guild_data() -> Map { + let guild_data_json = match File::open(GUILD_DATA_PATH) { + Ok(mut file) => { + let mut json = String::new(); + file.read_to_string(&mut json).unwrap(); + json + } + Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { + let mut file = File::create(GUILD_DATA_PATH).unwrap(); + file.write_all(b"{}").unwrap(); + file.flush().unwrap(); + String::from("{}") + } + Err(_) => panic!("Failed to open guild data file"), + }; + let mut submission_data: Value = serde_json::from_str(&guild_data_json).unwrap(); + submission_data.as_object_mut().unwrap().clone() +} + +fn set_guild_data(guild_data: Map) { + let guild_data: Value = guild_data.into(); + let mut guild_data_file = OpenOptions::new() + .write(true) + .truncate(true) + .open(GUILD_DATA_PATH) + .unwrap(); + guild_data_file + .write_all( + serde_json::to_string_pretty(&guild_data) + .unwrap() + .as_bytes(), + ) + .unwrap(); +} + #[command] #[owners_only] async fn sleep(ctx: &Context, msg: &Message) -> CommandResult { @@ -21,3 +68,23 @@ async fn sleep(ctx: &Context, msg: &Message) -> CommandResult { Ok(()) } + +#[command] +#[owners_only] +#[allow(non_snake_case)] +async fn setSubmissionChannel(ctx: &Context, msg: &Message) -> CommandResult { + let mut guild_data = get_guild_data(); + 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(); + } else { + guild_data.insert(guild, json!({ + "submissionChannel": channel + })); + } + set_guild_data(guild_data); + // TODO: Add guild name in message + msg.reply(&ctx.http, format!("Submission channel set to <#{}>.", msg.channel_id)).await?; + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6c20bfa..3e8c62d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,8 @@ impl EventHandler for Handler { images, imageDelete, help, - sleep + sleep, + setSubmissionChannel )] struct General;