forked from JQ-Networks/UnifiedMessageRelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
75 lines (64 loc) · 2.96 KB
/
command.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
import global_vars
import logging
from functools import wraps
logger = logging.getLogger("CTBMain." + __name__)
class Command:
def __init__(self,
command: str,
short_command: str,
handler: callable,
require_admin: bool,
tg_only: bool,
qq_only: bool,
description: str):
"""
Command class, used to store commands
:param command: full command
:param short_command: abbreviation of the command
:param handler: function of command handler
:param require_admin: if the command is admin only (not implemented)
:param tg_only: if the command is only available on telegram side
:param qq_only: if the command is only available on QQ side
:param description: description of the command, will show in !!show commands
"""
self.command = command
self.short_command = short_command
self.handler = handler # handler: function(forward_index, tg_group_id, qq_group_id)
self.require_admin = require_admin
self.tg_only = tg_only # if True, handler becomes function(tg_group_id)
self.qq_only = qq_only # if True, handler becomes function(qq_group_id)
self.description = description
def command_listener(command: str,
short_command: str='',
require_admin: bool=False,
tg_only: bool=False,
qq_only: bool=False,
description: str=''):
"""
Command decorator, used to register commands
:param command: full command
:param short_command: abbreviation of the command
:param require_admin: if the command is admin only (not implemented)
:param tg_only: if the command is only available on telegram side
:param qq_only: if the command is only available on QQ side
:param description: description of the command, will show in !!show commands
"""
def decorator(handler):
logger.debug('Registering new command: ' + command + '(' + handler.__name__ + ')')
@wraps(handler)
def return_wrapper(*args, **kwargs):
logger.debug(command + '(' + handler.__name__ + ')' + ' called')
return handler(*args, **kwargs)
global_vars.append_command(Command(command,
short_command,
return_wrapper,
require_admin,
tg_only,
qq_only,
description))
# add command to global_vars, for cross-plugin access
global_vars.create_variable(handler.__name__,
return_wrapper)
logger.debug(command + '(' + handler.__name__ + ') added to global_vars')
return return_wrapper
return decorator