From 557a2b1ca693ceee36a1ca5f0d3daca1e1e51ce0 Mon Sep 17 00:00:00 2001 From: BadHappy <1196554262@qq.com> Date: Sun, 31 Dec 2023 08:40:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B2=A1=E6=9C=89=E4=BB=BB=E4=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=80=A7=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-temp.toml | 2 +- connect.py | 44 +++++++++++++++++++++----------- module/hitokoto/__init__.py | 50 ++++++++++++++++++++++++++++--------- module/hitokoto/test.py | 0 4 files changed, 68 insertions(+), 28 deletions(-) delete mode 100644 module/hitokoto/test.py diff --git a/config-temp.toml b/config-temp.toml index 5ff2239..e239552 100644 --- a/config-temp.toml +++ b/config-temp.toml @@ -1,4 +1,4 @@ private_key = "" # 与 icalingua 客户端使用的 private_key 一致 -host = "" # docker 版 icalingua 服务的地址 +host = " " # docker 版 icalingua 服务的地址 self_id = 0 # 机器人的 qq 号 diff --git a/connect.py b/connect.py index 1455675..de46eb8 100644 --- a/connect.py +++ b/connect.py @@ -15,8 +15,10 @@ from mcstatus import JavaServer # 功能包引用处 from module import hitokoto + # 功能包结束引用 +# 读取配置 def get_config() -> Tuple[str, str, int]: with open('config.toml', 'r', encoding='utf-8') as f: config = qtoml.load(f) @@ -85,7 +87,7 @@ async def require_auth(salt: str, versions: Dict[str, str]): # 准备数据 sign = SigningKey(bytes.fromhex(KEY)) signature = sign.sign(bytes.fromhex(salt)) - + # 发送数据 print(f"{len(signature.signature)=} {type(signature.signature)=}") await sio.emit('auth', signature.signature) @@ -182,8 +184,10 @@ def safe_eval(code: str) -> str: return result +# 获取到新的消息 @sio.on('addMessage') async def add_message(data: Dict[str, Any]): + print(f"{Fore.MAGENTA}add_message: {data}{Style.RESET_ALL}") is_self = data['message']['senderId'] == SELF_ID @@ -191,54 +195,62 @@ async def add_message(data: Dict[str, Any]): sender_id = data['message']['senderId'] content = data['message']['content'] room_id = data['roomId'] + reply = ReplyMessage(id=data['message']['_id']) if not is_self: + # /bot if data.get('message').get('content') == '/bot': message = Message(content='icalingua bot test', room_id=data['roomId']) await sio.emit('sendMessage', message.to_json()) - + + # !!status elif content == '!!status': server = JavaServer.lookup("192.168.1.6:25565") status=server.status() # query = server.query() - + # if status.players.online != 0: # message = Message(content=f"此服务器有 {status.players.online} 个玩家在线\n当前在线玩家有 {.join(query.players.names)}",room_id=data['roomId']) # else: # message = Message(content=f"此服务器空无一人",room_id=data['roomId']) - - message = Message(content=f"此服务器有 {status.players.online} 个玩家在线",room_id=data['roomId']) + + message = Message(content=f"此服务器有 {status.players.online} 个玩家在线",reply_to=reply,room_id=data['roomId']) await sio.emit('sendMessage', message.to_json()) - + + # !!players elif content == '!!players': server = JavaServer.lookup("192.168.1.6:25565") query = server.query() players = query.players.names - message = Message(content=f"此服务器当前在线玩家有 {players}",room_id=data['roomId']) - + message = Message(content=f"此服务器当前在线玩家有 {players}",reply_to=reply,room_id=data['roomId']) + await sio.emit('sendMessage', message.to_json()) + # !!hitokoto elif content.startswith("!!hitokoto"): ctypet = content[len("!!hitokoto "):] if content == "!!hitokoto": - result_code, result_data = hitokoto.hitokoto("没有") + result_code, result_data, result_type= hitokoto.hitokoto(None) else: - result_code, result_data = hitokoto.hitokoto(ctypet) + result_code, result_data, result_type= hitokoto.hitokoto(ctypet) if result_code == 3: - message = Message(content=f"参数不对哦!",room_id=data['roomId']) + message = Message(content=f"参数不对哦!你可以输入!!hitokoto help查看帮助",reply_to=reply,room_id=data['roomId']) elif result_code == 2: - message = Message(content=f"发生了不可描述的错误X_X", room_id=data['roomId']) + message = Message(content=f"发生了不可描述的错误X_X,但可以肯定的是模块炸了(",reply_to=reply,room_id=data['roomId']) + elif result_code == 1: + message = Message(content=f"(参数)句子类型如下\na 动画\nb 漫画\nc 游戏\nd 文学\ne 原创\nf 来自网络\ng 其他\nh 影视\ni 诗词\nj 网易云\nk 哲学\nl 抖机灵",reply_to=reply,room_id=data['roomId']) elif result_code == 0: - message = Message(content=f"Hitokoto: {result_data['hitokoto']}", room_id=data['roomId']) + message = Message(content=f"“{result_data['hitokoto']}”\n来源:{result_data['from']}\n类型:{result_type}",reply_to=reply,room_id=data['roomId']) else: - message = Message(content=f"但你看到这条消息就代表有bug出炉", room_id=data['roomId']) + message = Message(content=f"你看到这条消息就代表有bug出炉,但肯定不是模块炸了(",reply_to=reply,room_id=data['roomId']) await sio.emit('sendMessage', message.to_json()) + # 未知 elif data.get('message').get('content').startswith('=='): evals: str = data.get('message').get('content')[2:] @@ -273,16 +285,18 @@ async def add_message(data: Dict[str, Any]): await asyncio.sleep(random.random() * 2) await sio.emit('sendMessage', message.to_json()) + + # !!jrrp elif data['message']['content'] == '!!jrrp': randomer = random.Random(f'{sender_id}-{data["message"]["date"]}-jrrp-v2') result = randomer.randint(0, 50) + randomer.randint(0, 50) print(f'{sender_name} 今日人品值为 {result}') - reply = ReplyMessage(id=data['message']['_id']) message = Message(content=f'{sender_name} 今日人品值为 {result}', reply_to=reply, room_id=room_id) await asyncio.sleep(0.5) await sio.emit('sendMessage', message.to_json()) + # 如果只包括一个或多个 6 # elif data['message']['content'].replace(' ', '') in ('6', '666', '六', '3+3', '5+1', '4+2', '2+4', '1+5'): # reply = ReplyMessage(id=data['message']['_id']) diff --git a/module/hitokoto/__init__.py b/module/hitokoto/__init__.py index 69316d5..a37f290 100644 --- a/module/hitokoto/__init__.py +++ b/module/hitokoto/__init__.py @@ -1,8 +1,10 @@ import requests import random - def hitokoto(ctypet): + + result_type = None + try: if ctypet == '动画' or ctypet == 'a': @@ -15,7 +17,7 @@ def hitokoto(ctypet): ctype = 'd' elif ctypet == '原创' or ctypet == 'e': ctype = 'e' - elif ctypet == '网络' or ctypet == 'f': + elif ctypet == '来自网络' or ctypet == 'f': ctype = 'f' elif ctypet == '其他' or ctypet == 'g': ctype = 'g' @@ -29,24 +31,48 @@ def hitokoto(ctypet): ctype = 'k' elif ctypet == '抖机灵' or ctypet == 'l': ctype = 'l' - - elif ctypet == '没有': + elif ctypet == None: ctype = random.choice('abcdefghijkl') - params_value = {'c': ctype} - hitokoto = requests.get('https://v1.hitokoto.cn', params=params_value) - - return 0, hitokoto.json() + elif ctypet == 'help': + return 1, None, result_type else: - return 3, None + return 3, None, result_type params_value = {'c': ctype} hitokoto = requests.get('https://v1.hitokoto.cn', params=params_value) - return 0, hitokoto.json() + htype=hitokoto.json()['type'] + if htype=='a': + result_type = '动画' + elif htype=='b': + result_type = '动漫' + elif htype=='c': + result_type = '游戏' + elif htype=='d': + result_type = '文学' + elif htype=='e': + result_type = '原创' + elif htype=='f': + result_type = '来自网络' + elif htype=='g': + result_type = '其他' + elif htype=='h': + result_type = '影视' + elif htype=='i': + result_type = '诗词' + elif htype=='j': + result_type = '网易云' + elif htype=='k': + result_type = '哲学' + elif htype=='l': + result_type = '抖机灵' + else: + result_type = '无法获取' + return 0, hitokoto.json(), result_type + except IOError: - return 2, None - + return 2, None, result_type diff --git a/module/hitokoto/test.py b/module/hitokoto/test.py deleted file mode 100644 index e69de29..0000000