1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-25 07:14: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,6 +76,11 @@ def main():
reply_markup=telegram.ReplyKeyboardRemove()) reply_markup=telegram.ReplyKeyboardRemove())
# Skip the update # Skip the update
continue continue
# 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 # Forward the update to the worker
receiving_worker.queue.put(update) receiving_worker.queue.put(update)
# If the update is a inline keyboard press... # If the update is a inline keyboard press...

View file

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