Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/server/server.py

155 lines
6.4 KiB
Python
Raw Normal View History

2020-06-13 12:43:43 +00:00
import json
2020-08-12 16:01:34 +00:00
import re
2021-01-08 12:30:59 +00:00
import traceback
2020-08-12 16:01:34 +00:00
2020-09-19 10:35:13 +00:00
import aiohttp
2020-08-12 16:01:34 +00:00
2021-02-01 15:13:11 +00:00
2021-02-11 15:10:14 +00:00
async def server(address, raw=False, showplayer=False):
2020-08-15 06:57:00 +00:00
matchObj = re.match(r'(.*):(.*)', address, re.M | re.I)
2020-07-18 12:58:39 +00:00
servers = []
2020-08-12 16:01:34 +00:00
2021-01-09 11:42:03 +00:00
if matchObj:
serip = matchObj.group(1)
port1 = matchObj.group(2)
else:
serip = address
port1 = '25565'
2021-02-10 11:53:59 +00:00
matchserip = re.match(r'(.*?)\.(.*?)\.(.*?)\.(.*?)', serip)
2021-02-10 11:49:57 +00:00
if matchserip:
2021-02-10 12:17:00 +00:00
print(matchserip.group(1), matchserip.group(2),
matchserip.group(3), matchserip.group(4))
2021-02-10 11:51:19 +00:00
try:
if matchserip.group(1) == '192':
if matchserip.group(2) == '168':
return '¿'
if matchserip.group(1) == '172':
if 16 <= int(matchserip.group(2)) <= 31:
return '¿'
if matchserip.group(1) == '10':
if 0 <= int(matchserip.group(2)) <= 255:
return '¿'
2021-02-10 12:14:37 +00:00
if matchserip.group(1) == '0':
if matchserip.group(2) == '0':
if matchserip.group(3) == '0':
2021-02-10 12:18:11 +00:00
return '¿'
2021-02-10 11:51:19 +00:00
except:
traceback.print_exc()
2021-01-09 11:42:03 +00:00
2020-06-13 12:43:43 +00:00
try:
2021-01-09 11:42:03 +00:00
url = 'http://motd.wd-api.com/java?ip=' + serip + '&port=' + port1
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=20)) as req:
if req.status != 200:
print(await req.text())
print(f"请求时发生错误:{req.status}")
else:
motd = await req.text()
file = json.loads(motd)
try:
if file['code'] == 200:
servers.append('[JE]')
jejson = file['data']
if 'description' in jejson:
description = jejson['description']
if 'text' in description:
2021-02-01 15:13:11 +00:00
servers.append(str(description['text']))
2021-01-09 11:42:03 +00:00
elif 'extra' in description:
extra = description['extra']
text = []
qwq = ''
for item in extra[:]:
2021-02-01 15:13:11 +00:00
text.append(str(item['text']))
2021-01-09 11:42:03 +00:00
servers.append(qwq.join(text))
else:
2021-02-01 15:13:11 +00:00
servers.append(str(description))
2020-08-12 16:01:34 +00:00
2021-01-09 11:42:03 +00:00
if 'players' in jejson:
onlinesplayer = f"在线玩家:{str(jejson['players']['online'])} / {str(jejson['players']['max'])}"
servers.append(onlinesplayer)
2021-02-11 15:10:14 +00:00
if showplayer:
playerlist = []
for x in jejson['players']['sample']:
playerlist.append(x['name'])
servers.append('当前在线玩家:\n' + '\n'.join(playerlist))
2021-01-09 11:42:03 +00:00
if 'version' in jejson:
versions = "游戏版本:" + file['data']['version']['name']
servers.append(versions)
servers.append(serip + ':' + port1)
2021-01-08 12:30:59 +00:00
else:
2021-02-16 14:24:30 +00:00
servers.append('获取JE服务器信息失败。')
2021-01-09 11:42:03 +00:00
except Exception:
traceback.print_exc()
servers.append("[JE]\n发生错误调用API时发生错误。")
except Exception:
2021-02-16 14:26:10 +00:00
servers.append('获取JE服务器信息失败。')
2021-01-09 11:42:03 +00:00
traceback.print_exc()
2021-02-16 14:24:30 +00:00
awa = '\n'
servers.append("[30秒后撤回本消息]")
print(servers)
if raw:
return awa.join(servers)
return re.sub(r'§\w', "", awa.join(servers))
async def server_be(address, raw=False):
matchObj = re.match(r'(.*):(.*)', address, re.M | re.I)
servers = []
if matchObj:
serip = matchObj.group(1)
port2 = matchObj.group(2)
else:
serip = address
port2 = '19132'
matchserip = re.match(r'(.*?)\.(.*?)\.(.*?)\.(.*?)', serip)
if matchserip:
print(matchserip.group(1), matchserip.group(2),
matchserip.group(3), matchserip.group(4))
try:
if matchserip.group(1) == '192':
if matchserip.group(2) == '168':
return '¿'
if matchserip.group(1) == '172':
if 16 <= int(matchserip.group(2)) <= 31:
return '¿'
if matchserip.group(1) == '10':
if 0 <= int(matchserip.group(2)) <= 255:
return '¿'
if matchserip.group(1) == '0':
if matchserip.group(2) == '0':
if matchserip.group(3) == '0':
return '¿'
except:
traceback.print_exc()
2021-01-09 11:42:03 +00:00
try:
beurl = 'http://motd.wd-api.com/bedrock?ip=' + serip + '&port=' + port2
print(beurl)
2021-01-09 11:51:23 +00:00
async with aiohttp.ClientSession() as session2:
2021-02-16 14:24:30 +00:00
async with session2.get(beurl, timeout=aiohttp.ClientTimeout(total=20)) as req:
2021-01-09 11:42:03 +00:00
if req.status != 200:
print(await req.text())
print(f"请求时发生错误:{req.status}")
2020-06-13 12:43:43 +00:00
else:
2021-01-09 11:42:03 +00:00
bemotd = await req.text()
bejson = json.loads(bemotd)
edition, motd_1, protocol, version_name, player_count, max_players, unique_id, motd_2, \
game_mode, game_mode_num, port_v4, port_v6, nothing_here = bejson['motd'].split(';')
2021-02-01 15:13:11 +00:00
bemsg = '[BE]\n' + \
motd_1 + ' - ' + motd_2 + \
'\n在线玩家:' + player_count + '/' + max_players + \
'\n游戏版本:' + edition + version_name + \
'\n游戏模式:' + game_mode
2021-01-09 11:42:03 +00:00
servers.append(bemsg)
2021-01-08 12:30:59 +00:00
2021-01-09 11:42:03 +00:00
except Exception:
2021-02-16 14:24:30 +00:00
servers.append('获取BE服务器信息失败。')
2021-01-09 11:42:03 +00:00
traceback.print_exc()
2021-02-16 14:24:30 +00:00
awa = '\n'
servers.append("[30秒后撤回本消息]")
print(servers)
if raw:
return awa.join(servers)
return re.sub(r'§\w', "", awa.join(servers))