added rest of the buttons (still needs listeners) & reformatted/improved other code
This commit is contained in:
+33
-28
@@ -18,14 +18,20 @@ class PostData:
|
||||
date_unix: int,
|
||||
media_type: str,
|
||||
media_urls: list[str],
|
||||
voters_re: list[str] = [],
|
||||
voters_dc: list[int] = [],
|
||||
mod_voters: list[int] = [],
|
||||
added_by_human: bool = False,
|
||||
added_by_bot: bool = False,
|
||||
approved_by_human: bool = False,
|
||||
approved_by_ris: bool = False
|
||||
removed: bool = False,
|
||||
removed_by: str | None = None,
|
||||
removed_reason: str | None = None,
|
||||
voters_re: list[str] = [],
|
||||
voters_dc: list[int] = [],
|
||||
mod_voters: list[int] = [],
|
||||
added_by_human: bool = False,
|
||||
added_by_bot: bool = False,
|
||||
approved_by_human: bool = False,
|
||||
approved_by_ris: bool = False
|
||||
):
|
||||
self.removed = removed
|
||||
self.removed_by = removed_by
|
||||
self.removed_reason = removed_reason
|
||||
self.url = url
|
||||
self.title = title
|
||||
self.upvotes = upvotes
|
||||
@@ -42,6 +48,11 @@ class PostData:
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
"removed": {
|
||||
"removed": self.removed,
|
||||
"by": self.removed_reason,
|
||||
"reason": self.removed_reason
|
||||
},
|
||||
"post_data": {
|
||||
"title": self.title,
|
||||
"upvotes": self.upvotes,
|
||||
@@ -125,31 +136,26 @@ async def read_cfg(bot: botPy.Bot) -> bool:
|
||||
|
||||
|
||||
def add_post_to_data(bot: botPy.Bot, new_data: PostData, bypass_conditions: bool = False) -> bool:
|
||||
if new_data.removed:
|
||||
new_data.removed = False
|
||||
new_data.removed_by = None
|
||||
new_data.removed_reason = None
|
||||
|
||||
if bypass_conditions:
|
||||
bot.data[botPy.RE_DATA_POSTS][new_data.url] = new_data.to_json()
|
||||
if bot.args["dev"]:
|
||||
py_print(f"Added post \"{new_data.url}\" (Conditions bypassed)")
|
||||
if bot.args["dev"]: py_print(f"Added post \"{new_data.url}\" (Conditions bypassed)")
|
||||
return True
|
||||
|
||||
# not sure what this is for
|
||||
updated = False
|
||||
|
||||
if new_data.url not in bot.data[botPy.RE_DATA_POSTS] or updated:
|
||||
if new_data.url not in bot.data[botPy.RE_DATA_POSTS]:
|
||||
bot.data[botPy.RE_DATA_POSTS][new_data.url] = new_data.to_json()
|
||||
if bot.args["dev"]:
|
||||
py_print(f"Added post \"{new_data.url}\"")
|
||||
if bot.args["dev"]: py_print(f"Added post \"{new_data.url}\"")
|
||||
return True
|
||||
|
||||
if "removed" not in bot.data[botPy.RE_DATA_POSTS][new_data.url]:
|
||||
updated = new_data.upvotes != bot.data[botPy.RE_DATA_POSTS][new_data.url]["post_data"]
|
||||
|
||||
else:
|
||||
py_print(f"Failed to add post \"{new_data.url}\": Removed flag is True.")
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def set_approve_post(bot: botPy.Bot, approved: bool, url: str) -> bool:
|
||||
if not hasattr(bot.data[botPy.RE_DATA_POSTS][url], "removed"):
|
||||
if not bot.data[botPy.RE_DATA_POSTS][url]["removed"]["removed"]:
|
||||
bot.data[botPy.RE_DATA_POSTS][url]["approved"]["by_human"] = approved
|
||||
return True
|
||||
|
||||
@@ -160,12 +166,11 @@ def remove_post(bot: botPy.Bot, url: str, removed_by: str = "UNKNOWN", reason: s
|
||||
weekly = bot.data[botPy.RE_DATA_POSTS]
|
||||
|
||||
if url in weekly:
|
||||
weekly[url] = {
|
||||
"removed": True,
|
||||
"removed_by": removed_by,
|
||||
"remove_reason": reason,
|
||||
"post_data": { "date_unix": weekly[url]["post_data"]["date_unix"] }
|
||||
}
|
||||
rm = weekly[url]["removed"]
|
||||
rm["removed"] = True
|
||||
rm["by"] = removed_by
|
||||
rm["reason"] = reason
|
||||
weekly[url]["removed"] = rm
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
@@ -46,7 +46,7 @@ async def parse_json(response: str, bot: botPy.Bot):
|
||||
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}")
|
||||
if json_response["print"]: py_print(f"Received from Rust: {response}")
|
||||
|
||||
result = await json_to_func(json_response, bot)
|
||||
await ws_global.ping()
|
||||
@@ -86,13 +86,15 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
|
||||
case "stop_praw": r = await bot .stop ()
|
||||
case _: value_supported = False
|
||||
|
||||
print_result = v["print"]
|
||||
|
||||
if not value_supported:
|
||||
val = v["value"]
|
||||
py_print(f"Value \"{val}\" is not supported")
|
||||
return {"type": "result", "value": False}
|
||||
return result_json(False, print_result)
|
||||
|
||||
return result_json(r)
|
||||
return result_json(r, print_result)
|
||||
|
||||
|
||||
def result_json(bool: bool) -> dict:
|
||||
return {"type": "result", "value": bool}
|
||||
def result_json(bool: bool, print_result: bool) -> dict:
|
||||
return {"type": "result", "value": bool, "print": print_result}
|
||||
Reference in New Issue
Block a user