From f17f6e0c16d866ce61864a413593a097f6daec26 Mon Sep 17 00:00:00 2001 From: Byte Dice Date: Sat, 22 Feb 2025 14:57:51 +0100 Subject: [PATCH] things --- README.md | 15 ++++++++------- bk_week_help_dc.md | 15 +++++++++++---- bk_week_help_re.md | 7 ++----- bk_week_suggestions.txt | 1 + src/bk_week_cmds.rs | 18 +++++++++++------- src/cmds.rs | 2 +- src/data.rs | 4 ---- src/python/bot.py | 2 +- src/python/cmds.py | 5 ++++- src/python/posts.py | 2 +- 10 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 bk_week_suggestions.txt diff --git a/README.md b/README.md index 8903711..64f5173 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,17 @@ An automation tool for Byte Dice. It's both a Discord and Reddit bot in one prog ## How to run **Dependecies:** This program uses Rust (v1.82.0) and Python (v3.11.4), you can likely use other versions if they are compatible. This program also uses these Python modules: - -* sys -* io -* asyncpraw -* json -* typing * asyncio +* asyncpraw +* asyncprawcore +* emoji +* io +* json +* os +* sys * threading * time -* emoji +* typing * websockets You can install Python modules by running `$ pip install {module}` or `$ python -m pip install {module}` in a terminal. diff --git a/bk_week_help_dc.md b/bk_week_help_dc.md index 8eaddd3..10f1135 100644 --- a/bk_week_help_dc.md +++ b/bk_week_help_dc.md @@ -1,22 +1,29 @@ # Discord Commands +## `/bk_week_get [url]` +Shows info about a single post. ## `/bk_week_add [url] ` Adds a URL to the list of posts. This is done automatically by the bot for certain posts. -- **``**: (OPTIONAL) `true` or `false`, determines whether to pre-approve the post. +- **``**: (OPTIONAL) `true` or `false`, determines whether to approve the post when added. ## `/bk_week_remove [url]` Removes an existing URL from the list of posts. ## `/bk_week_approve [url] ` Flags the post as **human_approved**, confirming that the artwork is original. - **``**: (OPTIONAL) `true` or `false`, set to `true` if you want to undo an approval. -## `/bk_week_update` -Updates all data in the bound Discord channel. It's recommended to run this rarely. ## `/bk_week_vote [url] ` Adds a vote to a post. The intended use for votes is for a "moderator picks" and a "community picks" section of the weekly art. You can vote on as many posts as you want, but only once per post. +## `/bk_week_update` +Updates all data in the bound Discord channel. It's recommended to only run this if absolutely needed to. ## `/bk_admin_bind` Binds the current channel as the channel where the bk_week data is sent and updated in. No binding means the only way to view the data is using `/bk_week_get`. +## `/bk_week_help [option]` +Shows a help text. +- **`[option]`**: `Discord` or `Reddit`, determines what help text to send. ### **Examples** ``` /bk_week_add https://reddit.com/post_url false /bk_week_add https://reddit.com/post_url /bk_week_approve https://reddit.com/post_url -``` \ No newline at end of file +``` +# Things to note +* This bot uses shortURLs when storing posts. If you want to access posts, you should use their shortURL. You can get a Reddit shortURL by running `/re_shorturl [url]`. \ No newline at end of file diff --git a/bk_week_help_re.md b/bk_week_help_re.md index aee1375..ae4c77e 100644 --- a/bk_week_help_re.md +++ b/bk_week_help_re.md @@ -1,12 +1,9 @@ # Reddit Commands -To execute a command on the Reddit bot, include `u/ByteDiceAssistant [args]` in a comment. -- **`[args]`**: The command you want to run and its arguments. +To execute a command on the Reddit bot, include `u/ByteDiceAssistant [cmd]` in a comment. +- **`[cmd]`**: The command you want to run and its arguments. ## `bk_week_add` Adds the post to the list of posts. - **Only moderators of a subreddit or the OP (Original Poster) can use this command.** - ### **Examples** ``` "u/ByteDiceAssistant bk_week_add" diff --git a/bk_week_suggestions.txt b/bk_week_suggestions.txt new file mode 100644 index 0000000..37fe54e --- /dev/null +++ b/bk_week_suggestions.txt @@ -0,0 +1 @@ +/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 26d8743..53e11c0 100644 --- a/src/bk_week_cmds.rs +++ b/src/bk_week_cmds.rs @@ -1,5 +1,5 @@ use crate::websocket::send_cmd_json; -use crate::{rs_println, websocket, Context, Data, Error, BK_WEEK}; +use crate::{cmds, rs_println, websocket, Context, Data, Error, BK_WEEK}; use crate::messages::*; use crate::data::{self, dc_bind_bk}; @@ -172,35 +172,38 @@ pub async fn bk_week_add( return Ok(()); } + let shorturl_u = cmds::to_shorturl(&url); + let shorturl = if shorturl_u.is_ok() { shorturl_u.unwrap() } else { url }; + data::update_re_data(ctx.data()).await; let reddit_data = get_reddit_data(ctx.data()).await.unwrap(); if let Some(bk_week) = reddit_data.get(BK_WEEK) { let a = approve.unwrap_or_else(|| false); - let r = websocket::send_cmd_json("add_post_url", Some(json!([&url, a, true]))).await.unwrap(); + let r = websocket::send_cmd_json("add_post_url", Some(json!([&shorturl, a, true]))).await.unwrap(); if !r["value"].as_bool().unwrap() { send_msg( ctx, r#"Unknown error! Error trace: `bk_week_cmds.rs -> bk_week_add() -> Unknown error`. - Common reason: The URL provided was likely invalid."#.to_string(), + Common reasons: The URL provided was likely invalid or 403: forbidden (e.g a private subreddit)."#.to_string(), true, true ).await; return Ok(()); } - if let Some(post) = bk_week.get(&url) { + if let Some(post) = bk_week.get(&shorturl) { if post.get("removed").is_some() { - send_unremove_msg(ctx, &url).await; + send_unremove_msg(ctx, &shorturl).await; } else { - send_updated_msg(ctx, &url).await; + send_updated_msg(ctx, &shorturl).await; } } else { - send_msg(ctx, format!("Added post with URL \"<{}>\"!", &url), true, true).await; + send_msg(ctx, format!("Added post with URL \"<{}>\"!", &shorturl), true, true).await; } if a { @@ -284,6 +287,7 @@ async fn approve_cmd(ctx: Context<'_>, url: &str, reddit_data: &Value, approve: 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; + return; } let r = websocket::send_cmd_json("set_approve_post", Some(json!([approve, &url]))).await.unwrap(); diff --git a/src/cmds.rs b/src/cmds.rs index 534ed17..9b80096 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -165,7 +165,7 @@ pub async fn re_shorturl( } -fn to_shorturl(url: &str) -> Result { +pub fn to_shorturl(url: &str) -> Result { let re = Regex::new(r"comments/([a-zA-Z0-9]+)").unwrap(); if let Some(caps) = re.captures(url) { diff --git a/src/data.rs b/src/data.rs index b1ae7f7..dc564b0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -118,9 +118,6 @@ pub async fn dc_add_server(data: &Data, server_id: u64) -> Result<(), ()> { 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": [] })); } - println!("moo"); - write_dc_data(data).await; - println!("muu"); return Ok(()); } @@ -141,7 +138,6 @@ pub async fn dc_bind_bk(data: &Data, server_id: u64, channel_id: u64) -> Result< let server = servers[&server_id.to_string()].as_object_mut().unwrap(); server.insert("bk_week_channel".to_string(), channel_id.into()); - write_dc_data(data).await; return Ok(()); } diff --git a/src/python/bot.py b/src/python/bot.py index 99a9138..bfdb639 100644 --- a/src/python/bot.py +++ b/src/python/bot.py @@ -28,7 +28,7 @@ class Bot: self.data: dict = {} async def initialize(self): - self.sr = await self.r.subreddit("bytedicetesting")#boykisser") + self.sr = await self.r.subreddit("bytedicetesting") async def set_args(self, args: dict): self.args = args diff --git a/src/python/cmds.py b/src/python/cmds.py index 1ceea64..896ab25 100644 --- a/src/python/cmds.py +++ b/src/python/cmds.py @@ -1,4 +1,3 @@ -import asyncpraw as praw import asyncpraw.models as models from macros import * @@ -40,6 +39,10 @@ async def respond_to_mention(bot: botPy.Bot) -> bool: async def bk_week_add(mention: models.Comment, bot: botPy.Bot): + if not mention.subreddit.display_name not in bot.sr: + await mention.mark_read() + return + await mention.submission.load() author = mention.author diff --git a/src/python/posts.py b/src/python/posts.py index a8d4ca7..4c9607a 100644 --- a/src/python/posts.py +++ b/src/python/posts.py @@ -97,7 +97,7 @@ async def from_url(bot: botPy.Bot, url: str) -> tuple[bool, models.Submission]: try: post: models.Submission = await bot.r.submission(url=url) return True, post - except (prawcore.NotFound, prawcore.BadRequest, exc.InvalidURL): + except (prawcore.NotFound, prawcore.BadRequest, exc.InvalidURL, prawcore.Forbidden): return False, None except Exception as e: py_error(f"Unexpected error at posts.py -> from_url():\n{e}")