diff --git a/data/discord_data_preset.json b/data/discord_data_preset.json index e5dccbf..cf97f26 100644 --- a/data/discord_data_preset.json +++ b/data/discord_data_preset.json @@ -2,7 +2,8 @@ "servers": { "SERVER ID": { "bk_week_channel": "CHANNEL ID INT", - "bk_week_users": [ + "bk_mod_role": "bk mod", + "bk_mods": [ "USER ID 1", "USER ID 2" ] diff --git a/src/bk_week_cmds.rs b/src/bk_week_cmds.rs index 10a0bab..f027f25 100644 --- a/src/bk_week_cmds.rs +++ b/src/bk_week_cmds.rs @@ -1,7 +1,7 @@ use crate::websocket::send_cmd_json; use crate::{rs_println, websocket, Context, Error, BK_WEEK}; use crate::messages::{send_embed, send_msg, EmbedOptions}; -use crate::data; +use crate::data::{self, dc_bind_bk}; use std::fs; @@ -16,7 +16,7 @@ enum HelpOptions { } -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_help( ctx: Context<'_>, #[description = "Discord or Reddit help."] option: HelpOptions @@ -35,7 +35,7 @@ pub async fn bk_week_help( } send_msg(ctx, help, true, true).await; - data::read_dc_data(ctx.data()); + data::read_dc_data(ctx.data(), false); return Ok(()); } @@ -43,7 +43,7 @@ pub async fn bk_week_help( -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_get( ctx: Context<'_>, #[description = "The post URL"] url: String @@ -162,7 +162,7 @@ async fn send_data_corrupted_message(ctx: Context<'_>, url: &str) { -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_add( ctx: Context<'_>, #[description = "The post URL"] url: String, @@ -214,7 +214,7 @@ async fn send_updated_msg(ctx: Context<'_>, url: &str) { -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_remove( ctx: Context<'_>, #[description = "The post URL"] url: String @@ -241,7 +241,7 @@ pub async fn bk_week_remove( -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_approve( ctx: Context<'_>, #[description = "The post URL"] url: String @@ -263,7 +263,7 @@ async fn approve_cmd(ctx: Context<'_>, url: &str, reddit_data: &Value, approve: } let r = websocket::send_cmd_json("set_approve_post", json!([approve, &url])).await.unwrap(); - if let Some(v) = r.get("value") { + if r.get("value").is_some() { if approve { send_msg(ctx, format!("Successfully flagged URL \"<{}>\" as `approved:by_human`!", &url), true, true).await; } @@ -282,7 +282,7 @@ async fn approve_cmd(ctx: Context<'_>, url: &str, reddit_data: &Value, approve: -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, guild_only)] pub async fn bk_week_disapprove( ctx: Context<'_>, #[description = "The post URL"] url: String @@ -293,5 +293,24 @@ pub async fn bk_week_disapprove( approve_cmd(ctx, &url, &reddit_data, false).await; + return Ok(()); +} + + +#[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR", guild_only)] +pub async fn bk_week_bind( + ctx: Context<'_> +) -> Result<(), Error> +{ + let c_id = ctx.channel_id().into(); + let r = dc_bind_bk(ctx.data(), ctx.guild_id().unwrap().into(), c_id); + + if r { + send_msg(ctx, format!("Successfully bound channel ID `{}` as the bk_week channel!", c_id), true, true).await; + } + else { + send_msg(ctx, "Your server is not in the data!\nHint: Run the command `/add_server` inside of a Discord server.".to_string(), true, true).await; + } + return Ok(()); } \ No newline at end of file diff --git a/src/cmds.rs b/src/cmds.rs index 5838504..0e38a20 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -1,5 +1,6 @@ use std::process; +use crate::data::dc_add_server; use crate::websocket::send_cmd_json; use crate::{data, Context, Error}; use crate::messages::{send_embed, send_msg, edit_msg, EmbedOptions}; @@ -210,6 +211,24 @@ pub async fn re_shorturl( } +#[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR", guild_only)] +pub async fn add_server( + ctx: Context<'_> +) -> Result<(), Error> +{ + let r = dc_add_server(ctx.data(), ctx.guild_id().unwrap().into()); + + if r { + send_msg(ctx, "Added your server to my data! Thanks for letting me steal it! (/s)".to_string(), true, true).await; + } + else { + send_msg(ctx, "Oopsies `(。>\\\\<)`. It looks like my data i-is \\**sob*\\*... c-cor-corrupted!".to_string(), true, true).await; + } + + return Ok(()); +} + + /* async fn autocomplete_rule_list(_: Context<'_>, _partial: &str) -> Vec { let json_str = std::fs::read_to_string("./data/write_json.json") .expect("No JSON preset file exists."); diff --git a/src/data.rs b/src/data.rs index cf55bcd..41bbefe 100644 --- a/src/data.rs +++ b/src/data.rs @@ -13,8 +13,12 @@ static DATA_PATH_RE: &str = "./data/reddit_data.json"; static PRESET_PATH_RE: &str = "./data/reddit_data_preset.json"; -pub fn read_dc_data(data: &Data) { - if !Path::new(DATA_PATH_DC).exists() { +pub fn read_dc_data(data: &Data, wipe: bool) { + if !Path::new(DATA_PATH_DC).exists() || wipe { + rs_println!( + "{} creating new from preset...", + if !wipe { "discord_data.json not found," } else { "[WIPE] (discord_data.json)" } + ); generate_dc_data(); } @@ -97,4 +101,40 @@ pub async fn update_re_data(data: &Data) { pub async fn write_re_data() { send_cmd_json("update_data_file", json!([])).await; +} + + +pub fn dc_add_server(data: &Data, server_id: u64) -> bool { + let mut dc_data_lock = data.discord_data.lock().unwrap(); + let dc_data = dc_data_lock.as_mut().unwrap(); + + if dc_data.get("servers").is_none() { return false; } + + let servers = dc_data["servers"].as_object_mut().unwrap(); + + if !servers.contains_key(&server_id.to_string()) { + servers.insert(server_id.to_string(), json!({ "bk_week_channel": 0, "bk_mod_role": "bk mod", "bk_mods": [] })); + } + + return true; +} + + +pub fn dc_bind_bk(data: &Data, server_id: u64, channel_id: u64) -> bool { + let mut dc_data_lock = data.discord_data.lock().unwrap(); + let dc_data = dc_data_lock.as_mut().unwrap(); + + if dc_data.get("servers").is_none() { return false; } + + let servers = dc_data["servers"].as_object_mut().unwrap(); + + if !servers.contains_key(&server_id.to_string()) { + return false; + } + + let server = servers[&server_id.to_string()].as_object_mut().unwrap(); + + server.insert("bk_week_channel".to_string(), channel_id.into()); + + return true; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index af9ef33..ee33f56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ #![warn(unused_extern_crates)] mod cmds; -#[allow(unused_variables)] mod bk_week_cmds; mod events; mod messages; @@ -23,6 +22,11 @@ use tokio::runtime::Runtime; use serde_json; +// TODO: command descriptions +// TODO: bot command permissions +// TODO: bk_mod verification system + + #[derive(Parser, Serialize, Clone)] struct Args { #[arg(short = 'p', long, default_value = "2920", help = "Sets the port number, e.g 2200.")] @@ -116,7 +120,7 @@ fn gen_data(args: Args) -> Data { args: args.clone() }; - data::read_dc_data(&data); + data::read_dc_data(&data, args.clone().wipe); data::read_re_data(&data, args.clone().wipe); return data; @@ -142,13 +146,15 @@ async fn gen_bot(data: Data) -> Client { cmds::eight_ball(), cmds::write_json(), cmds::re_shorturl(), + cmds::add_server(), //cmds::rule(), bk_week_cmds::bk_week_help(), bk_week_cmds::bk_week_get(), bk_week_cmds::bk_week_add(), bk_week_cmds::bk_week_remove(), bk_week_cmds::bk_week_approve(), - bk_week_cmds::bk_week_disapprove() + bk_week_cmds::bk_week_disapprove(), + bk_week_cmds::bk_week_bind() ], event_handler: events::event_handler, ..Default::default()