Added Reddit commandsgit add .git add .!

This commit is contained in:
2025-02-19 00:43:58 +01:00
parent 413cae9c75
commit c24b0d5d5b
11 changed files with 134 additions and 45 deletions
+78
View File
@@ -0,0 +1,78 @@
import asyncpraw as praw
import asyncpraw.models as models
from macros import *
import bot as botPy
import data
import posts
BOT_ACTION_POSTFIX = "\n\n^(I am not an AI, I am just a bot. This action was preformed automatically by the way.)"
async def make_cmd(cmd: str, bot: botPy.Bot) -> str:
return f"u/{await bot.r.user.me()} {cmd}"
async def is_cmd(cmd: str, text: str, bot: botPy.Bot) -> bool:
command: str = await make_cmd(cmd, bot)
return command.lower() in text.lower()
async def respond_to_mention(bot: botPy.Bot) -> bool:
async for mention in bot.r.inbox.mentions(limit=25):
if not mention.new:
continue
max_len = 100
body = mention.body
truncated = body[:max_len] + "..." if len(body) > max_len else body
py_print(f"New mention: {truncated}")
if await is_cmd("bk_week_add", body, bot):
await bk_week_add(mention, bot)
else:
await mention.mark_read()
return True
async def bk_week_add(mention: models.Comment, bot: botPy.Bot):
await mention.submission.load()
author = mention.author
is_op = author == mention.submission.author
is_mod = author in await mention.subreddit.moderator()
if not is_op and not is_mod:
await mention.mark_read()
return
short_url = mention.submission.shortlink
r = ""
bd = bot.data[data.BK_WEEKLY]
if short_url not in bd:
posts.add_post_url(bot, short_url)
r = "Successfully added this post to the data!"
if short_url in bd and is_mod:
if "removed" in bd[short_url]:
r = "(Mod action) Successfully un-removed this post from the data! Glad to see you back!"
else:
r = "(Mod action) Successfully added this post to the data!"
post = await posts.from_url(bot, short_url)
post_data = posts.get_post_details(post[1])
data.add_post_to_data(bot, post_data, True)
elif short_url in bd:
r = "Could not add this post to the data. Luckily, it's already there, so there's nothing to worry about!"
await mention.reply(r + " Thank you for participating!" + BOT_ACTION_POSTFIX)
await mention.mark_read()
+2 -2
View File
@@ -1,13 +1,13 @@
from printColors import PrintColors
def py_print(*args: str):
def py_print(*args):
print(
PrintColors.FG.blue + "Py",
"-",
" ".join(args) + PrintColors.Special.reset
)
def py_error(*args: str):
def py_error(*args):
print(
PrintColors.BG.red + "ERROR" + PrintColors.Special.reset,
PrintColors.FG.blue + "Py",
-2
View File
@@ -1,13 +1,11 @@
import sys
import asyncio
import threading
import time
from macros import *
import bot as botPy
import data
import py_websocket
import posts
async def main():
+1 -1
View File
@@ -119,7 +119,7 @@ def get_post_details(post: models.Submission, added_by_h: bool = False) -> data.
)
async def add_post_url(bot, url: str, approve: bool, added_by_h: bool = False) -> bool:
async def add_post_url(bot, url: str, approve: bool = False, added_by_h: bool = False) -> bool:
result, post = await from_url(bot, url)
if not result:
+13 -13
View File
@@ -6,6 +6,7 @@ from macros import *
import bot as botPy
import data
import posts
import cmds
ws_global = None
is_connected = False
@@ -66,26 +67,25 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
return
value_supported = True
result = {"type": "result", "value": False}
r = False
match v["value"]:
case "update_data_file": result = result_json(data.write_data(bot))
case "add_new_posts": result = result_json(await posts.add_new_posts(bot))
case "add_post_url": result = result_json(await posts.add_post_url(bot, *v["args"]))
case "remove_post_url": result = result_json(data.remove_post(bot, *v["args"]))
case "set_approve_post": result = result_json(data.set_approve_post(bot, *v["args"]))
case "set_vote_post": result = result_json(data.set_vote_post(bot, *v["args"]))
case "stop_praw": result = result_json(await bot.stop())
case "update_data_file": r = data .write_data (bot)
case "add_new_posts": r = await posts.add_new_posts (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 _: value_supported = False
if not isinstance(result.get("value"), bool):
py_print("Result JSON is invalid:", str(result))
if bot.args["dev"] and not value_supported:
if not value_supported:
val = v["value"]
py_print(f"Value \"{val}\" is not supported")
return {"type": "result", "value": False}
return result
return result_json(r)
def result_json(bool: bool) -> dict: