sycgram/plugins/ping.py

61 lines
1.9 KiB
Python
Raw Permalink Normal View History

2022-04-11 06:08:50 +00:00
from loguru import logger
2022-04-09 10:04:20 +00:00
from core import command
from pyrogram import Client
from pyrogram.types import Message
from tools.helpers import Parameters, show_cmd_tip, show_exception
2022-04-11 06:08:50 +00:00
from tools.poster import check_ip, check_ip_port, process_check_data
2022-04-11 02:05:12 +00:00
from tools.sessions import session
2022-04-09 10:04:20 +00:00
@Client.on_message(command('ip'))
async def ip(_: Client, msg: Message):
"""查询ip信息"""
cmd, address = Parameters.get(msg)
if not address:
2022-04-11 02:05:12 +00:00
return await show_cmd_tip(msg, cmd)
elif address == "me":
address = ''
2022-04-09 10:04:20 +00:00
async def get_api(api: str) -> str:
async with session.get(api) as resp:
if resp.status == 200:
data = await resp.json()
tmp = '\n'.join(f"{k}`{v}`" for k, v in data.items())
return tmp if tmp else "😂 No Response ~"
resp.raise_for_status()
try:
api = f"http://ip-api.com/json/{address}"
text = await get_api(api)
except Exception as e:
return await show_exception(msg, e)
else:
await msg.edit_text(text)
2022-04-11 06:08:50 +00:00
@Client.on_message(command("ipcheck"))
async def ip_checker(_: Client, msg: Message):
"""检测IP或者域名是否被阻断"""
cmd, args = Parameters.get_more(msg)
if len(args) == 1:
try:
resp = await check_ip(args[0])
except Exception as e:
logger.error(e)
return await show_exception(msg, e)
elif len(args) == 2:
try:
resp = await check_ip_port(args[0], args[1])
except Exception as e:
logger.error(e)
return await show_exception(msg, e)
else:
return await show_cmd_tip(msg, cmd)
try:
res = await process_check_data(len(args), resp=resp)
await msg.edit_text(f"🔎 Query `{' '.join(args)}`\n{res}")
except Exception as e:
logger.error(e)
await show_exception(msg, e)