1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-24 23:04:18 +00:00

#17: Properly generate CancelSignals from regular keyboards

This commit is contained in:
Steffo 2020-03-27 16:57:32 +01:00
parent 0a0f76168b
commit 3e49f7c8a5
2 changed files with 17 additions and 5 deletions

View file

@ -76,8 +76,13 @@ def main():
reply_markup=telegram.ReplyKeyboardRemove())
# Skip the update
continue
# Forward the update to the worker
receiving_worker.queue.put(update)
# If the message contains the "Cancel" string defined in the strings file...
if update.message.text == strings.menu_cancel:
# Send a CancelSignal to the worker instead of the update
receiving_worker.queue.put(worker.CancelSignal())
else:
# Forward the update to the worker
receiving_worker.queue.put(update)
# If the update is a inline keyboard press...
if isinstance(update.callback_query, telegram.CallbackQuery):
# Forward the update to the corresponding worker

View file

@ -10,6 +10,7 @@ import database as db
import re
import utils
import os
import traceback
from html import escape
import requests
import importlib
@ -278,9 +279,9 @@ class ChatWorker(threading.Thread):
self.bot.send_message(self.chat.id, strings.conversation_admin_select_user, reply_markup=keyboard)
# Wait for a reply
reply = self.__wait_for_regex("user_([0-9]+)", cancellable=True)
# Allow the cancellation of the operation
if reply == strings.menu_cancel:
return CancelSignal()
# Propagate CancelSignals
if isinstance(reply, CancelSignal):
return reply
# Find the user in the database
user = self.session.query(db.User).filter_by(user_id=int(reply)).one_or_none()
# Ensure the user exists
@ -974,6 +975,9 @@ class ChatWorker(threading.Thread):
"""Edit manually the credit of an user."""
# Make the admin select an user
user = self.__user_select()
# Allow the cancellation of the operation
if isinstance(user, CancelSignal):
return
# Create an inline keyboard with a single cancel button
cancel = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton(strings.menu_cancel,
callback_data="cmd_cancel")]])
@ -1139,6 +1143,9 @@ class ChatWorker(threading.Thread):
"""Add an administrator to the bot."""
# Let the admin select an administrator to promote
user = self.__user_select()
# Allow the cancellation of the operation
if isinstance(user, CancelSignal):
return
# Check if the user is already an administrator
admin = self.session.query(db.Admin).filter_by(user_id=user.user_id).one_or_none()
if admin is None: