partially made /bk_week_add command

This commit is contained in:
2025-02-09 21:22:44 +01:00
parent 9a723a2332
commit 99215d17e2
9 changed files with 233 additions and 57 deletions
+14 -6
View File
@@ -5,6 +5,7 @@ import json
from macros import py_print
import bot as botPy
import data
import posts
ws_global = None
is_connected = False
@@ -25,15 +26,17 @@ async def websocket_client(bot: botPy.Bot):
while True:
response = await ws.recv()
py_print(f"Received from Rust: {response}")
parse_json(response, bot)
await parse_json(response, bot)
def parse_json(response: str, bot: botPy.Bot):
async def parse_json(response: str, bot: botPy.Bot):
if response.startswith("json:"):
json_str = response[5:]
try:
json_response = json.loads(json_str)
json_to_func(json_response, bot)
result = await json_to_func(json_response, bot)
await ws_global.ping()
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}")
@@ -44,7 +47,7 @@ def run_thread(bot: botPy.Bot):
loop.run_until_complete(websocket_client(bot))
def json_to_func(v: dict, bot: botPy.Bot):
async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
if "type" not in v or "value" not in v or not isinstance(v, dict):
if bot.args["dev"]: py_print("JSON is not a dictionary or does not include \"type\" and \"value\" keys.")
return
@@ -53,9 +56,14 @@ def json_to_func(v: dict, bot: botPy.Bot):
return
value_supported = True
result = {"type": "result", "value": False}
match v["value"]:
case "update_data_file": data.write_data(bot)
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 _: value_supported = False
if bot.args["dev"] and not value_supported:
py_print(f"Value {v['value']} is not supported")
py_print(f"Value {v['value']} is not supported")
return result