自定义 context 类型并在 help 命令实现相应代码

This commit is contained in:
洛水居室 2022-06-24 19:48:19 +08:00
parent b07e3e7602
commit e63e5d8995
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
4 changed files with 36 additions and 11 deletions

View File

@ -43,7 +43,7 @@ def register_handlers(application: Application, service: BaseService):
application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block))
# 初始化
plugins_help = Help(service)
plugins_help = Help()
inline = Inline(service)
auth = Auth(service)
gacha = Gacha(service)

14
main.py
View File

@ -1,9 +1,10 @@
import asyncio
from warnings import filterwarnings
from telegram.ext import Application
from telegram.ext import Application, ContextTypes
from telegram.warnings import PTBUserWarning
from utils.base import PaimonContext
from config import config
from handler import register_handlers
from logger import Log
@ -39,7 +40,16 @@ def main() -> None:
# 构建BOT
Log.info("构建BOT")
application = Application.builder().token(config.TELEGRAM["token"]).build()
# 自定义 context 类型
context_types = ContextTypes(context=PaimonContext)
application = Application.builder().token(config.TELEGRAM["token"]).context_types(context_types).build()
# 保存实例化的类到 bot_data
# 这样在每个实例去获取 service 时
# 通过 PaimonContext 就能获取
application.bot_data.setdefault("service", service)
register_handlers(application, service)

View File

@ -1,32 +1,31 @@
from telegram import Update
from telegram.constants import ChatAction
from telegram.error import BadRequest
from telegram.ext import CallbackContext
from config import config
from logger import Log
from plugins.base import BasePlugins, restricts
from service import BaseService
from plugins.base import restricts
from utils.base import PaimonContext
class Help(BasePlugins):
class Help:
"""
帮助
"""
def __init__(self, service: BaseService):
super().__init__(service)
def __init__(self):
self.help_png = None
self.file_id = None
@restricts()
async def command_start(self, update: Update, _: CallbackContext) -> None:
async def command_start(self, update: Update, context: PaimonContext) -> None:
message = update.message
user = update.effective_user
service = context.service
Log.info(f"用户 {user.full_name}[{user.id}] 发出help命令")
if self.file_id is None or config.DEBUG:
await message.reply_chat_action(ChatAction.TYPING)
help_png = await self.service.template.render('bot/help', "help.html", {}, {"width": 768, "height": 768})
help_png = await service.template.render('bot/help', "help.html", {}, {"width": 768, "height": 768})
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
reply_photo = await message.reply_photo(help_png, filename="help.png", allow_sending_without_reply=True)
photo = reply_photo.photo[0]

16
utils/base.py Normal file
View File

@ -0,0 +1,16 @@
from telegram.ext import CallbackContext, ExtBot
from service import BaseService
class PaimonContext(CallbackContext[ExtBot, dict, dict, dict]):
"""
PaimoeContext
"""
@property
def service(self) -> BaseService:
value = self.bot_data.get("service")
if value is None:
raise RuntimeError("没有与此上下文对象关联的实例化服务")
return value