forked from diegodorgam/hubot-no-js
/
exec.py
126 lines (106 loc) · 3.59 KB
/
exec.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
#! /usr/bin/python3
from collections import namedtuple
from importlib import import_module
from sys import argv, path
import re, logspike
Trigger = namedtuple('Trigger', 'exp, flags')
##################################### Configuration ######################################
# Redis connection (see https://redis-py.readthedocs.io/en/latest/)
# To activate, comment the next line and uncomment the following 2
redis = {}
# from redis import Redis
# redis = Redis()
# Room where to send logging messages
log_room = 'general'
# Verbosity: 1-Error 2-Warning 3-Info 4-Debug
verbosity = 2
# If a sentence does not begin with a command but is matched
# by any of the following regular expressions (with flags),
# execute the indicated script.
trigger_scripts = {
# Example: anytime 'derp' is found in a sentence,
# regardless of case, bot_commands/smack_self.py will be executed
Trigger('derp', re.I): 'smack_self',
# Example: anytime 'bake [word] a cake' is found in a sentence,
# regardless of case, bot_commands/the_cake_is_a_lie.py will be executed
Trigger('bake (\w+) a cake', re.I): 'the_cake_is_a_lie',
}
# Local machine paths where to search for additional modules
local_path = [
'/usr/lib/python3.5' ,
'/usr/lib/python3.5/lib-dynload' ,
'/usr/lib/python3.5/plat-i386-linux-gnu' ,
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3/dist-packages' ,
'/usr/lib/python35.zip' ,
]
##########################################################################################
log_format = '`%(asctime)s:%(msecs)03d [%(levelname)s]` `%(message)s`'
log_manager = logspike.Logspike(fmt=']+> {} <+[ {}'.format(log_room, log_format))
log_manager.set_verbosity(verbosity)
logger = log_manager.logger
path.extend(local_path)
room = argv[1]
username = argv[2]
message = argv[3]
bot_name = argv[4]
direct = message.strip().lower().replace('@', '', 1).startswith(bot_name.lower())
arguments = message.strip().split(' ')
if bot_name.lower() in arguments[0].lower():
arguments.pop(0)
command = arguments.pop(0).strip()
debug_args = '''``
room:\t{}
user:\t{}
msg:\t{}
cmd:\t{}
args:\t{}
bot:\t{}
direct:\t{}
``'''
debug_args = debug_args.format(
room ,
username ,
message ,
command ,
arguments,
bot_name ,
direct ,
)
logger.debug(debug_args)
try:
module = import_module('bot_commands.{}'.format(command))
module.execute(
room = room ,
username = username ,
command = command ,
arguments = arguments,
bot_name = bot_name ,
direct = direct ,
redis = redis ,
logger = logger ,
)
except Exception as cmd_fail:
logger.debug('Failed to execute command: {}'.format(cmd_fail))
for trigger, _command in trigger_scripts.items():
exp = re.compile(trigger.exp, trigger.flags)
matches = re.findall(exp, message)
if not matches:
continue
try:
module = import_module('bot_commands.{}'.format(_command))
except Exception as trigger_fail:
logger.debug('Failed to execute trigger: {}'.format(trigger_fail))
continue
for m in matches:
module.execute(
room = room ,
username = username,
message = message ,
trigger = trigger ,
bot_name = bot_name,
direct = direct ,
redis = redis ,
logger = logger ,
)
exit()