From c9e52174e12d8fa62841a46750837f18e2a8caa5 Mon Sep 17 00:00:00 2001 From: NekoInverter Date: Thu, 3 Mar 2022 16:18:53 +0800 Subject: [PATCH] Apply fixes from @jh0ker --- actions.py | 6 +++++- bot.py | 13 +++++++------ internationalization.py | 19 ++++--------------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/actions.py b/actions.py index 9a03bd2..58a45a6 100644 --- a/actions.py +++ b/actions.py @@ -7,6 +7,7 @@ from datetime import datetime from telegram import Message, Chat from telegram.ext import CallbackContext +from apscheduler.jobstores.base import JobLookupError from config import TIME_REMOVAL_AFTER_SKIP, MIN_FAST_TURN_TIME from errors import DeckEmptyError, NotEnoughPlayersError @@ -192,7 +193,10 @@ def start_player_countdown(bot, game, job_queue): if game.mode == 'fast': if game.job: - game.job.schedule_removal() + try: + game.job.schedule_removal() + except JobLookupError: + pass job = job_queue.run_once( #lambda x,y: do_skip(bot, player), diff --git a/bot.py b/bot.py index b1ccf5d..b2825d5 100644 --- a/bot.py +++ b/bot.py @@ -49,6 +49,7 @@ logging.basicConfig( level=logging.INFO ) logger = logging.getLogger(__name__) +logging.getLogger('apscheduler').setLevel(logging.WARNING) @user_locale def notify_me(update: Update, context: CallbackContext): @@ -302,12 +303,12 @@ def select_game(update: Update, context: CallbackContext): def selected(): back = [[InlineKeyboardButton(text=_("Back to last group"), 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!"), show_alert=False, 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, text=_("Selected group: {group}\n" "Make sure that you switch to the correct " @@ -317,7 +318,7 @@ def select_game(update: Update, context: CallbackContext): parse_mode=ParseMode.HTML, timeout=TIMEOUT) - selected() + dispatcher.run_async(selected) @game_locales @@ -385,16 +386,16 @@ def start_game(update: Update, context: CallbackContext): def send_first(): """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)], timeout=TIMEOUT) - dispatcher.run_async(context.bot.sendMessage, chat.id, + context.bot.sendMessage(chat.id, text=first_message, reply_markup=InlineKeyboardMarkup(choice), timeout=TIMEOUT) - send_first() + dispatcher.run_async(send_first) start_player_countdown(context.bot, game, context.job_queue) elif len(context.args) and context.args[0] == 'select': diff --git a/internationalization.py b/internationalization.py index 072ee12..f56a20c 100644 --- a/internationalization.py +++ b/internationalization.py @@ -151,21 +151,10 @@ def game_locales(func): def _user_chat_from_update(update): + user = update.effective_user + chat = update.effective_chat - try: - user = update.message.from_user - 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 + if chat is None and user is not None and user.id in gm.userid_current: + chat = gm.userid_current.get(user.id).game.chat return user, chat