diff --git a/TODO.md b/TODO.md index 345b4a8..befef35 100644 --- a/TODO.md +++ b/TODO.md @@ -6,10 +6,12 @@ - [x] ~~Put it in a JSON~~ - [x] ~~Multithread so it can run both Discord and Reddit bot!!!~~ - [ ] Allow updating the data autonomously and via manual commands. - - [ ] Manually add posts (via `u/[bot] add` or `/bk_week_add [url]`) - - [ ] Manually remove posts (via `/bk_week_remove [url]`) - - [ ] Manually approve posts (via `/bk_week_approve [url]`) - - [ ] Manually un-approve posts (via `/bk_week_disapprove [url]`) + - [ ] Manually add posts + - [ ] via `u/[bot] add` + - [x] ~~via `/bk_week_add [url]`~~ + - [ ] Manually remove posts via `/bk_week_remove [url]` + - [ ] Manually approve posts via `/bk_week_approve [url]` + - [ ] Manually un-approve posts via `/bk_week_disapprove [url]` - [x] ~~Automatically add scraped posts to JSON~~ - [ ] Automatically remove posts older than 7 days from JSON - [x] ~~Function~~ diff --git a/data/reddit_data_preset.json b/data/reddit_data_preset.json index 35388ae..f630828 100644 --- a/data/reddit_data_preset.json +++ b/data/reddit_data_preset.json @@ -20,7 +20,8 @@ } }, "EXAMPLE VALUE DELETED": { - "removed": true + "removed": true, + "removed_by": "ME!!!!" } } } \ No newline at end of file diff --git a/src/bk_week_cmds.rs b/src/bk_week_cmds.rs index 057d75b..42d2bc0 100644 --- a/src/bk_week_cmds.rs +++ b/src/bk_week_cmds.rs @@ -1,4 +1,4 @@ -use crate::{rs_println, websocket, Context, Error}; +use crate::{rs_println, websocket, Context, Error, BK_WEEK}; use crate::messages::{send_embed, send_msg, EmbedOptions}; use crate::data; @@ -49,10 +49,10 @@ async fn get_reddit_data(ctx: Context<'_>) -> Result { async fn get_post_from_data(ctx: Context<'_>, reddit_data: &Value, url: &str) -> Result, Error> { - if let Some(bk_week) = reddit_data.get("bk_weekly_art_posts") { + if let Some(bk_week) = reddit_data.get(BK_WEEK) { if let Some(post) = bk_week.get(url) { if post.get("removed").is_some() { - send_post_removed_message(ctx, url).await; + send_post_removed_message(ctx, url, post.get("removed_by").unwrap().as_str().unwrap()).await; return Ok(None); } return Ok(Some(post.clone())); @@ -101,7 +101,7 @@ async fn send_post_not_found_message(ctx: Context<'_>, url: &str) { send_msg( ctx, format!( - r#"Post url \"<{}>\" not found: Post doesn't exist in the data! + r#"Post URL \"<{}>\" not found: Post doesn't exist in the data! Hint: Run the command `/bk_week_add [URL]` in a Discord channel or `u/ByteDiceAssistant bk_week_add` in a Reddit post."#, url ).trim().to_string(), @@ -111,13 +111,13 @@ async fn send_post_not_found_message(ctx: Context<'_>, url: &str) { } -async fn send_post_removed_message(ctx: Context<'_>, url: &str) { +async fn send_post_removed_message(ctx: Context<'_>, url: &str, rm_by: &str) { send_msg( ctx, format!( - r#"Post url \"<{}>\" is removed: Post is removed from the data! + r#"Post URL \"<{}>\" is removed: Post is removed from the data! (Removed by: {}) Hint: Run the command `/bk_week_add [URL]` in a Discord channel or `u/ByteDiceAssistant bk_week_add` in a Reddit post."#, - url + url, rm_by ).trim().to_string(), true, true @@ -152,7 +152,7 @@ pub async fn bk_week_add( data::update_re_data(ctx.data()).await; let reddit_data = get_reddit_data(ctx).await.unwrap(); - if let Some(bk_week) = reddit_data.get("bk_weekly_art_posts") { + if let Some(bk_week) = reddit_data.get(BK_WEEK) { websocket::send_cmd_json("add_post_url", json!([&url])).await; if let Some(post) = bk_week.get(&url) { @@ -170,12 +170,12 @@ pub async fn bk_week_add( async fn send_unremove_msg(ctx: Context<'_>, url: &str) { - send_msg(ctx, format!("Un-removed post with URL \"{}\"!", url), true, true).await; + send_msg(ctx, format!("Un-removed post with URL \"<{}>\"!", url), true, true).await; } async fn send_updated_msg(ctx: Context<'_>, url: &str) { - send_msg(ctx, format!("Updated post with URL \"{}\"!", url), true, true).await; + send_msg(ctx, format!("Updated post with URL \"<{}>\"!", url), true, true).await; } @@ -184,11 +184,9 @@ async fn send_updated_msg(ctx: Context<'_>, url: &str) { #[poise::command(slash_command, prefix_command)] pub async fn bk_week_remove( ctx: Context<'_>, - #[description = "The post URL"] url: Option + #[description = "The post URL"] url: String ) -> Result<(), Error> { - // update data - // use python_comms.rs to tell python to update its data return Ok(()); } @@ -198,11 +196,31 @@ pub async fn bk_week_remove( #[poise::command(slash_command, prefix_command)] pub async fn bk_week_approve( ctx: Context<'_>, - #[description = "The post URL"] url: Option + #[description = "The post URL"] url: String ) -> Result<(), Error> { - // update data - // use python_comms.rs to tell python to update its data + data::update_re_data(ctx.data()).await; + let reddit_data = get_reddit_data(ctx).await.unwrap(); + + if let Some(post) = reddit_data.get(BK_WEEK).unwrap().get(&url) { + if post.get("removed").is_some() { + send_post_removed_message(ctx, &url, post.get("removed_by").unwrap().as_str().unwrap()).await; + } + + let r = websocket::send_cmd_json("set_approve_post", json!([true, &url])).await; + if let Some(v) = r.unwrap().get("value") { + send_msg(ctx, format!("Successfully flagged URL \"<{}>\" as `approved:by_human`!", &url), true, true).await; + } + else { + send_msg(ctx, format!("Unknown error!\nError trace: `bk_week_cmds.rs -> bk_week_approve() -> unwrap websocket result error`."), true, true).await; + } + } + else { + send_post_not_found_message(ctx, &url).await; + } + + let result = websocket::send_cmd_json("set_approve_post", json!([true, &url])); + return Ok(()); } @@ -212,10 +230,8 @@ pub async fn bk_week_approve( #[poise::command(slash_command, prefix_command)] pub async fn bk_week_disapprove( ctx: Context<'_>, - #[description = "The post URL"] url: Option + #[description = "The post URL"] url: String ) -> Result<(), Error> { - // update data - // use python_comms.rs to tell python to update its data return Ok(()); } \ No newline at end of file diff --git a/src/data.rs b/src/data.rs index 3b90f00..ad102ce 100644 --- a/src/data.rs +++ b/src/data.rs @@ -3,7 +3,7 @@ use std::path::Path; use serde_json::{self, Value, json}; -use crate::Data; +use crate::{Data, BK_WEEK}; use crate::websocket::send_cmd_json; @@ -73,7 +73,7 @@ fn generate_re_data() { let preset_str = fs::read_to_string(PRESET_PATH_RE).unwrap(); let mut preset_json: Value = serde_json::from_str(&preset_str).unwrap(); - if let Some(bk_week) = preset_json["bk_weekly_art_posts"].as_object_mut() { + if let Some(bk_week) = preset_json[BK_WEEK].as_object_mut() { bk_week.remove("EXAMPLE VALUE"); bk_week.remove("EXAMPLE VALUE DELETED"); } diff --git a/src/main.rs b/src/main.rs index f76f4fe..55ac9da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,9 @@ type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; +static BK_WEEK: &str = "bk_weekly_art_posts"; + + #[tokio::main] async fn main() { let args = ::parse(); @@ -140,7 +143,10 @@ async fn gen_bot(data: Data) -> Client { //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_add(), + //bk_week_cmds::bk_week_remove(), + bk_week_cmds::bk_week_approve(), + //bk_week_cmds::bk_week_disapprove() ], event_handler: events::event_handler, ..Default::default() diff --git a/src/messages.rs b/src/messages.rs index 9bb7766..1c9f879 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,4 +1,4 @@ -use crate::{rs_println, Context}; +use crate::Context; use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle}; use poise::serenity_prelude::{Color, CreateEmbed, Timestamp}; @@ -61,8 +61,6 @@ pub async fn send_embed( reply: bool ) -> Option> { - let r_text = format!("Sent embed: {}", options.title.clone().unwrap_or_else(|| "No title".to_string())); - let mut embed = CreateEmbed::new() .title (none_to_empty(options.title)) .description(options.desc) @@ -79,13 +77,11 @@ pub async fn send_embed( }; let msg = ctx.send(r).await; - rs_println!("{}", r_text); return Some(msg.unwrap()); } else { let r = CreateMessage::new().embeds(vec![embed]); let _ = ctx.channel_id().send_message(ctx.http(), r).await; - rs_println!("{}", r_text); return None; } } diff --git a/src/python/data.py b/src/python/data.py index d25ceb9..2ad7229 100644 --- a/src/python/data.py +++ b/src/python/data.py @@ -69,8 +69,8 @@ def read_data(bot: botPy.Bot): with open(data_path + "\\reddit_data_preset.json", "r") as f: data_preset_json = json.load(f) - data_preset_json["bk_weekly_art_posts"].pop("EXAMPLE VALUE", None) - data_preset_json["bk_weekly_art_posts"].pop("EXAMPLE VALUE DELETED", None) + data_preset_json[BK_WEEKLY].pop("EXAMPLE VALUE", None) + data_preset_json[BK_WEEKLY].pop("EXAMPLE VALUE DELETED", None) with open(data_path + "\\reddit_data.json", "w") as f: json.dump(data_preset_json, f, indent = 2) @@ -109,4 +109,12 @@ def add_post_to_data(bot: botPy.Bot, new_data: PostData, bypass_conditions: bool else: py_print(f"Failed to add post \"{new_data.url}\": Already exists.") - return False \ No newline at end of file + return False + + +def set_approve_post(bot: botPy.Bot, approved: bool, url: str) -> bool: + if not hasattr(bot.data[BK_WEEKLY][url], "removed"): + bot.data[BK_WEEKLY][url]["approved"]["by_human"] = approved + return True + + return False \ No newline at end of file diff --git a/src/python/posts.py b/src/python/posts.py index 2ecdf8c..3a7b467 100644 --- a/src/python/posts.py +++ b/src/python/posts.py @@ -102,7 +102,7 @@ def from_url(bot: botPy.Bot, url: str) -> tuple[bool, models.Submission]: return False, None -def get_post_details(post: models.Submission): +def get_post_details(post: models.Submission) -> data.PostData: media = has_media(post) return data.PostData( @@ -116,11 +116,11 @@ def get_post_details(post: models.Submission): ) -async def add_post_url(bot, url: str) -> bool: +def add_post_url(bot, url: str) -> bool: result, post = from_url(bot, url) if not result: return result post_data = get_post_details(post) - data.add_post_to_data(bot, post_data, True) \ No newline at end of file + return data.add_post_to_data(bot, post_data, True) \ No newline at end of file diff --git a/src/python/py_websocket.py b/src/python/py_websocket.py index b81b46e..4fe0bfa 100644 --- a/src/python/py_websocket.py +++ b/src/python/py_websocket.py @@ -36,6 +36,7 @@ async def parse_json(response: str, bot: botPy.Bot): json_response = json.loads(json_str) result = await json_to_func(json_response, bot) await ws_global.ping() + print(result) await send_message(f"json:{json.dumps(result)}") except json.JSONDecodeError as e: if bot.args["dev"]: py_print(f"failed to parse json: {json_str}\n reason: {e}") @@ -60,7 +61,8 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict: match v["value"]: case "update_data_file": result = {"type": "result", "value": data.write_data(bot)} - case "add_post_url": result = {"type": "result", "value": await posts.add_post_url(bot, *v["args"])} + case "add_post_url": result = {"type": "result", "value": posts.add_post_url(bot, *v["args"])} + case "set_approve_post": result = {"type": "result", "value": data.set_approve_post(bot, *v["args"])} case _: value_supported = False if bot.args["dev"] and not value_supported: