-
Notifications
You must be signed in to change notification settings - Fork 1
/
CallbackHandler.py
318 lines (282 loc) · 14.2 KB
/
CallbackHandler.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import asyncio
import telepot
import time
from telepot.aio.routing import *
from telepot.namedtuple import *
from Messages import ALL_LANGS, setLang, edit_message, EN
from KeyboardQuery import *
from DatabaseStats import save_lang
import Globals
from Admin import check_admin, check_spam, get_maintenance, get_grp_info
# Function to group elements together by iterating through sequence
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
#Callback queries are split into 3 types:
#1. Configuration --> Changing settings (starting with @@)
#1. Menu --> Anything related to the navigation of the menu (starting with #)
#2. Game --> Game-related queries
class CallbackHandler(telepot.aio.helper.CallbackQueryOriginHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_callback_query(self, msg):
queryID, ID, queryData = telepot.glance(msg, flavor='callback_query')
try:
Messages = Globals.LANG[ID]
except KeyError:
Messages = EN #Set default to English
#First check maintenance status
if get_maintenance() and (ID != 28173774):
await edit_message(self.editor,Messages['maintenance']['shutdown'],reply_markup=None,parse_mode='HTML')
return
##################################
##### GAME-RELATED CALLBACKS #####
##################################
if queryData in ('~POWUP~','~POWDOWN~'):
try:
agent,game = Globals.DBP[ID]
Messages = game.Messages
if queryData == '~POWUP~':
if not game.powOn or (not agent.alive):
self.close()
return
await game.bot.answerCallbackQuery(queryID, text=Messages['powerUp']['yes'], show_alert=True)
game.powUp.add(agent)
await edit_message(game.messageEditor,
Messages['powerUp'][game.powerUp.name]['desc'].format(
game.powerUp.limit,len(game.powUp)),
reply_markup=generate_powerUp_keyboard(Messages))
self.close()
return
elif queryData == '~POWDOWN~':
if not game.powOn or (not agent.alive):
self.close()
return
await game.bot.answerCallbackQuery(queryID, text=Messages['powerUp']['no'], show_alert=True)
game.powUp.remove(agent)
await edit_message(game.messageEditor,
Messages['powerUp'][game.powerUp.name]['desc'].format(
game.powerUp.limit,len(game.powUp)),
reply_markup=generate_powerUp_keyboard(Messages))
self.close()
return
except KeyError:
self.close()
return
elif '{' in queryData:
try:
#Retreive stored hero and game object in userID from DB
agent,game = Globals.DBP[ID]
except KeyError: #Error retreiving objects. Maybe they dont exist
await edit_message(self.editor,Messages['error'],reply_markup=None)
#Time given in message is the time the message was sent, which is more or less the same for all
#so have to use own time
date = int(time.time())
if queryData == '{|ULTYES|}':
await edit_message(agent.editor,Messages['abilityUsed'],parse_mode='HTML',reply_markup=None)
queryData = queryData[:-1] + str(date) + '|}' #so structure becomes {|ULTYES|date|}
#If callback data is just an option to call another query
elif queryData == '{ULTNO}':
await edit_message(agent.editor,Messages['abilityNotUsed'],parse_mode='HTML',reply_markup=None)
#Person decides to stop using ult
elif queryData == '{NONE}':
await edit_message(agent.editor,Messages['acknowledgement'],parse_mode='HTML',reply_markup=None)
self.close()
return
elif queryData in ('{ATTACK}','{HEAL}'):
try:
await agent.editor.editMessageReplyMarkup(reply_markup=None)
except AttributeError:
pass
else: #callback data has the data structure: {|choice|targetAgentID|}
#Handle exception for multi-ult
agentID = queryData.split('|')
if len(agentID[2]) <= 2:
agentID = int(agentID[3])
else:
agentID = int(agentID[2])
await edit_message(agent.editor,Messages['choiceAccept']%(game.agents[agentID].agentName),
reply_markup=None,
parse_mode='HTML')
queryData = queryData[:-1] + str(date) + '|}' #so structure becomes {|choice|targetAgentID|date|}
await agent.process_query(game,queryData) #send to agent object for processing
self.close()
##############################
###### CHECK IF SPAMMED ######
##############################
if await check_spam(self,msg):
await edit_message(self.editor,Messages['spam'][2],reply_markup=None,parse_mode='HTML')
self.close()
return
#########################
##### MENU CALLBACK #####
#########################
##########
###BACK###
##########
#To return to previous menu page (Back button pressed)
if queryData == '#-1':
markup = generate_menu_keyboard(Messages,queryData)
await edit_message(self.editor,Messages['start'],reply_markup=markup,parse_mode='HTML')
self.close()
return
##########
###Info###
##########
elif queryData == '#a0':
markup = generate_info_keyboard(Messages,queryData)
await edit_message(self.editor,Messages['info'],reply_markup=markup,parse_mode='HTML')
self.close()
return
###########
###Rules###
###########
elif queryData == '#aa':
markup = generate_back_keyboard(Messages,queryData)
await self.bot.sendPhoto(ID,Messages['rules']['fileID'])
await edit_message(self.editor,Messages['rules']['sent'],reply_markup=markup)
self.close()
return
############
###Agents###
############
elif queryData == '#b0':
markup = generate_agent_keyboard(Messages,queryData)
await edit_message(self.editor,Messages['findOutChar'],reply_markup=markup,parse_mode='HTML')
self.close()
return
elif '#b' in queryData:
markup = generate_agent_keyboard(Messages,queryData)
message = generate_agent_info(Messages,queryData) #queryData = agentCode, function found in KeyboardQuery
await edit_message(self.editor,message,reply_markup=markup,parse_mode='HTML')
self.close()
return
###########
###Stats###
###########
elif queryData == '#c0':
markup = generate_stats_keyboard(Messages)
await edit_message(self.editor,Messages['stats']['query'],reply_markup=markup,parse_mode='HTML')
self.close()
return
elif '#c' in queryData:
markup = generate_back_keyboard(Messages,queryData)
if ID == 28173774:
#get local stats
message = Messages['stats']['local']
d = Globals.LOCALID[ID]
if d['normalGamesPlayed']:
p = (d['derpNormalWins']/d['normalGamesPlayed']*100,
d['drawsNormal']/d['normalGamesPlayed']*100,
d['pyroNormalWins']/d['normalGamesPlayed']*100,
d['normalGamesSurvived']/d['normalGamesPlayed']*100
)
else:
p = (0,0,0,0)
message = message.format(d=d,p=p)
#Get no. of groups and players playing
activeGrps, activePlayers = 0,0
for grp in Globals.DBG:
if Globals.DBG[grp]:
activeGrps += 1
for player in Globals.DBP:
if Globals.DBP[player]:
activePlayers += 1
message += Messages['stats']['des'].format(activeGrps,activePlayers)
#Finally, get global stats
d = Globals.GLOBAL_STATS
totalGames = d['derpWins'] + d['pyroWins'] + d['drawsNormal']
if totalGames == 0:
p = (0,0,0,0)
else:
p = (totalGames,d['derpWins']/totalGames*100,d['drawsNormal']/totalGames*100,d['pyroWins']/totalGames*100)
message += Messages['stats']['global'].format(d=d,p=p)
await edit_message(self.editor,message,reply_markup=markup,parse_mode='HTML')
self.close()
return
if queryData == '#ca': #Local stats
#get local stats
d = Globals.LOCALID[ID]
if d['normalGamesPlayed']:
p = (d['derpNormalWins']/d['normalGamesPlayed']*100,
d['drawsNormal']/d['normalGamesPlayed']*100,
d['pyroNormalWins']/d['normalGamesPlayed']*100,
d['normalGamesSurvived']/d['normalGamesPlayed']*100
)
else:
p = (0,0,0,0)
await edit_message(self.editor,Messages['stats']['local'].format(d=d,p=p),reply_markup=markup,parse_mode='HTML')
self.close()
return
else: #Global stats
d = Globals.GLOBAL_STATS
totalGames = d['derpWins'] + d['pyroWins'] + d['drawsNormal']
if totalGames == 0:
p = (0,0,0,0)
else:
p = (totalGames,d['derpWins']/totalGames*100,d['drawsNormal']/totalGames*100,d['pyroWins']/totalGames*100)
await edit_message(self.editor,Messages['stats']['global'].format(d=d,p=p),reply_markup=markup,parse_mode='HTML')
self.close()
return
############
###Config###
############
#Separate keyboards so that I know whether to save language for the person, or for the group
if queryData == '#d0' or '#d1' in queryData:
if queryData == '#d0':
markup = generate_lang_keyboard()
else:
queryData = queryData.split('|')[1]
markup = generate_lang_keyboard(grp=queryData)
await edit_message(self.editor,setLang,reply_markup=markup,parse_mode='HTML')
self.close()
return
#SET / CHANGE LANGUAGE for personal
elif '#da' in queryData: #Eg. #da0EN
await save_lang(ID,queryData[-2:])
Messages = Globals.LANG[ID]
markup = generate_menu_keyboard(Messages,-1)
await edit_message(self.editor,Messages['start'],reply_markup=markup,parse_mode='HTML')
self.close()
return
#SET / CHANGE LANGUAGE for group
elif '#db' in queryData: #Eg. db|-12345678|IN
queryData = queryData.split('|')
await save_lang(queryData[1],queryData[2])
Messages = Globals.LANG[ID]
markup = generate_config_keyboard(Messages,'#-1',queryData[1])
await edit_message(self.editor,Messages['config']['intro']%(Globals.GRPID[int(queryData[1])]['title']),reply_markup=markup,parse_mode='HTML')
self.close()
return
#Exit!
elif '#-2' in queryData:
await edit_message(self.editor,Messages['config']['done'],reply_markup=None,parse_mode='HTML')
self.close()
return
############
###Groups###
############
elif queryData == '#e0':
markup = generate_menu_keyboard(Messages,queryData)
message = Messages['groups']
grps = await get_grp_info(self.bot)
#Each item in grps is a tuple as follows: (grpID,{'memberCount':members,'title':GrpTitle,'link':inviteLink})
for item in grps:
result = item[1]
message += result['link'] + result['title'] + '</a>\n'
message += Globals.LANG[ID]['groupsMemberCount']%(result['members'])
await edit_message(self.editor,message,reply_markup = markup)
self.close()
return
####### #######
###Information that doesn't require processing (namely support, donate, rate and about buttons)###
####### #######
elif queryData in ('#f0','#g0','#h0','#i0'):
markup = generate_menu_keyboard(Messages,queryData)
key = get_menu_key(queryData)
await edit_message(self.editor,Messages[key],reply_markup=markup,parse_mode='HTML')
self.close()
return
markup = generate_back_keyboard(Messages,queryData)
await edit_message(self.editor,Messages['menu']['soon'],reply_markup=markup,parse_mode='HTML')
self.close()
return