From af38f52d8d947d3305d88ca24eff9d127da2f2a4 Mon Sep 17 00:00:00 2001 From: ByteDice Date: Sun, 2 Mar 2025 12:38:48 +0100 Subject: [PATCH] enhanced the bk mod IDs security by using env instead of a file --- README.md | 7 ++++--- data/bk_mods.json | 29 ----------------------------- src/bk_week_cmds.rs | 16 ++++++---------- src/main.rs | 13 +++++++++---- 4 files changed, 19 insertions(+), 46 deletions(-) delete mode 100644 data/bk_mods.json diff --git a/README.md b/README.md index 53dfde2..a5cf63e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ An automation tool for Byte Dice. It's both a Discord and Reddit bot in one program. > [!CAUTION] -> This tool is not intended for public use outside of the official *Byte Dice Assistant* bots. Expect issues if you host this yourself. +> This tool is not intended for public use outside of the official *Byte Dice Assistant* bots. Expect issues if you host this yourself.\ +> This tool is only designed to run on Windows (10 and 11) and XUbuntu and may not work on any other OS. # Open-source - Copyright @@ -38,7 +39,7 @@ You can install Python modules by running `$ pip install {module}` or `$ python | `ASSISTANT_R_NAME` | The username of the Reddit bot/account. | | `ASSISTANT_R_PASS` | The password for the Reddit bot/account. | | `ASSISTANT_OWNERS` | (OPTIONAL) A list of Discord user IDs that "own" the bot. Separate each ID with a single comma and **no** spaces. This will allow the specified user IDs to run root commands such as `/stop`, it will also DM these users when *certain* errors occur. | -| `ASSISTANT_BK_WEEK_MODS` | (OPTIONAL) Same format as `ASSISTANT_OWNERS` but for people who are allowed to use the `/bk_week` commands. | +| `ASSISTANT_BK_MODS` | (OPTIONAL) Same format as `ASSISTANT_OWNERS` but for people who are allowed to use the `/bk_week` commands. | ### Required permissions: **These are automatically set if you use the [official invite link](https://discord.com/oauth2/authorize?client_id=1212127255795335208&permissions=84992&integration_type=0&scope=bot) or an invite link with the permissions integer set to `84992`.** @@ -56,7 +57,7 @@ You can install Python modules by running `$ pip install {module}` or `$ python * Run `$ setx VARIABLE_NAME "value"` in a terminal. * On Unix (Linux / Mac): - * Run `$ sudo nano /etc/environment` or `$ sudo vim /etc/environment` in the terminal. + * Run `$ sudo nano /etc/environment` or `$ sudo vim /etc/environment` in the terminal (and enter your password if prompted to). * press `i` (only if you used VIM). * write `VARIABLE_NAME="value"` + a new line for every variable. * if nano: `ctrl + O` (and press enter) then `ctrl + X`. diff --git a/data/bk_mods.json b/data/bk_mods.json deleted file mode 100644 index 430a035..0000000 --- a/data/bk_mods.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "bk1": { - "discord": [ - 697149665166229614, - 526842473062989864, - 1171015493537247273, - 1156779840956010496, - 1021944396473716847, - 1085622021024653362, - 1223480842525872148, - 1155349278655520858 - ], - "reddit": [ - - ] - }, - "bk2": { - "discord": [ - 1004132854227292272, - 1031352334745354282, - 1173039143245332520, - 1281040971710201857, - 772049174672703528 - ], - "reddit": [ - - ] - } -} \ No newline at end of file diff --git a/src/bk_week_cmds.rs b/src/bk_week_cmds.rs index c185cb5..4c2c7d1 100644 --- a/src/bk_week_cmds.rs +++ b/src/bk_week_cmds.rs @@ -26,12 +26,8 @@ enum TopCategory { } -fn is_bk_mod(mod_list: Value, uid: u64) -> bool { - let obj = mod_list.as_object().unwrap(); - let bk1_arr = obj["bk1"]["discord"].as_array().unwrap(); - let bk2_arr = obj["bk2"]["discord"].as_array().unwrap(); - - return bk1_arr.contains(&json!(uid)) || bk2_arr.contains(&json!(uid)); +fn is_bk_mod(mod_list: Vec, uid: u64) -> bool { + return mod_list.contains(&uid); } @@ -181,7 +177,7 @@ pub async fn bk_week_add( #[description = "Wether to approve it after adding it"] approve: Option ) -> Result<(), Error> { - if !is_bk_mod(ctx.data().bk_mods_json.clone(), ctx.author().id.get()) { + if !is_bk_mod(ctx.data().bk_mods.clone(), ctx.author().id.get()) { not_bk_mod_msg(ctx).await; return Ok(()); } @@ -253,7 +249,7 @@ pub async fn bk_week_remove( #[description = "The reason of the removal."] reason: Option ) -> Result<(), Error> { - if !is_bk_mod(ctx.data().bk_mods_json.clone(), ctx.author().id.get()) { + if !is_bk_mod(ctx.data().bk_mods.clone(), ctx.author().id.get()) { not_bk_mod_msg(ctx).await; return Ok(()); } @@ -291,7 +287,7 @@ pub async fn bk_week_approve( #[description = "Wether to approve or disapprove the post"] disapprove: Option ) -> Result<(), Error> { - if !is_bk_mod(ctx.data().bk_mods_json.clone(), ctx.author().id.get()) { + if !is_bk_mod(ctx.data().bk_mods.clone(), ctx.author().id.get()) { not_bk_mod_msg(ctx).await; return Ok(()); } @@ -694,7 +690,7 @@ pub async fn bk_week_vote( let url_data = &post_data[&url]; - let is_mod = is_bk_mod(ctx.data().bk_mods_json.clone(), ctx.author().id.get()); + let is_mod = is_bk_mod(ctx.data().bk_mods.clone(), ctx.author().id.get()); let voters_dc = url_data["votes"]["voters_dc"].as_array().unwrap(); let mod_voters = url_data["votes"]["mod_voters"].as_array().unwrap(); let voters = if is_mod { mod_voters } else { voters_dc }; diff --git a/src/main.rs b/src/main.rs index ae3c98b..da7676b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ struct Data { ball_prompts: [Vec; 2], reddit_data: Mutex>, discord_data: Mutex>, - bk_mods_json: Value, + bk_mods: Vec, args: Args } @@ -145,16 +145,21 @@ async fn start(args: Args, owners: Vec) { async fn gen_data(args: Args, owners: Vec) -> Data { let ball_classic_str = std::fs::read_to_string("./data/8-ball_classic.txt").unwrap(); let ball_quirk_str = std::fs::read_to_string("./data/8-ball_quirky.txt").unwrap(); - let bk_mods_str = std::fs::read_to_string("./data/bk_mods.json").unwrap(); let ball_classic: Vec = ball_classic_str.lines().map(String::from).collect(); let ball_quirk: Vec = ball_quirk_str .lines().map(String::from).collect(); - let bk_mods: Value = serde_json::from_str(&bk_mods_str).unwrap(); + + let mods_env = std::env::var("ASSISTANT_BK_MODS").expect("Missing ASSISTANT_BK_MODS env var!"); + let mods_vec_str: Vec = mods_env.split(",").map(String::from).collect(); + let mods_vec_u64: Vec = mods_vec_str + .iter() + .filter_map(|s| Some(s.parse::().expect("Failed to parse ASSISTANT_BK_MODS. Invalid syntax."))) + .collect(); let data = Data { owners, ball_prompts: [ball_classic, ball_quirk], - bk_mods_json: bk_mods, + bk_mods: mods_vec_u64, reddit_data: None.into(), discord_data: None.into(), args: args.clone()