nonebot-plugin-mccheck 0.1.9__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nonebot-plugin-mccheck might be problematic. Click here for more details.

@@ -0,0 +1,221 @@
1
+ from nonebot import on_command
2
+ from nonebot.plugin import PluginMetadata
3
+ from nonebot.params import Arg, CommandArg, ArgPlainText
4
+ from nonebot.matcher import Matcher
5
+ from nonebot.exception import FinishedException
6
+ from nonebot.adapters.onebot.v11 import Message, MessageSegment, GroupMessageEvent, Bot
7
+ from .untils import (
8
+ resolve_srv,
9
+ is_invalid_address,
10
+ ColoredTextImage,
11
+ parse_motd,
12
+ )
13
+ from .data_source import MineStat
14
+ from .language import lang_data
15
+ from nonebot.log import logger
16
+ from .config import Config
17
+ from .config import config as plugin_config
18
+ import re
19
+ import asyncio
20
+ import traceback
21
+ import sys
22
+ import base64
23
+
24
+ __version__ = "0.1.9"
25
+
26
+ __plugin_meta__ = PluginMetadata(
27
+ name="Minecraft查服",
28
+ description="Minecraft服务器状态查询,支持IPv6",
29
+ type="application",
30
+ homepage="https://github.com/molanp/nonebot_plugin_mccheck",
31
+ config=Config,
32
+ usage=f"""
33
+ Minecraft服务器状态查询,支持IPv6
34
+ 用法:
35
+ 查服 [ip]:[端口] / 查服 [ip]
36
+ 设置语言 zh-cn
37
+ 当前语言
38
+ 语言列表
39
+ eg:
40
+ mcheck ip:port / mcheck ip
41
+ set_lang en
42
+ lang_now
43
+ lang_list
44
+ """.strip(),
45
+ extra={
46
+ "unique_name": "MCCheck",
47
+ "example": "mcheck",
48
+ "author": "molanp <luotian233@foxmail.com>",
49
+ "version": __version__,
50
+ },
51
+ )
52
+
53
+
54
+ lang = plugin_config.language
55
+ if lang == None:
56
+ lang = "zh-cn"
57
+
58
+ check = on_command("查服", aliases={'mcheck'}, priority=5, block=True)
59
+ lang_change = on_command("设置语言", aliases={'set_lang'}, priority=5, block=True)
60
+ lang_now = on_command("当前语言", aliases={'lang_now'}, priority=5, block=True)
61
+ lang_list = on_command("语言列表", aliases={'lang_list'}, priority=5, block=True)
62
+
63
+
64
+ @check.handle()
65
+ async def handle_first_receive(bot_: Bot, event_: GroupMessageEvent, matcher: Matcher, args: Message = CommandArg()):
66
+ plain_text = args.extract_plain_text()
67
+ if plain_text:
68
+ global bot, event
69
+ bot, event = bot_, event_
70
+ matcher.set_arg("host", args)
71
+
72
+
73
+ @check.got("host", prompt="IP?")
74
+ async def handle_host(host_name: str = ArgPlainText("host")):
75
+ address, port = parse_host(host_name)
76
+
77
+ if not str(port).isdigit() or not (0 <= int(port) <= 65535):
78
+ await check.finish(lang_data[lang]["where_port"], at_sender=True)
79
+
80
+ if is_invalid_address(address):
81
+ await check.finish(lang_data[lang]["where_ip"], at_sender=True)
82
+
83
+ await get_info(address, port)
84
+
85
+
86
+ async def get_info(ip, port):
87
+ global bot, event, ms
88
+
89
+ try:
90
+ srv = await resolve_srv(ip, port)
91
+ ms = await asyncio.to_thread(MineStat(srv[0], int(srv[1]), timeout = 1))
92
+ if ms.online:
93
+ if plugin_config.type == 0:
94
+ result = build_result(ms)
95
+ await send_image_message(result, ms.favicon, ms.favicon_b64)
96
+ else:
97
+ result = build_result(ms, text=True)
98
+ await send_text_message(result, ms.favicon, ms.favicon_b64)
99
+ else:
100
+ await check.finish(Message(f'{lang_data[lang]["offline"]}'), at_sender=True)
101
+ except FinishedException:
102
+ pass
103
+ except BaseException as e:
104
+ await handle_exception(e)
105
+
106
+
107
+ def parse_host(host_name):
108
+ if '.' in host_name:
109
+ parts = host_name.strip().split(':')
110
+ address = parts[0]
111
+ port = parts[1] if len(parts) > 1 else 0
112
+ else:
113
+ pattern = r'\[(.+)\](?::(\d+))?$'
114
+ match = re.match(pattern, host_name)
115
+ address = match.group(1)
116
+ port = match.group(2) if match.group(2) else 0
117
+
118
+ return address, port
119
+
120
+
121
+ def build_result(ms, text=False):
122
+ status = f'{ms.connection_status}|{lang_data[lang][str(ms.connection_status)]}'
123
+ base_result = (
124
+ f'\n{lang_data[lang]["version"]}{ms.version}'
125
+ f'\n{lang_data[lang]["slp_protocol"]}{ms.slp_protocol}'
126
+ f'\n{lang_data[lang]["address"]}{ms.address}'
127
+ f'\n{lang_data[lang]["port"]}{ms.port}'
128
+ f'\n{lang_data[lang]["delay"]}{ms.latency}ms'
129
+ )
130
+
131
+ if 'BEDROCK' in str(ms.slp_protocol):
132
+ base_result += f'\n{lang_data[lang]["gamemode"]}{ms.gamemode}'
133
+ if text:
134
+ motd_part = f'\n{lang_data[lang]["motd"]}{parse_motd(ms.stripped_motd)}'
135
+ else:
136
+ motd_part = f'\n{lang_data[lang]["motd"]}{parse_motd(ms.motd)}[#RESET]'
137
+
138
+ result = (
139
+ base_result +
140
+ motd_part +
141
+ f'\n{lang_data[lang]["players"]}{ms.current_players}/{ms.max_players}'
142
+ f'\n{lang_data[lang]["status"]}{status}'
143
+ )
144
+
145
+ return result
146
+
147
+
148
+ async def send_text_message(result, favicon, favicon_b64):
149
+ if favicon is not None and favicon != "":
150
+ await check.finish(Message([
151
+ Message(result),
152
+ MessageSegment.text('favicon:'),
153
+ MessageSegment.image(base64.b64decode(favicon_b64.split(",")[1]))
154
+ ]), at_sender=True)
155
+ else:
156
+ await check.finish(Message(result), at_sender=True)
157
+
158
+
159
+ async def send_image_message(result, favicon, favicon_b64):
160
+ if favicon is not None and favicon != "":
161
+ await check.finish(Message([
162
+ MessageSegment.image(
163
+ (await ColoredTextImage(result).draw_text_with_style()).pic2bytes()
164
+ ),
165
+ MessageSegment.text('Favicon:'),
166
+ MessageSegment.image(base64.b64decode(favicon_b64.split(",")[1]))
167
+ ]), at_sender=True)
168
+ else:
169
+ await check.finish(MessageSegment.image(
170
+ (await ColoredTextImage(result).draw_text_with_style()).pic2bytes()
171
+ ), at_sender=True)
172
+
173
+
174
+ async def handle_exception(e):
175
+ error_type = type(e).__name__
176
+ error_message = str(e)
177
+ error_traceback = traceback.extract_tb(sys.exc_info()[2])[-2]
178
+
179
+ result = f'ERROR:\nType: {error_type}\nMessage: {error_message}\nLine: {error_traceback.lineno}\nFile: {error_traceback.filename}\nFunction: {error_traceback.name}'
180
+ logger.error(result)
181
+ try:
182
+ await check.finish(Message(result), at_sender=True)
183
+ except FinishedException:
184
+ pass
185
+
186
+
187
+ @lang_change.handle()
188
+ async def handle_first_receive(matcher: Matcher, args: Message = CommandArg()):
189
+ plain_text = args.extract_plain_text()
190
+ if plain_text:
191
+ matcher.set_arg("lang_", args)
192
+
193
+
194
+ @lang_change.got("lang_", prompt="Language?")
195
+ async def handle_host(lang_: Message = Arg(), language_name: str = ArgPlainText("lang_")):
196
+ await lang_change.finish(Message(await change(language_name)), at_sender=True)
197
+
198
+
199
+ async def change(language: str):
200
+ global lang
201
+ try:
202
+ a = lang_data[language]
203
+ except:
204
+ return f'No language named "{language}"!'
205
+ else:
206
+ if language == lang:
207
+ return f'The language is already "{language}"!'
208
+ else:
209
+ lang = language
210
+ return f'Change to "{language}" success!'
211
+
212
+
213
+ @lang_now.handle()
214
+ async def _():
215
+ await lang_now.send(Message(f' Language: {lang}.'), at_sender=True)
216
+
217
+
218
+ @lang_list.handle()
219
+ async def _():
220
+ i = '\n'.join(list(lang_data.keys()))
221
+ await lang_list.send(Message(f"Language:\n{i}"), at_sender=True)
@@ -0,0 +1,18 @@
1
+ from pydantic import Field, BaseModel
2
+ from nonebot.plugin import get_plugin_config
3
+
4
+
5
+ class ScopedConfig(BaseModel):
6
+
7
+ language: str = Field(default="zh-cn")
8
+ """插件渲染图片所使用的语言"""
9
+ type: int = Field(default=0)
10
+ """插件发送的消息类型"""
11
+
12
+
13
+ class Config(BaseModel):
14
+ mcc: ScopedConfig = Field(default_factory=ScopedConfig)
15
+ """MCCheck Config"""
16
+
17
+
18
+ config: ScopedConfig = get_plugin_config(Config).mcc