enhanced the bk mod IDs security by using env instead of a file

This commit is contained in:
2025-03-02 12:38:48 +01:00
parent ab02ac1cf6
commit af38f52d8d
4 changed files with 19 additions and 46 deletions
+4 -3
View File
@@ -2,7 +2,8 @@
An automation tool for Byte Dice. It's both a Discord and Reddit bot in one program. An automation tool for Byte Dice. It's both a Discord and Reddit bot in one program.
> [!CAUTION] > [!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 # 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_NAME` | The username of the Reddit bot/account. |
| `ASSISTANT_R_PASS` | The password for 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_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: ### 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`.** **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. * Run `$ setx VARIABLE_NAME "value"` in a terminal.
* On Unix (Linux / Mac): * 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). * press `i` (only if you used VIM).
* write `VARIABLE_NAME="value"` + a new line for every variable. * write `VARIABLE_NAME="value"` + a new line for every variable.
* if nano: `ctrl + O` (and press enter) then `ctrl + X`. * if nano: `ctrl + O` (and press enter) then `ctrl + X`.
-29
View File
@@ -1,29 +0,0 @@
{
"bk1": {
"discord": [
697149665166229614,
526842473062989864,
1171015493537247273,
1156779840956010496,
1021944396473716847,
1085622021024653362,
1223480842525872148,
1155349278655520858
],
"reddit": [
]
},
"bk2": {
"discord": [
1004132854227292272,
1031352334745354282,
1173039143245332520,
1281040971710201857,
772049174672703528
],
"reddit": [
]
}
}
+6 -10
View File
@@ -26,12 +26,8 @@ enum TopCategory {
} }
fn is_bk_mod(mod_list: Value, uid: u64) -> bool { fn is_bk_mod(mod_list: Vec<u64>, uid: u64) -> bool {
let obj = mod_list.as_object().unwrap(); return mod_list.contains(&uid);
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));
} }
@@ -181,7 +177,7 @@ pub async fn bk_week_add(
#[description = "Wether to approve it after adding it"] approve: Option<bool> #[description = "Wether to approve it after adding it"] approve: Option<bool>
) -> Result<(), Error> ) -> 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; not_bk_mod_msg(ctx).await;
return Ok(()); return Ok(());
} }
@@ -253,7 +249,7 @@ pub async fn bk_week_remove(
#[description = "The reason of the removal."] reason: Option<String> #[description = "The reason of the removal."] reason: Option<String>
) -> Result<(), Error> ) -> 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; not_bk_mod_msg(ctx).await;
return Ok(()); return Ok(());
} }
@@ -291,7 +287,7 @@ pub async fn bk_week_approve(
#[description = "Wether to approve or disapprove the post"] disapprove: Option<bool> #[description = "Wether to approve or disapprove the post"] disapprove: Option<bool>
) -> Result<(), Error> ) -> 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; not_bk_mod_msg(ctx).await;
return Ok(()); return Ok(());
} }
@@ -694,7 +690,7 @@ pub async fn bk_week_vote(
let url_data = &post_data[&url]; 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 voters_dc = url_data["votes"]["voters_dc"].as_array().unwrap();
let mod_voters = url_data["votes"]["mod_voters"].as_array().unwrap(); let mod_voters = url_data["votes"]["mod_voters"].as_array().unwrap();
let voters = if is_mod { mod_voters } else { voters_dc }; let voters = if is_mod { mod_voters } else { voters_dc };
+9 -4
View File
@@ -62,7 +62,7 @@ struct Data {
ball_prompts: [Vec<String>; 2], ball_prompts: [Vec<String>; 2],
reddit_data: Mutex<Option<Value>>, reddit_data: Mutex<Option<Value>>,
discord_data: Mutex<Option<Value>>, discord_data: Mutex<Option<Value>>,
bk_mods_json: Value, bk_mods: Vec<u64>,
args: Args args: Args
} }
@@ -145,16 +145,21 @@ async fn start(args: Args, owners: Vec<u64>) {
async fn gen_data(args: Args, owners: Vec<u64>) -> Data { async fn gen_data(args: Args, owners: Vec<u64>) -> Data {
let ball_classic_str = std::fs::read_to_string("./data/8-ball_classic.txt").unwrap(); 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 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<String> = ball_classic_str.lines().map(String::from).collect(); let ball_classic: Vec<String> = ball_classic_str.lines().map(String::from).collect();
let ball_quirk: Vec<String> = ball_quirk_str .lines().map(String::from).collect(); let ball_quirk: Vec<String> = 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<String> = mods_env.split(",").map(String::from).collect();
let mods_vec_u64: Vec<u64> = mods_vec_str
.iter()
.filter_map(|s| Some(s.parse::<u64>().expect("Failed to parse ASSISTANT_BK_MODS. Invalid syntax.")))
.collect();
let data = Data { let data = Data {
owners, owners,
ball_prompts: [ball_classic, ball_quirk], ball_prompts: [ball_classic, ball_quirk],
bk_mods_json: bk_mods, bk_mods: mods_vec_u64,
reddit_data: None.into(), reddit_data: None.into(),
discord_data: None.into(), discord_data: None.into(),
args: args.clone() args: args.clone()