-
Notifications
You must be signed in to change notification settings - Fork 233
/
utils.py
126 lines (93 loc) · 3.58 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Telegram bot to play UNO in group chats
# Copyright (c) 2016 Jannes Höke <[email protected]>
#
# 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/>.
import logging
from telegram import Update
from telegram.ext import CallbackContext
from internationalization import _, __
from mwt import MWT
from shared_vars import gm, dispatcher
logger = logging.getLogger(__name__)
TIMEOUT = 2.5
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":
return _("{emoji} Red").format(emoji='❤️')
if color == "b":
return _("{emoji} Blue").format(emoji='💙')
if color == "g":
return _("{emoji} Green").format(emoji='💚')
if color == "y":
return _("{emoji} Yellow").format(emoji='💛')
def display_color_group(color, game):
""" Convert a color code to actual color name """
if color == "r":
return __("{emoji} Red", game.translate).format(
emoji='❤️')
if color == "b":
return __("{emoji} Blue", game.translate).format(
emoji='💙')
if color == "g":
return __("{emoji} Green", game.translate).format(
emoji='💚')
if color == "y":
return __("{emoji} Yellow", game.translate).format(
emoji='💛')
def error(update: Update, context: CallbackContext):
"""Simple error handler"""
logger.exception(context.error)
def send_async(bot, *args, **kwargs):
"""Send a message asynchronously"""
if 'timeout' not in kwargs:
kwargs['timeout'] = TIMEOUT
try:
dispatcher.run_async(bot.sendMessage, *args, **kwargs)
except Exception as e:
error(None, None, e)
def answer_async(bot, *args, **kwargs):
"""Answer an inline query asynchronously"""
if 'timeout' not in kwargs:
kwargs['timeout'] = TIMEOUT
try:
dispatcher.run_async(bot.answerInlineQuery, *args, **kwargs)
except Exception as e:
error(None, None, e)
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)
@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)]