diff --git a/EVEBot/bot.py b/EVEBot/bot.py index a945a51..99fccf2 100644 --- a/EVEBot/bot.py +++ b/EVEBot/bot.py @@ -31,6 +31,7 @@ nonebot.load_plugin('src.plugins.km') nonebot.load_plugin('src.plugins.kb') nonebot.load_plugin('src.plugins.checkServer') +nonebot.load_plugin('src.plugins.map') # 依赖 bind nonebot.load_plugin('src.plugins.test') # Please DO NOT modify this file unless you know what you are doing! diff --git a/EVEBot/src/plugins/bind/__init__.py b/EVEBot/src/plugins/bind/__init__.py index e8348e8..dc9feac 100644 --- a/EVEBot/src/plugins/bind/__init__.py +++ b/EVEBot/src/plugins/bind/__init__.py @@ -24,7 +24,7 @@ if not bind.bind : bind.bind = {} -bind_function = on_regex(r'^[\.。](bind|绑定)\s*\S+') +bind_function = on_regex(r'^[\.。](bind|绑定) \s*\S+') @bind_function.handle() async def _(bot: Bot, event: Event): global bind @@ -49,7 +49,7 @@ async def _(bot: Bot, event: Event): await bind_function.finish(message=Message('绑定成功')) -unbind_function = on_regex(r'^[\.。](unbind|解绑)\s*\S+') +unbind_function = on_regex(r'^[\.。](unbind|解绑) \s*\S+') @unbind_function.handle() async def _(bot: Bot, event: Event): global bind diff --git a/EVEBot/src/plugins/checkServer/__init__.py b/EVEBot/src/plugins/checkServer/__init__.py index c7010a0..47e8c98 100644 --- a/EVEBot/src/plugins/checkServer/__init__.py +++ b/EVEBot/src/plugins/checkServer/__init__.py @@ -34,8 +34,6 @@ async def check_server_every_1_time(): re = await client.get(url=f'https://esi.evetech.net/latest/status/?datasource=tranquility', headers=headers) except: return - if re.status_code != 200 : - return re_json = re.json() if 'start_time' in re_json : if flag : diff --git a/EVEBot/src/plugins/cmd/__init__.py b/EVEBot/src/plugins/cmd/__init__.py index db4ed7a..5583898 100644 --- a/EVEBot/src/plugins/cmd/__init__.py +++ b/EVEBot/src/plugins/cmd/__init__.py @@ -26,7 +26,7 @@ content = {} file.close() -load = on_regex(r'^[\.。](load|加载)\s*') +load = on_regex(r'^[\.。](load|加载)\s*$') @load.handle() async def _(bot: Bot, event: Event): global content @@ -61,7 +61,7 @@ async def _(bot: Bot, event: Event): else: await load.finish(message=Message(msg)) -cmd = on_regex(r'^[\.。](cmd|a|gl|攻略)\s*\S+') +cmd = on_regex(r'^[\.。](cmd|a|gl|攻略) \s*\S+') @cmd.handle() async def _(bot: Bot, event: Event): global content @@ -87,7 +87,7 @@ async def _(bot: Bot, event: Event): return await cmd.finish(message=Message('标题不正确,请检查标题!')) -list_cmd = on_regex(r'^[\.。](list)\s*') +list_cmd = on_regex(r'^[\.。](list)\s*$') @list_cmd.handle() async def _(bot: Bot, event: Event): global content @@ -97,7 +97,7 @@ async def _(bot: Bot, event: Event): if await util.isPass() or await util.isBan(event.user_id) : return - msg = '关键字列表:\n' + msg = '命令列表:\n' for k, _ in content.items() : msg = msg + k + ' ' diff --git a/EVEBot/src/plugins/help/__init__.py b/EVEBot/src/plugins/help/__init__.py index 339ace9..a3f10e8 100644 --- a/EVEBot/src/plugins/help/__init__.py +++ b/EVEBot/src/plugins/help/__init__.py @@ -32,6 +32,7 @@ async def _(bot: Bot, event: Event): '.tr / .tran / .翻译 <字符串> # 翻译\n' '.kb / .zkb # 查询人物基础信息\n' '.tool / .工具 / .常用 # 常用工具网址\n' + '.map / .地图 # 翻译地图名称\n' '管理员命令:\n' '.bind / .绑定 <别名>,<物品名> \n' '.unbind / .解绑 <别名>\n' diff --git a/EVEBot/src/plugins/jita/__init__.py b/EVEBot/src/plugins/jita/__init__.py index a72ff55..581d7f1 100644 --- a/EVEBot/src/plugins/jita/__init__.py +++ b/EVEBot/src/plugins/jita/__init__.py @@ -11,7 +11,7 @@ bind = pluginR('bind') -jita = on_regex(r'^[\.。](jita|吉他|jt)\s*\S+') +jita = on_regex(r'^[\.。](jita|吉他|jt) \s*\S+') @jita.handle() async def _(bot: Bot, event: Event): global bind diff --git a/EVEBot/src/plugins/jita/get_data.py b/EVEBot/src/plugins/jita/get_data.py index 7467ad1..f131208 100644 --- a/EVEBot/src/plugins/jita/get_data.py +++ b/EVEBot/src/plugins/jita/get_data.py @@ -36,8 +36,6 @@ async def get_price(name: str, num: int = 1): re = await client.get(url=f'https://esi.evetech.net/latest/markets/10000002/orders/?datasource=tranquility&order_type=all&type_id={itemID}', headers=headers) except: return '连接服务器失败,请稍后尝试!' - if re.status_code != 200 : - return '连接服务器失败,请稍后尝试!' re_json = re.json() buy = 0 sell = 0 diff --git a/EVEBot/src/plugins/kb/__init__.py b/EVEBot/src/plugins/kb/__init__.py index fca0e9b..7bf4fa7 100644 --- a/EVEBot/src/plugins/kb/__init__.py +++ b/EVEBot/src/plugins/kb/__init__.py @@ -32,9 +32,6 @@ async def _(bot: Bot, event: Event): except: await kb.finish(message=Message('当前网络连接错误,请稍后进行查询!')) return - if name_re.status_code != 200 : - await kb.finish(message=Message('当前网络连接错误,请稍后进行查询!')) - return name_json = name_re.json() if 'character' in name_json : @@ -48,9 +45,6 @@ async def _(bot: Bot, event: Event): except: await kb.finish(message=Message('zkb网连接失败,请稍后进行查询!')) return - if zkb_re.status_code != 200 : - await kb.finish(message=Message('zkb网连接失败,请稍后进行查询!')) - return zkb_json = zkb_re.json() if zkb_json['info'] is None : @@ -82,17 +76,15 @@ async def _(bot: Bot, event: Event): if corporation_str is None and 'corporationID' in zkb_json['info'] : try: corporation_re = await client.get(url=f"https://esi.evetech.net/latest/corporations/{zkb_json['info']['corporationID']}/?datasource=tranquility", headers=headers) - if corporation_re.status_code == 200 : - corporation_json = corporation_re.json() - corporation_str = corporation_json['name'] + ' [' + corporation_json['ticker'] + ']' + corporation_json = corporation_re.json() + corporation_str = corporation_json['name'] + ' [' + corporation_json['ticker'] + ']' except: pass if alliance_str is None and 'allianceID' in zkb_json['info'] : try: alliance_re = await client.get(url=f"https://esi.evetech.net/latest/alliances/{zkb_json['info']['allianceID']}/?datasource=tranquility", headers=headers) - if alliance_re.status_code == 200 : - alliance_json = alliance_re.json() - alliance_str = alliance_json['name'] + ' <' + alliance_json['ticker'] + '>' + alliance_json = alliance_re.json() + alliance_str = alliance_json['name'] + ' <' + alliance_json['ticker'] + '>' except: pass if corporation_str is not None : diff --git a/EVEBot/src/plugins/km/__init__.py b/EVEBot/src/plugins/km/__init__.py index 59f0b2e..9321cfa 100644 --- a/EVEBot/src/plugins/km/__init__.py +++ b/EVEBot/src/plugins/km/__init__.py @@ -3,12 +3,15 @@ from nonebot.adapters.cqhttp import Event, Bot, Message import nonebot from nonebot.plugin import require as pluginR +from nonebot.log import logger +import sys import time import json import asyncio import httpx import websockets import ssl +import math from pathlib import Path driver=get_driver() @@ -71,6 +74,7 @@ async def km(): try: re = await websocket.recv() except: + logger.error(f'websocket.recv() 错误 文件:{__file__} 行号:{sys._getframe().f_lineno}') break new_time = time.time() @@ -97,8 +101,7 @@ async def km(): try: zkb_re = await client.get(url=f'https://zkillboard.com/api/killID/{killID}/') except: - continue - if zkb_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue zkb_json = zkb_re.json() @@ -112,8 +115,7 @@ async def km(): try: esi_re = await client.get(url=f'https://esi.evetech.net/latest/killmails/{killID}/{killHash}/?datasource=tranquility') except: - continue - if esi_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue esi_json = esi_re.json() @@ -132,8 +134,7 @@ async def km(): try: ship_re = await client.get(url=f'https://esi.evetech.net/latest/universe/types/{ship_type_id}/?datasource=tranquility&language=zh', headers=headers) except: - continue - if ship_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue ship_json = ship_re.json() ship_name = ship_json['name'] @@ -147,8 +148,7 @@ async def km(): try: dead_re = await client.get(url=f'https://esi.evetech.net/latest/characters/{dead_id}/?datasource=tranquility', headers=headers) except: - continue - if dead_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue dead_json = dead_re.json() dead_name = dead_json['name'] @@ -156,8 +156,7 @@ async def km(): try: dead_corporation_re = await client.get(url=f'https://esi.evetech.net/latest/corporations/{dead_json["corporation_id"]}/?datasource=tranquility', headers=headers) except: - continue - if dead_corporation_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue dead_corporation_json = dead_corporation_re.json() dead_corporation_ticker = '[' + dead_corporation_json['ticker'] + ']' @@ -165,8 +164,7 @@ async def km(): try: dead_alliance_re = await client.get(url=f'https://esi.evetech.net/latest/alliances/{dead_json["alliance_id"]}/?datasource=tranquility', headers=headers) except: - continue - if dead_alliance_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue dead_alliance_json = dead_alliance_re.json() dead_alliance_ticker = '<' + dead_alliance_json['ticker'] + '>' @@ -188,8 +186,7 @@ async def km(): try: killer_re = await client.get(url=f'https://esi.evetech.net/latest/characters/{killer_id}/?datasource=tranquility', headers=headers) except: - continue - if killer_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue killer_json = killer_re.json() killer_name = killer_json['name'] @@ -197,8 +194,7 @@ async def km(): try: killer_corporation_re = await client.get(url=f'https://esi.evetech.net/latest/corporations/{killer_json["corporation_id"]}/?datasource=tranquility', headers=headers) except: - continue - if killer_corporation_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue killer_corporation_json = killer_corporation_re.json() killer_corporation_ticker = '[' + killer_corporation_json['ticker'] + ']' @@ -208,8 +204,7 @@ async def km(): try: killer_alliance_re = await client.get(url=f'https://esi.evetech.net/latest/alliances/{killer_json["alliance_id"]}/?datasource=tranquility', headers=headers) except: - continue - if killer_alliance_re.status_code != 200 : + logger.error(f'文件:{__file__} 行号:{sys._getframe().f_lineno} killID:{killID}') continue killer_alliance_json = killer_alliance_re.json() killer_alliance_ticker = '<' + killer_alliance_json['ticker'] + '>' @@ -232,12 +227,34 @@ async def km(): region_re = await client.get(url=f'https://esi.evetech.net/latest/universe/regions/{region_id}/?datasource=tranquility&language=zh', headers=headers) region_json = region_re.json() region_name = region_json['name'] - msg = msg + f'位置: {system_name} / {constellation_name} / {region_name}\n' + msg = msg + f'星系: {system_name} / {constellation_name} / {region_name}\n' + try: + moon_id = zkb_json[0]['zkb']['locationID'] + moon_re = await client.get(url=f'https://esi.evetech.net/latest/universe/moons/{moon_id}/?datasource=tranquility', headers=headers) + moon_json = moon_re.json() + moon_name = moon_json['name'] + dx = math.fabs(esi_json['victim']['position']['x'] - moon_json['position']['x']) + dy = math.fabs(esi_json['victim']['position']['y'] - moon_json['position']['y']) + dz = math.fabs(esi_json['victim']['position']['z'] - moon_json['position']['z']) + distance = math.sqrt(dx**2 + dy**2 + dz**2) + if distance >= 149597870700 * 0.1 : + distance = distance / 149597870700 + unit = 'AU' + elif distance >= 1000 : + distance = distance / 1000 + unit = 'km' + else: + unit = 'm' + msg = msg + f'位置: {moon_name} | 距离: {distance:,.2f} {unit}\n' + except: + pass except: pass if zkb_json[0]['zkb']['solo'] == True : msg = msg + 'SOLO\n' + else: + msg = msg + f'签名人数: {len(esi_json["attackers"])}\n' msg = msg + '\n' diff --git a/EVEBot/src/plugins/map/__init__.py b/EVEBot/src/plugins/map/__init__.py new file mode 100644 index 0000000..438e126 --- /dev/null +++ b/EVEBot/src/plugins/map/__init__.py @@ -0,0 +1,88 @@ +from nonebot import on_regex +from nonebot.adapters.cqhttp import Event, Bot, Message +from nonebot.plugin import require as pluginR +import httpx +import urllib.parse + +tool = pluginR('tool') + +util = pluginR('util') + +bind = pluginR('bind') + +headers = {"accept": "application/json", "Cache-Control": "no-cache"} + +client = httpx.AsyncClient() + +search_map = on_regex(r'^[\.。](map|地图) \s*\S+') +@search_map.handle() +async def _(bot: Bot, event: Event): + global bind + + if not (event.message_type == 'group' and event.group_id in tool.group_ids) : + return + + if await util.isPass() or await util.isBan(event.user_id) : + return + + name = str(event.get_message()).split(' ', 1)[1].strip() + + if name in bind.bind : + name = bind.bind[name] + + if len(name) <= 2 : + name = f' {name} ' + + url_name = urllib.parse.quote(name) + + input_lang = 'zh' + output_lang = 'en' + + for i in range(2) : + try: + esi_re = await client.get(url=f'https://esi.evetech.net/latest/search/?categories=constellation,region,solar_system&datasource=tranquility&language={input_lang}&search={url_name}&strict=true', headers=headers) + except: + await search_map.finish(message=Message('当前网络连接错误,请稍后进行查询!')) + return + esi_json = esi_re.json() + if ('region' not in esi_json) and ('constellation' not in esi_json) and ('solar_system' not in esi_json) : + input_lang = 'en' + output_lang = 'zh' + else: + break + + + if 'region' in esi_json : + region_id = esi_json['region'][0] + try: + region_re = await client.get(url=f'https://esi.evetech.net/latest/universe/regions/{region_id}/?datasource=tranquility&language={output_lang}', headers=headers) + except: + await search_map.finish(message=Message('当前网络连接错误,请稍后进行查询!')) + return + region_json = region_re.json() + region_name = region_json['name'] + msg = f'星域: {name} / {region_name}' + elif 'constellation' in esi_json : + constellation_id = esi_json['constellation'][0] + try: + constellation_re = await client.get(url=f'https://esi.evetech.net/latest/universe/constellations/{constellation_id}/?datasource=tranquility&language={output_lang}', headers=headers) + except: + await search_map.finish(message=Message('当前网络连接错误,请稍后进行查询!')) + return + constellation_json = constellation_re.json() + constellation_name = constellation_json['name'] + msg = f'星座: {name} / {constellation_name}' + elif 'solar_system' in esi_json : + system_id = esi_json['solar_system'][0] + try: + system_re = await client.get(url=f'https://esi.evetech.net/latest/universe/systems/{system_id}/?datasource=tranquility&language={output_lang}', headers=headers) + except: + await search_map.finish(message=Message('当前网络连接错误,请稍后进行查询!')) + return + system_json = system_re.json() + system_name = system_json['name'] + msg = f'星系: {name} / {system_name}' + else: + msg = '没有该名称对应的星域/星座/星系,请检查输入是否正确!' + + await search_map.finish(message=Message(msg)) diff --git a/EVEBot/src/plugins/search/__init__.py b/EVEBot/src/plugins/search/__init__.py index 0219293..61cef3a 100644 --- a/EVEBot/src/plugins/search/__init__.py +++ b/EVEBot/src/plugins/search/__init__.py @@ -10,7 +10,7 @@ util = pluginR('util') -search = on_regex(r'^[\.。](search|搜索)\s*\S+') +search = on_regex(r'^[\.。](search|搜索) \s*\S+') @search.handle() async def _(bot: Bot, event: Event): if not (event.message_type == 'group' and event.group_id in group_ids) : diff --git a/EVEBot/src/plugins/settings/__init__.py b/EVEBot/src/plugins/settings/__init__.py index 443cb2b..0ba2987 100644 --- a/EVEBot/src/plugins/settings/__init__.py +++ b/EVEBot/src/plugins/settings/__init__.py @@ -29,7 +29,7 @@ if 'ban' not in settings.data : settings.data['ban'] = [] -limit = on_regex(r'^[\.。](limit|限制)\s+\S+') +limit = on_regex(r'^[\.。](limit|限制) \s+\S+') @limit.handle() async def _(bot: Bot, event: Event): global util diff --git a/EVEBot/src/plugins/suit/__init__.py b/EVEBot/src/plugins/suit/__init__.py index 2d9af3e..32c90f0 100644 --- a/EVEBot/src/plugins/suit/__init__.py +++ b/EVEBot/src/plugins/suit/__init__.py @@ -17,7 +17,7 @@ client = httpx.AsyncClient() -suit = on_regex(r'^[\.。](suit)\s*\S+') +suit = on_regex(r'^[\.。](suit) \s*\S+') @suit.handle() async def _(bot: Bot, event: Event): global bind @@ -72,8 +72,6 @@ async def get_price_all(name: str): marketGroup_re = await client.get(url=f'https://esi.evetech.net/latest/markets/groups/{marketGroupID}/?datasource=tranquility&language=zh', headers=headers) except: return [ '连接服务器失败,请稍后尝试!' ] - if marketGroup_re.status_code != 200 : - return [ '连接服务器失败,请稍后尝试!' ] marketGroup_json = marketGroup_re.json() items = marketGroup_json['types'] @@ -90,8 +88,6 @@ async def get_price_all(name: str): re = await client.get(url=f'https://esi.evetech.net/latest/markets/10000002/orders/?datasource=tranquility&order_type=all&type_id={itemID}', headers=headers) except: return [ '连接服务器失败,请稍后尝试!' ] - if re.status_code != 200 : - return [ '连接服务器失败,请稍后尝试!' ] re_json = re.json() buy = 0 sell = 0 diff --git a/EVEBot/src/plugins/test/__init__.py b/EVEBot/src/plugins/test/__init__.py index 99f2d3e..22f8eed 100644 --- a/EVEBot/src/plugins/test/__init__.py +++ b/EVEBot/src/plugins/test/__init__.py @@ -2,3 +2,8 @@ import nonebot config = nonebot.get_driver().config.dict() ''' + +'''状态码 +if re.status_code != 200 : + return +''' diff --git a/EVEBot/src/plugins/translate/__init__.py b/EVEBot/src/plugins/translate/__init__.py index a3af54f..7f79950 100644 --- a/EVEBot/src/plugins/translate/__init__.py +++ b/EVEBot/src/plugins/translate/__init__.py @@ -11,7 +11,7 @@ bind = pluginR('bind') -tr = on_regex(r'^[\.。](tr|tran|翻译)\s*\S+') +tr = on_regex(r'^[\.。](tr|tran|翻译) \s*\S+') @tr.handle() async def _(bot: Bot, event: Event): if not (event.message_type == 'group' and event.group_id in group_ids) : diff --git a/README.md b/README.md index fa66ad6..e2669d2 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ * [go-cqhttp](#go-cqhttp-1) * [NoneBot2](#nonebot2-1) - [BUG](#bug) + * [更新版本后提示插件已存在错误](#更新版本后提示插件已存在错误) - [性能](#性能) ## 功能 @@ -53,6 +54,8 @@ .tr / .tran / .翻译 <关键字> # 从zkillboard网获得人物信息 .kb / .zkb +# 翻译中文星域/星座/星系名到英文 +.map / .地图 <星域/星座/星系名> ### 管理员命令 # 绑定物品的别名,比如绑定MTU到移动式牵引装置,方便查价 @@ -286,7 +289,13 @@ python3 bot.py ## BUG +### 更新版本后提示插件已存在错误 +关闭机器人,在 `EVEBot` 目录中执行该命令后启动机器人: + +```shell +find ./ -name __pycache__ | xargs rm -rf +``` ## 性能 diff --git a/Tools/README.md b/Tools/README.md index a7c7360..110a546 100644 --- a/Tools/README.md +++ b/Tools/README.md @@ -30,3 +30,15 @@ pip3 install pillow python3 pic_resize.py ``` +## `itemname_translate.py` + +使用前需要安装 python 包: + +```shell +pip3 install pandas openpyxl +``` + +解析 `typeIDs.yaml` 文件,导出到 `物品名中英对照表.xlsx` Excel 表格文件 + +因为数据量庞大,所以执行时间很长,需要5分钟左右 + diff --git a/Tools/itemname_translate.py b/Tools/itemname_translate.py new file mode 100644 index 0000000..0f4f78a --- /dev/null +++ b/Tools/itemname_translate.py @@ -0,0 +1,17 @@ +import pandas as pd +import yaml + +old_file = open('typeIDs.yaml', 'r', encoding='utf-8') +old_data = yaml.load(old_file.read(), Loader=yaml.FullLoader) +old_file.close() + +new_data = [] + +for k, v in old_data.items() : + node = { 'id': k, 'en': v['name']['en'] } + if 'zh' in v['name'] : + node['zh'] = v['name']['zh'] + new_data.append(node) + +out = pd.DataFrame(new_data) +out.to_excel('物品名中英对照表.xlsx', encoding='utf-8', index=False)