mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2024-11-27 06:35:06 +08:00
这下算是 merge 好了
This commit is contained in:
parent
a7932a0b2c
commit
33abea4b5b
|
@ -157,13 +157,9 @@ async def main():
|
||||||
if self.eio.state != 'connected':
|
if self.eio.state != 'connected':
|
||||||
break
|
break
|
||||||
"""
|
"""
|
||||||
await sio.connect(BOTCONFIG.host)
|
|
||||||
await sio.wait()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
try:
|
try:
|
||||||
asyncio.run(main())
|
await sio.connect(BOTCONFIG.host)
|
||||||
|
await sio.wait()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("KeyboardInterrupt")
|
logger.info("KeyboardInterrupt")
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
|
@ -55,12 +55,23 @@ class SendMessage(Options):
|
||||||
|
|
||||||
class NewMessage(Options):
|
class NewMessage(Options):
|
||||||
sender_id: int
|
sender_id: int
|
||||||
|
sender_name: str
|
||||||
room_id: int
|
room_id: int
|
||||||
content: str
|
content: str
|
||||||
msg_id: str
|
msg_id: str
|
||||||
|
data: dict
|
||||||
|
|
||||||
# def is_self(self) -> bool:
|
def init(self, **kwargs) -> None:
|
||||||
# return self.sender_id == BOTCONFIG.self_id
|
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):
|
class BotConfig(Options):
|
||||||
|
|
8
main.py
8
main.py
|
@ -1,9 +1,5 @@
|
||||||
import time
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import argparse
|
import argparse
|
||||||
import traceback
|
|
||||||
|
|
||||||
import socketio
|
|
||||||
|
|
||||||
# from lib_not_dr.types import Options
|
# from lib_not_dr.types import Options
|
||||||
from lib_not_dr.loggers import config
|
from lib_not_dr.loggers import config
|
||||||
|
@ -17,6 +13,7 @@ logger = config.get_logger("bot")
|
||||||
BOTCONFIG: BotConfig = get_config()
|
BOTCONFIG: BotConfig = get_config()
|
||||||
BotStatus = BotStatus()
|
BotStatus = BotStatus()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# --debug
|
# --debug
|
||||||
# --config=config.toml
|
# --config=config.toml
|
||||||
|
@ -34,3 +31,6 @@ if __name__ == "__main__":
|
||||||
if args.no_notice:
|
if args.no_notice:
|
||||||
BOTCONFIG.notice_start = False
|
BOTCONFIG.notice_start = False
|
||||||
|
|
||||||
|
from connect import main
|
||||||
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,13 @@ import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import socketio
|
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 asyncio.sleep(0.1)
|
||||||
await sio.emit(
|
await sio.emit(
|
||||||
"sendMessage", reply_msg.to_content("请求数据中……").to_json()
|
"sendMessage", reply_msg.to_content("请求数据中……").to_json()
|
||||||
|
@ -28,25 +34,27 @@ async def bmcl(sio):
|
||||||
raw_data = await response.text()
|
raw_data = await response.text()
|
||||||
try:
|
try:
|
||||||
data = json.loads(raw_data)
|
data = json.loads(raw_data)
|
||||||
data_bytes: int = data["bytes"]
|
data_bytes: float = data["bytes"]
|
||||||
data_hits: int = data["hits"]
|
data_hits: int = data["hits"]
|
||||||
data_bandwidth: float = data["currentBandwidth"]
|
data_bandwidth: float = data["currentBandwidth"]
|
||||||
load_str: float = data["load"] * 100
|
load_str: float = data["load"] * 100
|
||||||
online_node: int = data["currentNodes"]
|
online_node: int = data["currentNodes"]
|
||||||
online_bandwidth: int = data["bandwidth"]
|
online_bandwidth: int = data["bandwidth"]
|
||||||
data_lens = ["B", "KB", "MB", "GB", "TB"]
|
data_lens = ["B", "KB", "MB", "GB", "TB"]
|
||||||
|
data_len = "0B"
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
if data_bytes < 1024:
|
if data_bytes < 1024:
|
||||||
data_bytes = round(data_bytes, 5)
|
data_bytes = round(data_bytes, 5)
|
||||||
data_bytes = f"{data_bytes}{data_lens[i]}"
|
data_len = f"{data_bytes}{data_lens[i]}"
|
||||||
break
|
break
|
||||||
data_bytes /= 1024
|
else:
|
||||||
|
data_bytes /= 1024
|
||||||
|
|
||||||
report_msg = (
|
report_msg = (
|
||||||
"OpenBMCLAPI 状态:\n"
|
"OpenBMCLAPI 状态:\n"
|
||||||
f"在线节点: {online_node} 带宽: {online_bandwidth}Mbps\n"
|
f"在线节点: {online_node} 带宽: {online_bandwidth}Mbps\n"
|
||||||
f"实时负载带宽: {data_bandwidth:.5f}Mbps 负载: {load_str:.3f}%\n"
|
f"实时负载带宽: {data_bandwidth:.5f}Mbps 负载: {load_str:.3f}%\n"
|
||||||
f"当日 总请求: {data_hits} 总数据量: {data_bytes}"
|
f"当日 总请求: {data_hits} 总数据量: {data_len}"
|
||||||
)
|
)
|
||||||
await sio.emit(
|
await sio.emit(
|
||||||
"sendMessage",
|
"sendMessage",
|
||||||
|
|
|
@ -2,6 +2,13 @@ import time
|
||||||
import random
|
import random
|
||||||
import traceback
|
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:
|
def safe_eval(code: str) -> str:
|
||||||
try:
|
try:
|
||||||
# code = code.replace('help', '坏东西!\n')
|
# code = code.replace('help', '坏东西!\n')
|
||||||
|
|
19
router.py
19
router.py
|
@ -4,11 +4,9 @@ import asyncio
|
||||||
from lib_not_dr.loggers import config
|
from lib_not_dr.loggers import config
|
||||||
|
|
||||||
from main import BOTCONFIG, _version_
|
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 import bmcl, yw, safe_eval
|
||||||
from plugins.bmcl import bmcl
|
|
||||||
from plugins.yw import yw
|
|
||||||
|
|
||||||
logger = config.get_logger("router")
|
logger = config.get_logger("router")
|
||||||
|
|
||||||
|
@ -20,6 +18,7 @@ async def route(data, sio):
|
||||||
content = data["message"]["content"]
|
content = data["message"]["content"]
|
||||||
room_id = data["roomId"]
|
room_id = data["roomId"]
|
||||||
msg_id = data["message"]["_id"]
|
msg_id = data["message"]["_id"]
|
||||||
|
msg = NewMessage(data=data)
|
||||||
|
|
||||||
reply_msg = SendMessage(content="", room_id=room_id, reply_to=ReplyMessage(id=msg_id))
|
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("=="):
|
elif content.startswith("=="):
|
||||||
evals: str = content[2:]
|
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', ' ', '.', '+', '-', '*', '/', '(', ')', '<',
|
# whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '.', '+', '-', '*', '/', '(', ')', '<',
|
||||||
# '>', '=']
|
# '>', '=']
|
||||||
# evals = evals.replace('**', '')
|
# evals = evals.replace('**', '')
|
||||||
|
@ -55,9 +54,9 @@ async def route(data, sio):
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
await sio.emit("sendMessage", message.to_json())
|
await sio.emit("sendMessage", message.to_json())
|
||||||
elif content == "/bmcl":
|
elif content == "/bmcl":
|
||||||
await bmcl.bmcl(sio)
|
await bmcl.bmcl(sio, reply_msg, msg)
|
||||||
elif content == "/yw":
|
# elif content == "/yw":
|
||||||
message = yw.yw()
|
# message = yw.yw()
|
||||||
await asyncio.sleep(random.random() * 2)
|
# await asyncio.sleep(random.random() * 2)
|
||||||
await sio.emit("sendMessage", message.to_json())
|
# await sio.emit("sendMessage", message.to_json())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user