这下算是 merge 好了

This commit is contained in:
shenjack-5600u 2024-02-02 22:23:51 +08:00
parent a7932a0b2c
commit 33abea4b5b
Signed by: shenjack
GPG Key ID: FDF9864E11C7E79F
6 changed files with 48 additions and 27 deletions

View File

@ -157,13 +157,9 @@ async def main():
if self.eio.state != 'connected':
break
"""
try:
await sio.connect(BOTCONFIG.host)
await sio.wait()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("KeyboardInterrupt")
except Exception:

View File

@ -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 is_self(self) -> bool:
# return self.sender_id == BOTCONFIG.self_id
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, self_id: int) -> bool:
return self.sender_id == self_id
class BotConfig(Options):

View File

@ -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
@ -34,3 +31,6 @@ if __name__ == "__main__":
if args.no_notice:
BOTCONFIG.notice_start = False
from connect import main
asyncio.run(main())

View File

@ -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
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",

View File

@ -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')

View File

@ -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())