PaiGram/main.py

123 lines
6.8 KiB
Python
Raw Normal View History

2022-04-14 07:18:45 +00:00
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ConversationHandler, \
CallbackQueryHandler, InlineQueryHandler, CallbackContext
from plugins.admin import Admin
from plugins.auth import Auth
2022-05-18 14:09:07 +00:00
from plugins.base import NewChatMembersHandler
2022-04-14 07:18:45 +00:00
from plugins.cookies import Cookies
from plugins.errorhandler import error_handler
from plugins.gacha import Gacha
from plugins.get_user import GetUser
from plugins.inline import Inline
from plugins.job_queue import JobQueue
from plugins.quiz import Quiz
from plugins.sign import Sign
2022-05-18 14:12:30 +00:00
from plugins.start import start, help_command, emergency_food, ping, reply_keyboard_remove
2022-04-14 07:18:45 +00:00
from plugins.weapon import Weapon
from service import StartService
from service.repository import AsyncRepository
from config import config
from service.cache import RedisCache
def main() -> None:
repository = AsyncRepository(mysql_host=config.MYSQL["host"],
mysql_user=config.MYSQL["user"],
mysql_password=config.MYSQL["password"],
mysql_port=config.MYSQL["port"],
mysql_database=config.MYSQL["database"]
)
cache = RedisCache(db=6)
service = StartService(repository, cache)
application = Application.builder().token(config.TELEGRAM["token"]).build()
2022-05-18 12:36:15 +00:00
application.add_handler(CommandHandler("start", start, block=False))
application.add_handler(CommandHandler("help", help_command, block=False))
application.add_handler(CommandHandler("ping", ping, block=False))
2022-04-14 07:18:45 +00:00
# application.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, new_chat_members))
auth = Auth(service)
2022-05-18 14:09:07 +00:00
new_chat_members_handler = NewChatMembersHandler(service,auth.new_mem)
application.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS,
new_chat_members_handler.new_member, block=False))
2022-05-18 12:36:15 +00:00
application.add_handler(CallbackQueryHandler(auth.query, pattern=r"^auth_challenge\|", block=False))
application.add_handler(CallbackQueryHandler(auth.admin, pattern=r"^auth_admin\|", block=False))
2022-04-14 07:18:45 +00:00
# application.add_handler(MessageHandler((filters.Regex(r'.派蒙是应急食品') & filters.ChatType.PRIVATE), emergency_food))
cookies = Cookies(service)
cookies_handler = ConversationHandler(
2022-05-18 12:36:15 +00:00
entry_points=[CommandHandler('adduser', cookies.command_start, filters.ChatType.PRIVATE, block=False),
MessageHandler(filters.Regex(r"^绑定账号(.*)") & filters.ChatType.PRIVATE,
cookies.command_start, block=False)],
2022-04-14 07:18:45 +00:00
states={
2022-05-18 12:36:15 +00:00
cookies.CHECK_SERVER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
cookies.check_server, block=False)],
cookies.CHECK_COOKIES: [MessageHandler(filters.TEXT & ~filters.COMMAND,
cookies.check_cookies, block=False)],
cookies.COMMAND_RESULT: [MessageHandler(filters.TEXT & ~filters.COMMAND,
cookies.command_result, block=False)],
2022-04-14 07:18:45 +00:00
},
2022-05-18 12:36:15 +00:00
fallbacks=[CommandHandler('cancel', cookies.cancel, block=False)],
2022-04-14 07:18:45 +00:00
)
get_user = GetUser(service)
get_user_handler = ConversationHandler(
2022-05-18 12:36:15 +00:00
entry_points=[CommandHandler('getuser', get_user.command_start, block=False),
MessageHandler(filters.Regex(r"^玩家查询(.*)"), get_user.command_start, block=False)],
2022-04-14 07:18:45 +00:00
states={
2022-05-18 12:36:15 +00:00
get_user.COMMAND_RESULT: [CallbackQueryHandler(get_user.command_result, block=False)]
2022-04-14 07:18:45 +00:00
},
2022-05-18 12:36:15 +00:00
fallbacks=[CommandHandler('cancel', get_user.cancel, block=False)],
2022-04-14 07:18:45 +00:00
)
sign = Sign(service)
sign_handler = ConversationHandler(
2022-05-18 12:36:15 +00:00
entry_points=[CommandHandler('sign', sign.command_start, block=False),
MessageHandler(filters.Regex(r"^每日签到(.*)"), sign.command_start, block=False)],
2022-04-14 07:18:45 +00:00
states={
2022-05-18 12:36:15 +00:00
sign.COMMAND_RESULT: [CallbackQueryHandler(sign.command_result, block=False)]
2022-04-14 07:18:45 +00:00
},
2022-05-18 12:36:15 +00:00
fallbacks=[CommandHandler('cancel', sign.cancel, block=False)],
2022-04-14 07:18:45 +00:00
)
application.add_handler(sign_handler)
quiz = Quiz(service)
quiz_handler = ConversationHandler(
2022-05-18 12:36:15 +00:00
entry_points=[CommandHandler('quiz', quiz.command_start, block=False)],
2022-04-14 07:18:45 +00:00
states={
2022-05-18 12:36:15 +00:00
quiz.CHECK_COMMAND: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.check_command, block=False)],
quiz.CHECK_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.check_question, block=False)],
quiz.GET_NEW_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.get_new_question, block=False)],
quiz.GET_NEW_CORRECT_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.get_new_correct_answer, block=False)],
quiz.GET_NEW_WRONG_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.get_new_wrong_answer, block=False),
2022-04-14 07:18:45 +00:00
CommandHandler("finish", quiz.finish_edit)],
2022-05-18 12:36:15 +00:00
quiz.SAVE_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
quiz.save_question, block=False)],
2022-04-14 07:18:45 +00:00
},
2022-05-18 12:36:15 +00:00
fallbacks=[CommandHandler('cancel', quiz.cancel, block=False)]
2022-04-14 07:18:45 +00:00
)
gacha = Gacha(service)
2022-05-18 12:36:15 +00:00
application.add_handler(CommandHandler("gacha", gacha.command_start, block=False))
2022-04-14 07:18:45 +00:00
admin = Admin(service)
2022-05-18 12:36:15 +00:00
application.add_handler(CommandHandler("add_admin", admin.add_admin, block=False))
application.add_handler(CommandHandler("del_admin", admin.del_admin, block=False))
2022-04-14 07:18:45 +00:00
weapon = Weapon(service)
2022-05-18 12:36:15 +00:00
application.add_handler(CommandHandler("weapon", weapon.command_start, block=False))
application.add_handler(CommandHandler("reply_keyboard_remove", reply_keyboard_remove, block=False))
application.add_handler(MessageHandler(filters.Regex(r"^武器查询(.*)"), weapon.command_start, block=False))
2022-04-14 07:18:45 +00:00
application.add_handler(quiz_handler)
application.add_handler(cookies_handler)
application.add_handler(get_user_handler)
inline = Inline(service)
2022-05-18 12:36:15 +00:00
application.add_handler(InlineQueryHandler(inline.inline_query, block=False))
2022-04-14 07:18:45 +00:00
job_queue = JobQueue(service)
application.job_queue.run_once(job_queue.start_job, when=3, name="start_job")
# application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command))
2022-05-18 12:36:15 +00:00
application.add_error_handler(error_handler, block=False)
2022-04-14 07:18:45 +00:00
application.run_polling()
if __name__ == '__main__':
main()