2016-05-08 20:37:25 +08:00
|
|
|
#!/usr/bin/env python3
|
2016-05-20 05:15:46 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-05-08 20:37:25 +08:00
|
|
|
#
|
|
|
|
# Telegram bot to play UNO in group chats
|
|
|
|
# Copyright (c) 2016 Jannes Höke <uno@jhoeke.de>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-05-22 09:13:05 +08:00
|
|
|
import logging
|
2016-05-22 03:41:38 +08:00
|
|
|
|
2016-05-22 20:45:51 +08:00
|
|
|
from telegram.ext.dispatcher import run_async
|
2016-05-22 09:13:05 +08:00
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
from internationalization import _, __
|
2017-08-19 06:08:55 +08:00
|
|
|
from mwt import MWT
|
2017-11-28 00:59:19 +08:00
|
|
|
from shared_vars import gm
|
2016-05-22 09:13:05 +08:00
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-05-22 09:13:05 +08:00
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
TIMEOUT = 2.5
|
2016-04-29 23:02:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
def list_subtract(list1, list2):
|
|
|
|
""" Helper function to subtract two lists and return the sorted result """
|
|
|
|
list1 = list1.copy()
|
|
|
|
|
|
|
|
for x in list2:
|
|
|
|
list1.remove(x)
|
|
|
|
|
|
|
|
return list(sorted(list1))
|
|
|
|
|
|
|
|
|
|
|
|
def display_name(user):
|
|
|
|
""" Get the current players name including their username, if possible """
|
|
|
|
user_name = user.first_name
|
|
|
|
if user.username:
|
|
|
|
user_name += ' (@' + user.username + ')'
|
|
|
|
return user_name
|
|
|
|
|
|
|
|
|
|
|
|
def display_color(color):
|
|
|
|
""" Convert a color code to actual color name """
|
|
|
|
if color == "r":
|
2017-08-19 05:36:30 +08:00
|
|
|
return _("{emoji} Red").format(emoji='❤️')
|
2016-04-29 23:02:50 +08:00
|
|
|
if color == "b":
|
2017-08-19 05:36:30 +08:00
|
|
|
return _("{emoji} Blue").format(emoji='💙')
|
2016-04-29 23:02:50 +08:00
|
|
|
if color == "g":
|
2017-08-19 05:36:30 +08:00
|
|
|
return _("{emoji} Green").format(emoji='💚')
|
2016-04-29 23:02:50 +08:00
|
|
|
if color == "y":
|
2017-08-19 05:36:30 +08:00
|
|
|
return _("{emoji} Yellow").format(emoji='💛')
|
2016-05-23 01:21:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def display_color_group(color, game):
|
|
|
|
""" Convert a color code to actual color name """
|
|
|
|
if color == "r":
|
|
|
|
return __("{emoji} Red", game.translate).format(
|
2017-08-19 05:36:30 +08:00
|
|
|
emoji='❤️')
|
2016-05-23 01:21:51 +08:00
|
|
|
if color == "b":
|
|
|
|
return __("{emoji} Blue", game.translate).format(
|
2017-08-19 05:36:30 +08:00
|
|
|
emoji='💙')
|
2016-05-23 01:21:51 +08:00
|
|
|
if color == "g":
|
|
|
|
return __("{emoji} Green", game.translate).format(
|
2017-08-19 05:36:30 +08:00
|
|
|
emoji='💚')
|
2016-05-23 01:21:51 +08:00
|
|
|
if color == "y":
|
|
|
|
return __("{emoji} Yellow", game.translate).format(
|
2017-08-19 05:36:30 +08:00
|
|
|
emoji='💛')
|
2016-05-22 20:45:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def error(bot, update, error):
|
|
|
|
"""Simple error handler"""
|
|
|
|
logger.exception(error)
|
|
|
|
|
|
|
|
|
|
|
|
@run_async
|
|
|
|
def send_async(bot, *args, **kwargs):
|
|
|
|
"""Send a message asynchronously"""
|
|
|
|
if 'timeout' not in kwargs:
|
|
|
|
kwargs['timeout'] = TIMEOUT
|
|
|
|
|
|
|
|
try:
|
|
|
|
bot.sendMessage(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
error(None, None, e)
|
|
|
|
|
|
|
|
|
|
|
|
@run_async
|
|
|
|
def answer_async(bot, *args, **kwargs):
|
|
|
|
"""Answer an inline query asynchronously"""
|
|
|
|
if 'timeout' not in kwargs:
|
|
|
|
kwargs['timeout'] = TIMEOUT
|
|
|
|
|
|
|
|
try:
|
|
|
|
bot.answerInlineQuery(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
error(None, None, e)
|
2017-08-19 06:08:55 +08:00
|
|
|
|
|
|
|
|
2017-11-28 00:59:19 +08:00
|
|
|
def game_is_running(game):
|
|
|
|
return game in gm.chatid_games.get(game.chat.id, list())
|
|
|
|
|
|
|
|
|
|
|
|
def user_is_creator(user, game):
|
|
|
|
return user.id in game.owner
|
|
|
|
|
|
|
|
|
|
|
|
def user_is_admin(user, bot, chat):
|
|
|
|
return user.id in get_admin_ids(bot, chat.id)
|
|
|
|
|
|
|
|
|
|
|
|
def user_is_creator_or_admin(user, game, bot, chat):
|
|
|
|
return user_is_creator(user, game) or user_is_admin(user, bot, chat)
|
|
|
|
|
|
|
|
|
2017-08-19 06:08:55 +08:00
|
|
|
@MWT(timeout=60*60)
|
|
|
|
def get_admin_ids(bot, chat_id):
|
|
|
|
"""Returns a list of admin IDs for a given chat. Results are cached for 1 hour."""
|
|
|
|
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
|