PaiGram/plugins/genshin/help_raw.py

50 lines
2.0 KiB
Python
Raw Normal View History

import os
from typing import Optional
import aiofiles
from bs4 import BeautifulSoup
from telegram import Update
from telegram.ext import CallbackContext
from core.plugin import Plugin, handler
from utils.log import logger
__all__ = ("HelpRawPlugin",)
class HelpRawPlugin(Plugin):
def __init__(self):
self.help_raw: Optional[str] = None
async def initialize(self):
2023-05-10 09:30:28 +00:00
file_path = os.path.join(os.getcwd(), "resources", "bot", "help", "help.jinja2")
async with aiofiles.open(file_path, mode="r", encoding="utf-8") as f:
html_content = await f.read()
2023-11-23 03:34:33 +00:00
soup = BeautifulSoup(html_content, "html.parser")
commands = []
command_div = soup.find_all("div", class_="command")
for div in command_div:
2023-11-23 03:34:33 +00:00
command_name_div = div.find("div", class_="command-name")
command_description_div = div.find("div", class_="command-description")
if command_name_div and command_description_div:
command_name_div_text = command_name_div.text.strip()
if command_name_div_text.startswith(r"@{{bot_username}}"):
command_name_div_text = command_name_div_text.replace(
r"@{{bot_username}}", self.application.telegram.bot.name
)
commands.append(f"{command_name_div_text} - {command_description_div.text.strip()}")
if commands:
self.help_raw = "\n".join(commands)
@handler.command(command="help_raw", block=False)
async def start(self, update: Update, _: CallbackContext):
2023-11-23 03:34:33 +00:00
message = update.effective_message
2024-03-10 12:40:26 +00:00
self.log_user(update, logger.info, "发出 help_raw 命令")
2023-11-23 03:34:33 +00:00
if self.help_raw is None:
await self.initialize()
if self.help_raw is None:
await message.reply_text("出错了呜呜呜~派蒙没有找到任何帮助信息")
return
await message.reply_text(self.help_raw, allow_sending_without_reply=True)