From 2c797ad69dfc1c4186f03bd3daa6f256ce74b7c8 Mon Sep 17 00:00:00 2001 From: Byte Dice Date: Thu, 19 Jun 2025 10:06:39 +0200 Subject: [PATCH] added full language support for python & fixed reddut "add_post" cmd. --- data/lang/en.json | 7 ++++++- src/python.rs | 2 +- src/python/bot.py | 2 +- src/python/cmds.py | 26 ++++++++++++++------------ src/python/data.py | 2 ++ src/python/macros.py | 2 ++ 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/data/lang/en.json b/data/lang/en.json index 3304727..a30af42 100644 --- a/data/lang/en.json +++ b/data/lang/en.json @@ -56,5 +56,10 @@ "dc_msg_update_removing_old": "{0}Removing old posts (threshold: {1}d)...", "dc_msg_update_removing": "{0}Removing removed posts...", "log_lang_load_success": "Successfully loaded the english language file!", - "none": "None" + "none": "None", + "py_re_response_suffix": "^(I am not an AI, I am just a bot. This action was performed automatically by the way. You can report bugs and view my source code [here](https://github.com/ByteDice/ByteDiceAssistant)!)", + "py_re_response_weekly_add": "Successfully added your post to the weekly art submissions! Thank you for participating!", + "py_re_response_weekly_exists": "Couldn't add this post to the submissions! Luckily, it's already there! Thank you for participating!", + "py_re_response_weekly_mod_add": "[MOD ACTION] Successfully added this post to the weekly art submissions!", + "py_re_response_weekly_mod_unremove": "[MOD ACTION] Successfully un-removed this post from the weekly art submissions!" } \ No newline at end of file diff --git a/src/python.rs b/src/python.rs index 89c96c3..d382dcd 100644 --- a/src/python.rs +++ b/src/python.rs @@ -25,7 +25,7 @@ pub async fn start(args: Args) -> PyResult<()> { unsafe { app_path = CString::new( - format!("args = {}\nlang_name = {}\n{}", + format!("args = {}\nlang_name = \"{}\"\n{}", py_args, LANG_NAME.clone().unwrap(), code diff --git a/src/python/bot.py b/src/python/bot.py index 0d1dc92..40ed8e1 100644 --- a/src/python/bot.py +++ b/src/python/bot.py @@ -66,5 +66,5 @@ class Bot: self.sr_list = new_cfg[CFG_DATA_RE]["subreddits"].split("+") self.sr = await self.r.subreddit("+".join(self.sr_list)) self.fetch_limit = new_cfg[CFG_DATA_RE]["fetch_limit"] - self.flairs = new_cfg[CFG_DATA_RE]["search_flair"] + self.flairs = new_cfg[CFG_DATA_RE]["search_flairs"] return True \ No newline at end of file diff --git a/src/python/cmds.py b/src/python/cmds.py index 7a06f59..6727792 100644 --- a/src/python/cmds.py +++ b/src/python/cmds.py @@ -2,13 +2,9 @@ import asyncpraw.models as models from macros import * import bot as botPy -import data import posts -BOT_ACTION_POSTFIX = "^(I am not an AI, I am just a bot. This action was performed automatically by the way. You can report bugs and view my source code [here](https://github.com/ByteDice/ByteDiceAssistant)!)" - - async def make_cmd(cmd: str, bot: botPy.Bot) -> str: return f"u/{await bot.r.user.me()} {cmd}" @@ -33,7 +29,7 @@ async def respond_to_mention(bot: botPy.Bot) -> bool: await bk_week_add(mention, bot) else: - py_print("Mention was not a command.") + if bot.args["dev"]: py_print("Mention was not a command.") await mention.mark_read() return True @@ -41,8 +37,11 @@ async def respond_to_mention(bot: botPy.Bot) -> bool: async def bk_week_add(mention: models.Comment, bot: botPy.Bot): if bot.args["dev"]: py_print("Mention was a command: add_post") - if not mention.subreddit.display_name not in bot.sr_list: + + subreddit = mention.subreddit.display_name + if subreddit not in bot.sr_list: await mention.mark_read() + if bot.args["dev"]: py_print(f"Mention wasn't in a selected subreddit. Subreddit: {subreddit}") return await mention.submission.load() @@ -52,8 +51,9 @@ async def bk_week_add(mention: models.Comment, bot: botPy.Bot): is_op = author == mention.submission.author is_mod = author in await mention.subreddit.moderator() - if not is_op and not is_mod: + if (not is_op) and (not is_mod): await mention.mark_read() + if bot.args["dev"]: py_print("Mention wasn't by the OP or a moderator.") return short_url = mention.submission.shortlink @@ -62,16 +62,18 @@ async def bk_week_add(mention: models.Comment, bot: botPy.Bot): bd = bot.data[botPy.RE_DATA_POSTS] if short_url not in bd: - if not is_mod: r = "Successfully added your post to the weekly art submissions!" - if is_mod: r = "[MOD ACTION] Successfully added this post to the weekly art submissions!" + if not is_mod: r = lang("py_re_response_weekly_add") + if is_mod: r = lang("py_re_response_weekly_mod_add") else: if bd[short_url]["removed"]["removed"] and is_mod: - r = "[MOD ACTION] Successfully un-removed this post from the weekly art submissions!" + r = lang("py_re_response_weekly_mod_unremove") elif not bd[short_url]["removed"]["removed"]: - r = "Couldn't add this post to the submissions! Luckily, it's already there!" + r = lang("py_re_response_weekly_exists") await posts.add_post_url(bot, short_url) if r != "": - await mention.reply(r + " Thank you for participating!" + "\n\n" + BOT_ACTION_POSTFIX) + await mention.reply(r + "\n\n" + lang("py_re_response_suffix")) + if bot.args["dev"]: py_print("Responded to mention.") + elif bot.args["dev"]: py_print("Response is empty.") await mention.mark_read() \ No newline at end of file diff --git a/src/python/data.py b/src/python/data.py index 061339f..19767ab 100644 --- a/src/python/data.py +++ b/src/python/data.py @@ -144,6 +144,8 @@ def add_post_to_data(bot: botPy.Bot, new_data: PostData, bypass_conditions: bool new_data.removed = False new_data.removed_by = None new_data.removed_reason = None + if bot.args["dev"]: py_print(f"Un-removed post \"{new_data.url}\"") + return True if bypass_conditions: bot.data[botPy.RE_DATA_POSTS][new_data.url] = new_data.to_json() diff --git a/src/python/macros.py b/src/python/macros.py index 3518f80..0248f40 100644 --- a/src/python/macros.py +++ b/src/python/macros.py @@ -26,6 +26,8 @@ def py_error(*args): def lang(k: str) -> str: + if G_LANG == {}: + py_error("Language must be initialized before use!") t = G_LANG.get(k) if k is None: py_error(f"Key not found in language \"{G_LANG_NAME}\": {k}") return str(t)