From 2a637da75f047b253f76fbb3429f68d63b878aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 19 Apr 2016 02:22:42 +0200 Subject: [PATCH] forcefully remove player if he leaves the group --- bot.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index b466467..8680883 100644 --- a/bot.py +++ b/bot.py @@ -5,7 +5,7 @@ from random import randint from telegram import InlineQueryResultArticle, ParseMode, Message, Chat, \ Emoji, InputTextMessageContent from telegram.ext import Updater, InlineQueryHandler, \ - ChosenInlineResultHandler, CommandHandler + ChosenInlineResultHandler, CommandHandler, MessageHandler, filters from telegram.utils.botan import Botan from game_manager import GameManager @@ -134,6 +134,33 @@ def leave_game(bot, update): bot.sendMessage(chat_id, text="Okay") +def status_update(bot, update): + """ Remove player from game if user leaves the group """ + + if update.message.left_chat_member: + try: + chat_id = update.message.chat_id + game = gm.chatid_game[chat_id] + user = update.message.left_chat_member + except KeyError: + return + + user_ids = list() + current_player = game.current_player + user_ids.append(current_player.user.id) + + itplayer = current_player.next + + while itplayer is not current_player: + user_ids.append(itplayer.user.id) + itplayer = itplayer.next + + if user.id in user_ids: + gm.leave_game(user) + bot.sendMessage(chat_id, text="Removing %s from the game" + % display_name(user)) + + def start_game(bot, update): """ Handler for the /start command """ @@ -439,6 +466,7 @@ dp.addHandler(CommandHandler('join', join_game)) dp.addHandler(CommandHandler('leave', leave_game)) dp.addHandler(CommandHandler('help', help)) dp.addHandler(CommandHandler('news', news)) +dp.addHandler(MessageHandler([filters.STATUS_UPDATE], status_update)) dp.addErrorHandler(error) start_bot(u)