mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2024-11-16 12:51:35 +00:00
自定义 context
类型并在 help
命令实现相应代码
This commit is contained in:
parent
b07e3e7602
commit
e63e5d8995
@ -43,7 +43,7 @@ def register_handlers(application: Application, service: BaseService):
|
|||||||
application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block))
|
application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block))
|
||||||
|
|
||||||
# 初始化
|
# 初始化
|
||||||
plugins_help = Help(service)
|
plugins_help = Help()
|
||||||
inline = Inline(service)
|
inline = Inline(service)
|
||||||
auth = Auth(service)
|
auth = Auth(service)
|
||||||
gacha = Gacha(service)
|
gacha = Gacha(service)
|
||||||
|
14
main.py
14
main.py
@ -1,9 +1,10 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from warnings import filterwarnings
|
from warnings import filterwarnings
|
||||||
|
|
||||||
from telegram.ext import Application
|
from telegram.ext import Application, ContextTypes
|
||||||
from telegram.warnings import PTBUserWarning
|
from telegram.warnings import PTBUserWarning
|
||||||
|
|
||||||
|
from utils.base import PaimonContext
|
||||||
from config import config
|
from config import config
|
||||||
from handler import register_handlers
|
from handler import register_handlers
|
||||||
from logger import Log
|
from logger import Log
|
||||||
@ -39,7 +40,16 @@ def main() -> None:
|
|||||||
|
|
||||||
# 构建BOT
|
# 构建BOT
|
||||||
Log.info("构建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)
|
register_handlers(application, service)
|
||||||
|
|
||||||
|
@ -1,32 +1,31 @@
|
|||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.constants import ChatAction
|
from telegram.constants import ChatAction
|
||||||
from telegram.error import BadRequest
|
from telegram.error import BadRequest
|
||||||
from telegram.ext import CallbackContext
|
|
||||||
|
|
||||||
from config import config
|
from config import config
|
||||||
from logger import Log
|
from logger import Log
|
||||||
from plugins.base import BasePlugins, restricts
|
from plugins.base import restricts
|
||||||
from service import BaseService
|
from utils.base import PaimonContext
|
||||||
|
|
||||||
|
|
||||||
class Help(BasePlugins):
|
class Help:
|
||||||
"""
|
"""
|
||||||
帮助
|
帮助
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, service: BaseService):
|
def __init__(self):
|
||||||
super().__init__(service)
|
|
||||||
self.help_png = None
|
self.help_png = None
|
||||||
self.file_id = None
|
self.file_id = None
|
||||||
|
|
||||||
@restricts()
|
@restricts()
|
||||||
async def command_start(self, update: Update, _: CallbackContext) -> None:
|
async def command_start(self, update: Update, context: PaimonContext) -> None:
|
||||||
message = update.message
|
message = update.message
|
||||||
user = update.effective_user
|
user = update.effective_user
|
||||||
|
service = context.service
|
||||||
Log.info(f"用户 {user.full_name}[{user.id}] 发出help命令")
|
Log.info(f"用户 {user.full_name}[{user.id}] 发出help命令")
|
||||||
if self.file_id is None or config.DEBUG:
|
if self.file_id is None or config.DEBUG:
|
||||||
await message.reply_chat_action(ChatAction.TYPING)
|
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)
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
||||||
reply_photo = await message.reply_photo(help_png, filename="help.png", allow_sending_without_reply=True)
|
reply_photo = await message.reply_photo(help_png, filename="help.png", allow_sending_without_reply=True)
|
||||||
photo = reply_photo.photo[0]
|
photo = reply_photo.photo[0]
|
||||||
|
16
utils/base.py
Normal file
16
utils/base.py
Normal 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
|
Loading…
Reference in New Issue
Block a user