From 2423e62c06c8fa94287e0d36928477c93490340d Mon Sep 17 00:00:00 2001 From: Byte Dice Date: Tue, 25 Feb 2025 19:15:41 +0100 Subject: [PATCH] spam reduction & config command --- TODO.md | 1 + bk_week_suggestions.txt | 1 - src/bk_week_cmds.rs | 23 +++++++++++++++++++++++ src/cmds.rs | 7 ++++++- src/main.rs | 8 ++++++-- src/python/bot.py | 6 +++++- src/python/py_websocket.py | 11 ++++++++--- src/websocket.rs | 6 ++++-- 8 files changed, 53 insertions(+), 10 deletions(-) delete mode 100644 bk_week_suggestions.txt diff --git a/TODO.md b/TODO.md index f6b9cbe..0b0072a 100644 --- a/TODO.md +++ b/TODO.md @@ -8,6 +8,7 @@ - [ ] `/bk_week_top [category] [amount]` to get the top N posts in a category (e.g upvotes) + - [ ] Allow updating the data autonomously and via manual commands. diff --git a/bk_week_suggestions.txt b/bk_week_suggestions.txt deleted file mode 100644 index 37fe54e..0000000 --- a/bk_week_suggestions.txt +++ /dev/null @@ -1 +0,0 @@ -/bk_week_top - get the top posts in a category diff --git a/src/bk_week_cmds.rs b/src/bk_week_cmds.rs index 53e11c0..c3c6f50 100644 --- a/src/bk_week_cmds.rs +++ b/src/bk_week_cmds.rs @@ -643,3 +643,26 @@ pub async fn bk_week_vote( return Ok(()); } + + + + +#[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR")] +/// Changes the subreddit(s) the bot patrols in. +pub async fn bk_cfg_sr(ctx: Context<'_>, sr: String) -> Result<(), Error> { + if !cmds::is_creator(ctx) { + send_msg(ctx, "Failed to update subreddits: Invalid permissions.".to_string(), true, true).await; + } + + let r = send_cmd_json("change_sr", Some(json!([sr]))).await; + + if r.is_some() { + if r.unwrap()["value"].as_bool().unwrap() { + send_msg(ctx, "Successfully updated subreddits!".to_string(), true, true).await; + return Ok(()); + } + } + + send_msg(ctx, "Failed to update subreddits: Failed-type response from Python.".to_string(), true, true).await; + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds.rs b/src/cmds.rs index 9b80096..deb39f9 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -33,7 +33,7 @@ pub async fn stop( let should_stop = ctx.data().args.dev || confirmation.unwrap_or_else(|| "".to_string()).to_lowercase() == "i want to stop the bot now"; - let is_creator = ctx.author().id == UserId::new(ctx.data().byte_dice_id); + let is_creator = is_creator(ctx); if should_stop && is_creator { let msg = send_msg(ctx, "Saving data...".to_string(), true, true).await.unwrap(); @@ -58,6 +58,11 @@ pub async fn stop( } +pub fn is_creator(ctx: Context<'_>) -> bool { + return ctx.author().id == UserId::new(ctx.data().byte_dice_id); +} + + #[poise::command( slash_command, prefix_command, diff --git a/src/main.rs b/src/main.rs index a91a900..90a9349 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,14 +189,18 @@ async fn gen_bot(data: Data, args: Args) -> Client { cmds::re_shorturl(), cmds::add_server(), //cmds::rule(), + // bk_week 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_admin_bind(), bk_week_cmds::bk_week_update(), - bk_week_cmds::bk_week_vote() + bk_week_cmds::bk_week_vote(), + // bk_admin + bk_week_cmds::bk_admin_bind(), + // bk_cfg + bk_week_cmds::bk_cfg_sr() ], event_handler: events::event_handler, ..Default::default() diff --git a/src/python/bot.py b/src/python/bot.py index 5656036..7895a83 100644 --- a/src/python/bot.py +++ b/src/python/bot.py @@ -46,4 +46,8 @@ class Bot: py_print("Stopped Reddit bot.") return True - return False \ No newline at end of file + return False + + async def change_sr(self, new_sr) -> bool: + self.sr = await self.r.subreddit(new_sr) + return True \ No newline at end of file diff --git a/src/python/py_websocket.py b/src/python/py_websocket.py index 65a3c27..fbbafb5 100644 --- a/src/python/py_websocket.py +++ b/src/python/py_websocket.py @@ -35,7 +35,8 @@ async def websocket_client(bot: botPy.Bot): while True: response = await ws.recv() - py_print(f"Received from Rust: {response}") + if not response.startswith("json:"): + py_print(f"Received from Rust: {response}") await parse_json(response, bot) @@ -44,6 +45,9 @@ async def parse_json(response: str, bot: botPy.Bot): json_str = response[5:] try: json_response = json.loads(json_str) + if json_response["value"] not in ["respond_mentions"] or bot.args["dev"]: + py_print(f"Received from Rust: {response}") + result = await json_to_func(json_response, bot) await ws_global.ping() await send_message(f"json:{json.dumps(result)}") @@ -72,12 +76,13 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict: match v["value"]: case "update_data_file": r = data .write_data (bot) case "add_new_posts": r = await posts.add_new_posts (bot) + case "respond_mentions": r = await cmds .respond_to_mention(bot) case "add_post_url": r = await posts.add_post_url (bot, *v["args"]) case "remove_post_url": r = data .remove_post (bot, *v["args"]) case "set_approve_post": r = data .set_approve_post (bot, *v["args"]) case "set_vote_post": r = data .set_vote_post (bot, *v["args"]) - case "respond_mentions": r = await cmds .respond_to_mention(bot) - case "stop_praw": r = await bot .stop() + case "change_sr": r = await bot .change_sr (*v["args"]) + case "stop_praw": r = await bot .stop () case _: value_supported = False if not value_supported: diff --git a/src/websocket.rs b/src/websocket.rs index 2cecd4f..22c9744 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -65,8 +65,10 @@ pub async fn send_cmd_json(func_name: &str, func_args: Option) -> Option< } let r = receive_response().await; - rs_println!("Received from Python: [RESPONSE] {:?}", r); - + if !["respond_mentions"].contains(&func_name) || ::parse().dev { + rs_println!("Received from Python: [RESPONSE] {:?}", r); + } + if r.is_none() { rs_println!("--- Response from Python is None!"); }