-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
160 lines (118 loc) · 4.7 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/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.ext import InlineQueryHandler, ChosenInlineResultHandler, \
CommandHandler, MessageHandler, Filters, CallbackQueryHandler, 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
#Card Played
def display_color(color):
""" Convert a color code to actual color name """
if color == "r":
return _("{emoji} Красный").format(emoji='❤️')
if color == "b":
return _("{emoji} Синий").format(emoji='💙')
if color == "g":
return _("{emoji} Зелёный").format(emoji='💚')
if color == "y":
return _("{emoji} Жёлтый").format(emoji='💛')
def display_color_group(color, game):
""" Convert a color code to actual color name """
if color == "r":
return __("{emoji} Красный", game.translate).format(
emoji='❤️')
if color == "b":
return __("{emoji} Синий", game.translate).format(
emoji='💙')
if color == "g":
return __("{emoji} Зелёный", game.translate).format(
emoji='💚')
if color == "y":
return __("{emoji} Жёлтый", game.translate).format(
emoji='💛')
def display_color_dark(color):
""" Convert a color code to actual color name """
if color == "w":
return _("{emoji} Белый").format(emoji='🤍')
if color == "p":
return _("{emoji} Фиолетовый").format(emoji='💜')
if color == "g":
return _("{emoji} Зелёный").format(emoji='💚')
if color == "o":
return _("{emoji} Оранжевый").format(emoji='🧡')
def display_color_group_dark(color, game):
""" Convert a color code to actual color name """
if color == "w":
return __("{emoji} Белый", game.translate).format(
emoji='🤍')
if color == "p":
return __("{emoji} Фиолетовый", game.translate).format(
emoji='💜')
if color == "g":
return __("{emoji} Зелёный", game.translate).format(
emoji='💚')
if color == "o":
return __("{emoji} Оранжевый", game.translate).format(
emoji='🧡')
def error(bot, update, error):
"""Simple error handler"""
logger.exception(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)]