diff --git a/connect.py b/connect.py index b3a087b..0d3bd74 100644 --- a/connect.py +++ b/connect.py @@ -157,13 +157,9 @@ async def main(): if self.eio.state != 'connected': break """ - await sio.connect(BOTCONFIG.host) - await sio.wait() - - -if __name__ == "__main__": try: - asyncio.run(main()) + await sio.connect(BOTCONFIG.host) + await sio.wait() except KeyboardInterrupt: logger.info("KeyboardInterrupt") except Exception: diff --git a/data_struct.py b/data_struct.py index 81180f5..104a4f9 100644 --- a/data_struct.py +++ b/data_struct.py @@ -55,12 +55,23 @@ class SendMessage(Options): class NewMessage(Options): sender_id: int + sender_name: str room_id: int content: str msg_id: str + data: dict + + def init(self, **kwargs) -> None: + data = kwargs.pop('data') + + self.sender_name = data["message"]["username"] + self.sender_id = data["message"]["senderId"] + self.content = data["message"]["content"] + self.room_id = data["roomId"] + self.msg_id = data["message"]["_id"] - # def is_self(self) -> bool: - # return self.sender_id == BOTCONFIG.self_id + def is_self(self, self_id: int) -> bool: + return self.sender_id == self_id class BotConfig(Options): diff --git a/main.py b/main.py index ebd2c84..5f52db0 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,5 @@ -import time import asyncio import argparse -import traceback - -import socketio # from lib_not_dr.types import Options from lib_not_dr.loggers import config @@ -17,6 +13,7 @@ logger = config.get_logger("bot") BOTCONFIG: BotConfig = get_config() BotStatus = BotStatus() + if __name__ == "__main__": # --debug # --config=config.toml @@ -33,4 +30,7 @@ if __name__ == "__main__": BOTCONFIG: BotConfig = get_config(args.config) if args.no_notice: BOTCONFIG.notice_start = False + + from connect import main + asyncio.run(main()) diff --git a/plugins/bmcl.py b/plugins/bmcl.py index 2c63402..ce0af04 100644 --- a/plugins/bmcl.py +++ b/plugins/bmcl.py @@ -4,7 +4,13 @@ import asyncio import aiohttp import socketio -async def bmcl(sio): +from lib_not_dr.loggers import config + +from data_struct import NewMessage, SendMessage + +logger = config.get_logger("bmcl") + +async def bmcl(sio, reply_msg: SendMessage, msg: NewMessage): await asyncio.sleep(0.1) await sio.emit( "sendMessage", reply_msg.to_content("请求数据中……").to_json() @@ -28,25 +34,27 @@ async def bmcl(sio): raw_data = await response.text() try: data = json.loads(raw_data) - data_bytes: int = data["bytes"] + data_bytes: float = data["bytes"] data_hits: int = data["hits"] data_bandwidth: float = data["currentBandwidth"] load_str: float = data["load"] * 100 online_node: int = data["currentNodes"] online_bandwidth: int = data["bandwidth"] data_lens = ["B", "KB", "MB", "GB", "TB"] + data_len = "0B" for i in range(5): if data_bytes < 1024: data_bytes = round(data_bytes, 5) - data_bytes = f"{data_bytes}{data_lens[i]}" + data_len = f"{data_bytes}{data_lens[i]}" break - data_bytes /= 1024 + else: + data_bytes /= 1024 report_msg = ( "OpenBMCLAPI 状态:\n" f"在线节点: {online_node} 带宽: {online_bandwidth}Mbps\n" f"实时负载带宽: {data_bandwidth:.5f}Mbps 负载: {load_str:.3f}%\n" - f"当日 总请求: {data_hits} 总数据量: {data_bytes}" + f"当日 总请求: {data_hits} 总数据量: {data_len}" ) await sio.emit( "sendMessage", diff --git a/plugins/safe_eval.py b/plugins/safe_eval.py index 03ae983..2612c86 100644 --- a/plugins/safe_eval.py +++ b/plugins/safe_eval.py @@ -2,6 +2,13 @@ import time import random import traceback +from main import BOTCONFIG + +from colorama import Fore +from lib_not_dr.loggers import config + +logger = config.get_logger("safe_eval") + def safe_eval(code: str) -> str: try: # code = code.replace('help', '坏东西!\n') diff --git a/router.py b/router.py index 2be8939..54b13e6 100644 --- a/router.py +++ b/router.py @@ -4,11 +4,9 @@ import asyncio from lib_not_dr.loggers import config from main import BOTCONFIG, _version_ -from data_struct import SendMessage, ReplyMessage +from data_struct import SendMessage, ReplyMessage, NewMessage -from plugins.safe_eval import safe_eval -from plugins.bmcl import bmcl -from plugins.yw import yw +from plugins import bmcl, yw, safe_eval logger = config.get_logger("router") @@ -20,6 +18,7 @@ async def route(data, sio): content = data["message"]["content"] room_id = data["roomId"] msg_id = data["message"]["_id"] + msg = NewMessage(data=data) reply_msg = SendMessage(content="", room_id=room_id, reply_to=ReplyMessage(id=msg_id)) @@ -29,7 +28,7 @@ async def route(data, sio): elif content.startswith("=="): evals: str = content[2:] - result = safe_eval(evals) + result = safe_eval.safe_eval(evals) # whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '.', '+', '-', '*', '/', '(', ')', '<', # '>', '='] # evals = evals.replace('**', '') @@ -55,9 +54,9 @@ async def route(data, sio): await asyncio.sleep(0.5) await sio.emit("sendMessage", message.to_json()) elif content == "/bmcl": - await bmcl.bmcl(sio) - elif content == "/yw": - message = yw.yw() - await asyncio.sleep(random.random() * 2) - await sio.emit("sendMessage", message.to_json()) + await bmcl.bmcl(sio, reply_msg, msg) + # elif content == "/yw": + # message = yw.yw() + # await asyncio.sleep(random.random() * 2) + # await sio.emit("sendMessage", message.to_json())