Skip to content

Commit

Permalink
实现异步
Browse files Browse the repository at this point in the history
  • Loading branch information
molanp committed Aug 21, 2024
1 parent 4a65447 commit 4d6c454
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
7 changes: 3 additions & 4 deletions nonebot_plugin_mccheck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
is_invalid_address,
ColoredTextImage,
parse_motd,
get_mc,
readInfo
)
from .data_source import MineStat
from nonebot.log import logger
from .config import Config
from .config import config as plugin_config
Expand All @@ -20,7 +20,7 @@
import sys
import base64

__version__ = "0.1.10"
__version__ = "0.1.11"

__plugin_meta__ = PluginMetadata(
name="Minecraft查服",
Expand Down Expand Up @@ -86,8 +86,7 @@ async def get_info(ip, port):

try:
srv = await resolve_srv(ip, port)
#无法实现异步
ms = MineStat(srv[0], int(srv[1]), timeout = 1)
ms = await get_mc(srv[0], int(srv[1]), timeout = 1)
if ms.online:
if plugin_config.type == 0:
result = build_result(ms)
Expand Down
31 changes: 21 additions & 10 deletions nonebot_plugin_mccheck/untils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
import os
import asyncio
import dns.resolver
from .data_source import MineStat


def readInfo(file):
with open(os.path.join(os.path.dirname(__file__), file), "r", encoding="utf-8") as f:
return json.loads((f.read()).strip())


def create_mine_stat(host, port, timeout):
ms = MineStat(host, port, timeout)
return ms


async def get_mc(host, port, timeout=1):
ms = await asyncio.to_thread(create_mine_stat, host, port, timeout)
return ms


def is_invalid_address(address):
domain_pattern = r"^(?:(?!_)(?!-)(?!.*--)[a-zA-Z0-9\u4e00-\u9fa5\-_]{1,63}\.?)+[a-zA-Z\u4e00-\u9fa5]{2,}$"
ipv4_pattern = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Expand All @@ -21,22 +33,23 @@ def is_invalid_address(address):
match_ipv6 = re.match(ipv6_pattern, address)

return (match_domain is None) and (match_ipv4 is None) and (match_ipv6 is None)


async def resolve_srv(ip: str, port: int = 0):
result = await asyncio.to_thread(resolve_srv_sync, ip, port)
return result


def resolve_srv_sync(ip: str, port: int = 0):
resolver = dns.resolver.Resolver()
resolver.nameservers = ['223.5.5.5', '1.1.1.1']

try:
response = resolver.resolve(f'_minecraft._tcp.{ip}', 'SRV')

if not response:
return [ip, port]

for rdata in response:
address = str(rdata.target).rstrip('.')
if port == 0:
Expand All @@ -46,6 +59,7 @@ def resolve_srv_sync(ip: str, port: int = 0):
pass
return [ip, port]


def parse_motd(json_data):
"""
解析MOTD数据并转换为带有自定义十六进制颜色标记的字符串。
Expand Down Expand Up @@ -166,10 +180,6 @@ def __init__(self, text: str, background_color: tuple[int, int, int] = (249, 246
self.italic_font_path = os.path.join(
os.path.dirname(__file__), "font", "Italic.ttf")
self.font_size = 40
#width, height = self._calculate_dimensions(text)
#self.image = Image.new('RGB', (width, height), self.background_color)
#self.draw = ImageDraw.Draw(self.image)
#self.draw_text_with_style(text)

def _calculate_dimensions(self, text: str) -> tuple[int, int]:
"""
Expand Down Expand Up @@ -233,7 +243,7 @@ async def draw_text_with_style(self) -> None:
width, height = self._calculate_dimensions(text)
self.image = Image.new('RGB', (width, height), self.background_color)
self.draw = ImageDraw.Draw(self.image)
#self.draw_text_with_style(text)
# self.draw_text_with_style(text)

bold = italic = underline = strikethrough = False
current_color = (0, 0, 0)
Expand Down Expand Up @@ -328,4 +338,5 @@ def pic2bytes(self) -> bytes:
byte_io = io.BytesIO()
self.image.save(byte_io, format='PNG')
byte_io.seek(0)
return byte_io.getvalue()
return byte_io.getvalue()

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-mccheck"
version = "0.1.10"
version = "0.1.11"
description = "Minecraft服务器状态查询,支持IPv6"
readme = "README.md"
requires-python = ">=3.9"
Expand Down

0 comments on commit 4d6c454

Please sign in to comment.