Apply fixes from @jh0ker

This commit is contained in:
NekoInverter 2022-03-03 16:18:53 +08:00
parent f11df72b0b
commit c9e52174e1
3 changed files with 16 additions and 22 deletions

View file

@ -7,6 +7,7 @@ from datetime import datetime
from telegram import Message, Chat from telegram import Message, Chat
from telegram.ext import CallbackContext from telegram.ext import CallbackContext
from apscheduler.jobstores.base import JobLookupError
from config import TIME_REMOVAL_AFTER_SKIP, MIN_FAST_TURN_TIME from config import TIME_REMOVAL_AFTER_SKIP, MIN_FAST_TURN_TIME
from errors import DeckEmptyError, NotEnoughPlayersError from errors import DeckEmptyError, NotEnoughPlayersError
@ -192,7 +193,10 @@ def start_player_countdown(bot, game, job_queue):
if game.mode == 'fast': if game.mode == 'fast':
if game.job: if game.job:
try:
game.job.schedule_removal() game.job.schedule_removal()
except JobLookupError:
pass
job = job_queue.run_once( job = job_queue.run_once(
#lambda x,y: do_skip(bot, player), #lambda x,y: do_skip(bot, player),

13
bot.py
View file

@ -49,6 +49,7 @@ logging.basicConfig(
level=logging.INFO level=logging.INFO
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logging.getLogger('apscheduler').setLevel(logging.WARNING)
@user_locale @user_locale
def notify_me(update: Update, context: CallbackContext): def notify_me(update: Update, context: CallbackContext):
@ -302,12 +303,12 @@ def select_game(update: Update, context: CallbackContext):
def selected(): def selected():
back = [[InlineKeyboardButton(text=_("Back to last group"), back = [[InlineKeyboardButton(text=_("Back to last group"),
switch_inline_query='')]] switch_inline_query='')]]
dispatcher.run_async(context.bot.answerCallbackQuery, update.callback_query.id, context.bot.answerCallbackQuery(update.callback_query.id,
text=_("Please switch to the group you selected!"), text=_("Please switch to the group you selected!"),
show_alert=False, show_alert=False,
timeout=TIMEOUT) timeout=TIMEOUT)
dispatcher.run_async(context.bot.editMessageText, chat_id=update.callback_query.message.chat_id, context.bot.editMessageText(chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id, message_id=update.callback_query.message.message_id,
text=_("Selected group: {group}\n" text=_("Selected group: {group}\n"
"<b>Make sure that you switch to the correct " "<b>Make sure that you switch to the correct "
@ -317,7 +318,7 @@ def select_game(update: Update, context: CallbackContext):
parse_mode=ParseMode.HTML, parse_mode=ParseMode.HTML,
timeout=TIMEOUT) timeout=TIMEOUT)
selected() dispatcher.run_async(selected)
@game_locales @game_locales
@ -385,16 +386,16 @@ def start_game(update: Update, context: CallbackContext):
def send_first(): def send_first():
"""Send the first card and player""" """Send the first card and player"""
dispatcher.run_async(context.bot.sendSticker, chat.id, context.bot.sendSticker(chat.id,
sticker=c.STICKERS[str(game.last_card)], sticker=c.STICKERS[str(game.last_card)],
timeout=TIMEOUT) timeout=TIMEOUT)
dispatcher.run_async(context.bot.sendMessage, chat.id, context.bot.sendMessage(chat.id,
text=first_message, text=first_message,
reply_markup=InlineKeyboardMarkup(choice), reply_markup=InlineKeyboardMarkup(choice),
timeout=TIMEOUT) timeout=TIMEOUT)
send_first() dispatcher.run_async(send_first)
start_player_countdown(context.bot, game, context.job_queue) start_player_countdown(context.bot, game, context.job_queue)
elif len(context.args) and context.args[0] == 'select': elif len(context.args) and context.args[0] == 'select':

View file

@ -151,21 +151,10 @@ def game_locales(func):
def _user_chat_from_update(update): def _user_chat_from_update(update):
user = update.effective_user
chat = update.effective_chat
try: if chat is None and user is not None and user.id in gm.userid_current:
user = update.message.from_user chat = gm.userid_current.get(user.id).game.chat
chat = update.message.chat
except (NameError, AttributeError):
try:
user = update.inline_query.from_user
chat = gm.userid_current[user.id].game.chat
except KeyError:
chat = None
except (NameError, AttributeError):
try:
user = update.chosen_inline_result.from_user
chat = gm.userid_current[user.id].game.chat
except (NameError, AttributeError, KeyError):
chat = None
return user, chat return user, chat