socket-bot/ica-py/router.py

63 lines
2.0 KiB
Python
Raw Normal View History

2024-02-02 21:44:15 +08:00
import random
import asyncio
from lib_not_dr.loggers import config
2024-02-02 21:47:43 +08:00
from main import BOTCONFIG, _version_
2024-02-02 22:23:51 +08:00
from data_struct import SendMessage, ReplyMessage, NewMessage
2024-02-02 21:47:43 +08:00
from plugins import bmcl, safe_eval
2024-02-02 21:44:15 +08:00
logger = config.get_logger("router")
2024-02-02 21:47:43 +08:00
async def route(data, sio):
is_self = data["message"]["senderId"] == BOTCONFIG.self_id
sender_name = data["message"]["username"]
sender_id = data["message"]["senderId"]
content = data["message"]["content"]
room_id = data["roomId"]
msg_id = data["message"]["_id"]
2024-02-02 22:23:51 +08:00
msg = NewMessage(data=data)
2024-02-02 21:47:43 +08:00
reply_msg = SendMessage(content="", room_id=room_id, reply_to=ReplyMessage(id=msg_id))
if content == "/bot":
message = reply_msg.to_content(f"icalingua bot-python pong v{_version_}")
await sio.emit("sendMessage", message.to_json())
elif content.startswith("=="):
evals: str = content[2:]
2024-02-02 22:23:51 +08:00
result = safe_eval.safe_eval(evals)
# whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '.', '+', '-', '*', '/', '(', ')', '<',
# '>', '=']
# evals = evals.replace('**', '')
# express = ''
# for text in evals:
# if text in whitelist:
# express += text
# if express == '':
# result = '你在干嘛'
# else:
# result = str(eval(express))
message = reply_msg.to_content(result)
await asyncio.sleep(random.random() * 2)
await sio.emit("sendMessage", message.to_json())
elif content == "!!jrrp":
randomer = random.Random(
f'{sender_id}-{data["message"]["date"]}-jrrp-{_version_}'
)
result = randomer.randint(0, 50) + randomer.randint(0, 50)
logger.info(f"{sender_name} 今日人品值为 {result}")
message = reply_msg.to_content(f"{sender_name} 今日人品为 {result}")
await asyncio.sleep(0.5)
await sio.emit("sendMessage", message.to_json())
elif content == "/bmcl":
2024-02-02 22:23:51 +08:00
await bmcl.bmcl(sio, reply_msg, msg)