2022-05-26 15:47:22 +00:00
|
|
|
|
import datetime
|
2022-06-09 07:12:06 +00:00
|
|
|
|
import time
|
2022-05-24 11:41:04 +00:00
|
|
|
|
|
|
|
|
|
import genshin
|
|
|
|
|
from genshin import Game, GenshinException, AlreadyClaimed
|
2022-06-03 07:58:51 +00:00
|
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
2022-05-30 08:08:56 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
2022-06-09 05:26:08 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, ConversationHandler, filters, \
|
|
|
|
|
CallbackQueryHandler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
2022-05-18 14:27:33 +00:00
|
|
|
|
from logger import Log
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from model.base import ServiceEnum
|
2022-06-22 05:42:34 +00:00
|
|
|
|
from plugins.base import BasePlugins, RestrictsCalls
|
2022-06-03 07:54:56 +00:00
|
|
|
|
from plugins.errorhandler import conversation_error_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from service import BaseService
|
|
|
|
|
from service.base import UserInfoData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SignCommandData:
|
|
|
|
|
user_info: UserInfoData = UserInfoData()
|
|
|
|
|
chat_id: int = 0
|
|
|
|
|
reply_to_message_id: int = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Sign(BasePlugins):
|
2022-06-09 12:52:59 +00:00
|
|
|
|
"""
|
|
|
|
|
每日签到
|
|
|
|
|
"""
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
CHECK_SERVER, COMMAND_RESULT = range(10400, 10402)
|
|
|
|
|
|
2022-06-09 05:26:08 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def create_conversation_handler(service: BaseService):
|
|
|
|
|
sign = Sign(service)
|
|
|
|
|
sign_handler = ConversationHandler(
|
|
|
|
|
entry_points=[CommandHandler('sign', sign.command_start, block=True),
|
2022-06-09 07:12:06 +00:00
|
|
|
|
MessageHandler(filters.Regex(r"^每日签到(.*)"), sign.command_start, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
states={
|
|
|
|
|
sign.COMMAND_RESULT: [CallbackQueryHandler(sign.command_result, block=True)]
|
|
|
|
|
},
|
|
|
|
|
fallbacks=[CommandHandler('cancel', sign.cancel, block=True)]
|
|
|
|
|
)
|
|
|
|
|
return sign_handler
|
|
|
|
|
|
2022-05-24 11:41:04 +00:00
|
|
|
|
@staticmethod
|
2022-05-25 10:35:13 +00:00
|
|
|
|
async def _start_sign(user_info: UserInfoData, service: ServiceEnum) -> str:
|
2022-06-19 11:37:31 +00:00
|
|
|
|
if service == ServiceEnum.HYPERION:
|
2022-05-24 11:41:04 +00:00
|
|
|
|
client = genshin.ChineseClient(cookies=user_info.mihoyo_cookie)
|
|
|
|
|
uid = user_info.mihoyo_game_uid
|
|
|
|
|
else:
|
2022-05-25 10:35:13 +00:00
|
|
|
|
client = genshin.GenshinClient(cookies=user_info.hoyoverse_cookie, lang="zh-cn")
|
2022-05-24 11:41:04 +00:00
|
|
|
|
uid = user_info.hoyoverse_game_uid
|
|
|
|
|
try:
|
|
|
|
|
rewards = await client.get_monthly_rewards(game=Game.GENSHIN, lang="zh-cn")
|
|
|
|
|
except GenshinException as error:
|
|
|
|
|
Log.error(f"UID {uid} 获取签到信息失败,API返回信息为 {str(error)}")
|
|
|
|
|
return f"获取签到信息失败,API返回信息为 {str(error)}"
|
|
|
|
|
try:
|
|
|
|
|
daily_reward_info = await client.get_reward_info(game=Game.GENSHIN, lang="zh-cn") # 获取签到信息失败
|
|
|
|
|
except GenshinException as error:
|
|
|
|
|
Log.error(f"UID {uid} 获取签到状态失败,API返回信息为 {str(error)}")
|
|
|
|
|
return f"获取签到状态失败,API返回信息为 {str(error)}"
|
|
|
|
|
if not daily_reward_info.signed_in:
|
|
|
|
|
try:
|
|
|
|
|
request_daily_reward = await client.request_daily_reward("sign", method="POST",
|
|
|
|
|
game=Game.GENSHIN, lang="zh-cn")
|
|
|
|
|
Log.info(f"UID {uid} 签到请求 {request_daily_reward}")
|
|
|
|
|
except AlreadyClaimed:
|
|
|
|
|
result = "今天旅行者已经签到过了~"
|
|
|
|
|
except GenshinException as error:
|
|
|
|
|
Log.error(f"UID {uid} 签到失败,API返回信息为 {str(error)}")
|
|
|
|
|
return f"获取签到状态失败,API返回信息为 {str(error)}"
|
|
|
|
|
else:
|
|
|
|
|
result = "OK"
|
|
|
|
|
else:
|
|
|
|
|
result = "今天旅行者已经签到过了~"
|
|
|
|
|
Log.info(f"UID {uid} 签到结果 {result}")
|
2022-06-08 08:33:28 +00:00
|
|
|
|
reward = rewards[daily_reward_info.claimed_rewards - (1 if daily_reward_info.signed_in else 0)]
|
2022-05-24 11:41:04 +00:00
|
|
|
|
today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
2022-05-26 15:47:22 +00:00
|
|
|
|
cn_timezone = datetime.timezone(datetime.timedelta(hours=8))
|
|
|
|
|
now = datetime.datetime.now(cn_timezone)
|
|
|
|
|
missed_days = now.day - daily_reward_info.claimed_rewards
|
2022-05-31 14:44:47 +00:00
|
|
|
|
if not daily_reward_info.signed_in:
|
|
|
|
|
missed_days -= 1
|
2022-05-25 10:40:07 +00:00
|
|
|
|
message = f"#### {today} (UTC+8) ####\n" \
|
2022-05-24 11:41:04 +00:00
|
|
|
|
f"UID: {uid}\n" \
|
|
|
|
|
f"今日奖励: {reward.name} × {reward.amount}\n" \
|
2022-05-26 15:47:22 +00:00
|
|
|
|
f"本月漏签次数:{missed_days}\n" \
|
2022-05-24 11:41:04 +00:00
|
|
|
|
f"签到结果: {result}"
|
|
|
|
|
return message
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
@conversation_error_handler
|
2022-06-22 05:42:34 +00:00
|
|
|
|
@RestrictsCalls(filters_chat=filters.ALL, return_data=ConversationHandler.END, try_delete_message=True)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.message
|
2022-05-18 14:27:33 +00:00
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 每日签到命令请求")
|
2022-05-02 09:06:10 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
sign_command_data: SignCommandData = context.chat_data.get("sign_command_data")
|
|
|
|
|
if sign_command_data is None:
|
|
|
|
|
sign_command_data = SignCommandData()
|
|
|
|
|
context.chat_data["sign_command_data"] = sign_command_data
|
|
|
|
|
user_info = await self.service.user_service_db.get_user_info(user.id)
|
2022-04-21 11:28:17 +00:00
|
|
|
|
if user_info.user_id == 0:
|
2022-06-08 17:55:14 +00:00
|
|
|
|
await update.message.reply_text("未查询到账号信息,请先私聊派蒙绑定账号")
|
2022-04-21 11:28:17 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
if user_info.service == ServiceEnum.NULL:
|
|
|
|
|
keyboard = [
|
|
|
|
|
[
|
2022-05-24 12:39:14 +00:00
|
|
|
|
InlineKeyboardButton("米游社", callback_data="sign|米游社"),
|
|
|
|
|
InlineKeyboardButton("HoYoLab", callback_data="sign|HoYoLab")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
sign_command_data.user_info = user_info
|
2022-05-29 16:29:24 +00:00
|
|
|
|
await update.message.reply_text("请选择你要签到的服务器", reply_markup=InlineKeyboardMarkup(keyboard))
|
2022-04-14 07:18:45 +00:00
|
|
|
|
sign_command_data.chat_id = update.message.chat_id
|
|
|
|
|
sign_command_data.reply_to_message_id = update.message.message_id
|
|
|
|
|
return self.COMMAND_RESULT
|
|
|
|
|
else:
|
2022-05-30 08:08:56 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-06-03 07:54:56 +00:00
|
|
|
|
sign = await self._start_sign(user_info, user_info.service)
|
2022-06-10 09:16:01 +00:00
|
|
|
|
reply_message = await message.reply_text(sign, allow_sending_without_reply=True)
|
2022-05-02 08:51:41 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2022-04-14 07:18:45 +00:00
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
@conversation_error_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def command_result(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
sign_command_data: SignCommandData = context.chat_data["sign_command_data"]
|
|
|
|
|
user_info = sign_command_data.user_info
|
|
|
|
|
query = update.callback_query
|
|
|
|
|
await query.answer()
|
|
|
|
|
message = "签到失败"
|
2022-05-30 08:08:56 +00:00
|
|
|
|
await query.message.reply_chat_action(ChatAction.TYPING)
|
2022-06-03 07:54:56 +00:00
|
|
|
|
if query.data == "sign|米游社":
|
2022-06-19 11:37:31 +00:00
|
|
|
|
message = await self._start_sign(user_info, ServiceEnum.HYPERION)
|
2022-06-03 07:54:56 +00:00
|
|
|
|
if query.data == "sign|HoYoLab":
|
|
|
|
|
message = await self._start_sign(user_info, ServiceEnum.HOYOLAB)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await query.edit_message_text(message)
|
2022-05-02 09:06:10 +00:00
|
|
|
|
if query_message := query.message:
|
|
|
|
|
if filters.ChatType.GROUPS.filter(query_message):
|
|
|
|
|
self._add_delete_message_job(context, query_message.chat_id, query_message.message_id)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return ConversationHandler.END
|