-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
422 lines (371 loc) · 12 KB
/
handlers.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import functools
import logging
from flask import current_app
from pony.orm import db_session
import views
from roller import DiceRoller
from models import User, Roll
from common.unicode import emoji
# getting data from flask.app_context
app = current_app
with app.app_context():
bot = app.config['TELEBOT']
botlogger = app.config['TELEBOT_LOGGER']
class BotHandlers:
"""
Message handlers.
For use only within app.app_context.
Every single handler function should be decorated
with custom @handler decorator.
"""
handlers = []
@classmethod
def register(cls):
"""
Register message handlers defined below into a TeleBot.
"""
try:
for handler in BotHandlers.handlers:
name = handler[0]
kwargs = handler[1]
bot.register_message_handler(name, **kwargs)
return True
except Exception as exc:
logging.error('Cannot register handlers: %s', exc)
return False
def handler(append_to=handlers, **out_kwargs):
"""
Decorator to register telebot handlers
"""
def decorator_register(func):
if out_kwargs:
append_to.append((func, out_kwargs))
@functools.wraps(func)
def wrapper_register(*args, **kwargs):
return func(*args, **kwargs)
return wrapper_register
return decorator_register
def with_info(f):
"""
Decorator to get user and char database objects as
child function attributes:
@handler(...)
@with_info
def some_function(message, user, char):
...
"""
@functools.wraps(f)
def wrapper_with_info(*args, **kwargs):
message = args[0]
user_id = message.from_user.id
user = User.get_user_by_id(user_id)
char = user.active_char()
new_args = args + (user, char,)
f(*new_args, **kwargs)
return wrapper_with_info
############################################################
# #
# BOT MESSAGE HANDLERS #
# #
############################################################
#
# Service handlers
#
@handler(append_to=handlers, commands=['start', 'help'])
def answer_start(message):
"""
Bot sends general help page and basic bot info
"""
send(message, views.hello(message.from_user.username))
@handler(append_to=handlers, commands=['stats'])
def send_stats(message):
"""
Bot sends statistics
"""
send(message, views.statistics(Roll.get_stats()))
@handler(append_to=handlers, commands=['info'])
def send_info(message):
"""
Bot sends info
"""
send(message, views.info())
#
# Managing user settings handlers
#
@handler(append_to=handlers, commands=['char', 'chars'])
@db_session
@with_info
def show_user_chars_list(message, user, char):
"""
Show charlist for user
"""
reply(message, views.charlist(user))
@handler(append_to=handlers, commands=['createchar'])
@db_session
@with_info
def create_char(message, user, char):
"""
Creating a char
"""
charname = message.text[12:].strip()
if not charname:
send(message, views.command_help('/createchar'))
return
try:
newchar = user.create_char(charname)
except Exception as exc:
reply(message, views.error(exc))
else:
answer = (
f'{emoji["horn"]} '
f'New char {emoji["elf"]} {newchar.name} created!\n'
'Your /chars have been updated.'
)
reply(message, answer)
@handler(append_to=handlers, commands=['deletechar'])
@db_session
@with_info
def delete_char(message, user, char):
"""
Delete char
"""
charname = message.text[12:].strip()
if not charname or len(charname.split()) > 1:
# charname should be a single word
reply(message, views.command_help('deletechar'))
return
try:
user.delete_char(name=charname)
except Exception as exc:
reply(message, views.error(exc))
else:
answer = (
f'{emoji["trashbin"]} '
f'Char {emoji["elf"]} {charname} deleted.'
)
reply(message, answer)
@handler(append_to=handlers, commands=['activechar'])
@db_session
@with_info
def set_active(message, user, char):
"""
Change active char
"""
charname = message.text[12:].strip()
if not charname:
reply(message, views.command_help('activechar'))
return
try:
user.set_active_char(charname)
except Exception as exc:
reply(message, views.error(exc))
else:
reply(message, views.charlist(user))
@handler(append_to=handlers, commands=['createroll'])
@db_session
@with_info
def create_throw(message, user, char):
"""
Create custom throw
"""
if not message.text[12:].strip():
reply(message, views.command_help('createroll'))
return
query = message.text[12:].split()
try:
throw_name, formula = query[0], ' '.join((query[1:]))
except IndexError:
error_text = 'Incorrect command syntax'
reply(message, views.command_help('createroll', error_text))
return
if not char:
reply(message, views.error('You have not a char yet.'))
return
try:
char.create_throw(throw_name, formula)
except Exception as exc:
reply(message, views.error(exc))
else:
reply(message, views.charlist(user))
@handler(append_to=handlers, commands=['deleteroll', 'deletethrow'])
@db_session
@with_info
def delete_throw(message, user, char):
"""
Delete custom throw
"""
throw_name = message.text[12:]
if not throw_name:
reply(message, views.command_help('deleteroll'))
return
if not char:
reply(message, views.error('You have no chars. Use /createchar'))
return
try:
char.delete_throw(throw_name)
except Exception as exc:
reply(message, views.error(exc))
else:
reply(message, views.charlist(user))
@handler(append_to=handlers, commands=['addmod'])
@db_session
@with_info
def create_attribute(message, user, char):
query: list = message.text[8:].strip().split()
if len(query) < 3 or len(query) > 4:
if len(query): # empty query
error_text = 'Incorrect command syntax.'
else:
error_text = ''
reply(message, views.command_help('addmod', error_text=error_text))
return
# getting user vars
if len(query) == 3:
name, alias, value = query
mod = None
elif len(query) == 4:
name, alias, value, mod = query
if not char:
reply(message, views.error('You have no chars. Use /createchar'))
return
try:
char.create_attribute(name, alias, value, mod)
except Exception as exc:
reply(message, views.error(exc))
else:
reply(message, views.charlist(user))
@handler(append_to=handlers, commands=['deletemod'])
@db_session
@with_info
def delete_attribute(message, user, char):
name = message.text[11:].strip()
if not name:
reply(message, views.command_help('deletemod'))
return
if not char:
reply(message, views.error('You have not a char yet. /createchar'))
return
try:
char.delete_attribute(name)
except Exception as exc:
reply(message, views.error(exc))
else:
reply(message, views.charlist(user))
#
# Rolls handlers
#
@handler(append_to=handlers, commands=['roll'])
@db_session
def roll_anything(message):
"""
Answering to the /roll command
"""
telegram_user = message.from_user
raw_formula = message.text[6:] # removeprefix /roll
if not raw_formula:
send(message, views.command_help('/roll'))
return
roller = DiceRoller(raw_formula, telegram_user)
hand = roller.hand
Roll.register()
reply(message, views.roll(roller, hand))
@handler(append_to=handlers, commands=['rollme'])
@db_session
@with_info
def roll_custom_throw(message, user, char):
"""
Rolling pre-defined throws by name
"""
if not char:
reply(message, views.error('You have not a char yet. /createchar'))
return
query = message.text[7:].strip().split()
if not len(query):
reply(message, views.command_help('rollme'))
throwname = query[0] # first arg would be the throwname
formula = char.throw(throwname) # naked formula
if len(query) > 1:
# got some addition to the Throw, like "/rollme throwname + 2"
addition = ' '.join(query[1:])
addition = ' ' + addition # adding space before
else:
# no addition provided
addition = ''
if formula:
roller = DiceRoller(formula + addition, message.from_user)
hand = roller.hand
Roll.register()
reply(message, views.roll(roller, hand))
else:
error_text = (
f'Sorry, such Throw ({throwname}) is not '
f'registered for your active char {char.name}'
)
reply(message, views.error(error_text))
#
# Roll shorthands commands
#
@handler(commands=['roll20'])
def roll_20(message):
shorthand(message, 20)
@handler(commands=['roll12'])
def roll_12(message):
shorthand(message, 12)
@handler(commands=['roll10'])
def roll_10(message):
shorthand(message, 10)
@handler(commands=['roll8'])
def roll_8(message):
shorthand(message, 8)
@handler(commands=['roll6'])
def roll_6(message):
shorthand(message, 6)
@handler(commands=['roll4'])
def roll_4(message):
shorthand(message, 4)
#
#
# HELPER FUNCTIONS
#
#
def reply(to_message: object, with_message: str):
"""
Reply to given incoming message with outcoming message
(with Telegram reply wrapper).
* to_message: the original message object
came to bot from user
* with_message: answer the bot should send to
the author of incoming_message
"""
bot.reply_to(
to_message,
with_message,
parse_mode='HTML'
)
def send(incoming_message: object, outcoming_message: str):
"""
Send message (without Telegram reply wrapper).
* incoming_message: the original message object
came to bot from user
* outcoming_message: answer the bot should send to
the author of incoming_message
"""
bot.send_message(
incoming_message.from_user.id,
outcoming_message,
parse_mode='HTML'
)
def shorthand(message: object, dice: int):
"""
Make a throw with one dice of cpecified type and
send the roll result to the user.
* message: the original message object
came to bot from user
* dice: dice type (for exaple, 1d20 dice should be described
as 20, 1d100 as 100, etc.)
"""
descr = message.text[8:] if dice > 9 else message.text[7:]
roller = DiceRoller(f'/roll d{dice} ' + descr, message.from_user)
hand = roller.hand
Roll.register()
reply(message, views.roll(roller, hand))