18
18
from cloudbot import clients
19
19
from cloudbot .client import Client
20
20
from cloudbot .config import Config
21
- from cloudbot .event import Event , CommandEvent , RegexEvent , EventType
21
+ from cloudbot .event import CommandEvent , Event , EventType , RegexEvent
22
22
from cloudbot .hook import Action
23
23
from cloudbot .plugin import PluginManager
24
- from cloudbot .reloader import PluginReloader , ConfigReloader
25
- from cloudbot .util import database , formatting , async_util
24
+ from cloudbot .reloader import ConfigReloader , PluginReloader
25
+ from cloudbot .util import async_util , database , formatting
26
26
from cloudbot .util .mapping import KeyFoldDict
27
27
28
28
logger = logging .getLogger ("cloudbot" )
@@ -58,28 +58,34 @@ def clean_name(n):
58
58
:type n: str
59
59
:rtype: str
60
60
"""
61
- return re .sub (' [^A-Za-z0-9_]+' , '' , n .replace (" " , "_" ))
61
+ return re .sub (" [^A-Za-z0-9_]+" , "" , n .replace (" " , "_" ))
62
62
63
63
64
64
def get_cmd_regex (event ):
65
65
conn = event .conn
66
66
is_pm = event .chan .lower () == event .nick .lower ()
67
- command_prefix = re .escape (conn .config .get (' command_prefix' , '.' ))
67
+ command_prefix = re .escape (conn .config .get (" command_prefix" , "." ))
68
68
conn_nick = re .escape (event .conn .nick )
69
69
cmd_re = re .compile (
70
70
r"""
71
71
^
72
72
# Prefix or nick
73
73
(?:
74
- (?P<prefix>[""" + command_prefix + r"""])""" + ('?' if is_pm else '' ) + r"""
74
+ (?P<prefix>["""
75
+ + command_prefix
76
+ + r"""])"""
77
+ + ("?" if is_pm else "" )
78
+ + r"""
75
79
|
76
- """ + conn_nick + r"""[,;:]+\s+
80
+ """
81
+ + conn_nick
82
+ + r"""[,;:]+\s+
77
83
)
78
84
(?P<command>\w+) # Command
79
85
(?:$|\s+)
80
86
(?P<text>.*) # Text
81
87
""" ,
82
- re .IGNORECASE | re .VERBOSE
88
+ re .IGNORECASE | re .VERBOSE ,
83
89
)
84
90
return cmd_re
85
91
@@ -126,7 +132,7 @@ def __init__(self, loop=asyncio.get_event_loop()):
126
132
self .memory = collections .defaultdict ()
127
133
128
134
# declare and create data folder
129
- self .data_dir = os .path .abspath (' data' )
135
+ self .data_dir = os .path .abspath (" data" )
130
136
if not os .path .exists (self .data_dir ):
131
137
logger .debug ("Data folder not found, creating." )
132
138
os .mkdir (self .data_dir )
@@ -141,11 +147,15 @@ def __init__(self, loop=asyncio.get_event_loop()):
141
147
self .config_reloading_enabled = reloading_conf .get ("config_reloading" , True )
142
148
143
149
# this doesn't REALLY need to be here but it's nice
144
- self .user_agent = self .config .get ('user_agent' , 'CloudBot/3.0 - CloudBot Refresh '
145
- '<https://github.com/CloudBotIRC/CloudBot/>' )
150
+ self .repo_link = self .config .get (
151
+ "repo_link" , "https://github.com/TotallyNotRobots/CloudBot/"
152
+ )
153
+ self .user_agent = self .config .get (
154
+ "user_agent" , "CloudBot/3.0 - CloudBot Refresh <{repo_link}>"
155
+ ).format (repo_link = self .repo_link )
146
156
147
157
# setup db
148
- db_path = self .config .get (' database' , ' sqlite:///cloudbot.db' )
158
+ db_path = self .config .get (" database" , " sqlite:///cloudbot.db" )
149
159
self .db_engine = create_engine (db_path )
150
160
self .db_factory = sessionmaker (bind = self .db_engine )
151
161
self .db_session = scoped_session (self .db_factory )
@@ -201,15 +211,14 @@ def register_client(self, name, cls):
201
211
202
212
def create_connections (self ):
203
213
""" Create a BotConnection for all the networks defined in the config """
204
- for config in self .config [' connections' ]:
214
+ for config in self .config [" connections" ]:
205
215
# strip all spaces and capitalization from the connection name
206
- name = clean_name (config [' name' ])
207
- nick = config [' nick' ]
216
+ name = clean_name (config [" name" ])
217
+ nick = config [" nick" ]
208
218
_type = config .get ("type" , "irc" )
209
219
210
220
self .connections [name ] = self .get_client (_type )(
211
- self , _type , name , nick , config = config ,
212
- channels = config ['channels' ]
221
+ self , _type , name , nick , config = config , channels = config ["channels" ]
213
222
)
214
223
logger .debug ("[%s] Created connection." , name )
215
224
@@ -283,7 +292,9 @@ async def _init_routine(self):
283
292
conn .active = True
284
293
285
294
# Connect to servers
286
- await asyncio .gather (* [conn .try_connect () for conn in self .connections .values ()], loop = self .loop )
295
+ await asyncio .gather (
296
+ * [conn .try_connect () for conn in self .connections .values ()], loop = self .loop
297
+ )
287
298
logger .debug ("Connections created." )
288
299
289
300
# Run a manual garbage collection cycle, to clean up any unused objects created during initialization
@@ -294,7 +305,7 @@ def load_clients(self):
294
305
Load all clients from the "clients" directory
295
306
"""
296
307
scanner = Scanner (bot = self )
297
- scanner .scan (clients , categories = [' cloudbot.client' ])
308
+ scanner .scan (clients , categories = [" cloudbot.client" ])
298
309
299
310
async def process (self , event ):
300
311
"""
@@ -331,7 +342,9 @@ def add_hook(hook, _event, _run_before=False):
331
342
for raw_hook in self .plugin_manager .catch_all_triggers :
332
343
# run catch-all coroutine hooks before all others - TODO: Make this a plugin argument
333
344
run_before = not raw_hook .threaded
334
- if not add_hook (raw_hook , Event (hook = raw_hook , base_event = event ), _run_before = run_before ):
345
+ if not add_hook (
346
+ raw_hook , Event (hook = raw_hook , base_event = event ), _run_before = run_before
347
+ ):
335
348
# The hook has an action of Action.HALT* so stop adding new tasks
336
349
break
337
350
@@ -355,12 +368,16 @@ def add_hook(hook, _event, _run_before=False):
355
368
cmd_match = get_cmd_regex (event ).match (event .content )
356
369
357
370
if cmd_match :
358
- command_prefix = event .conn .config .get (' command_prefix' , '.' )
359
- prefix = cmd_match .group (' prefix' ) or command_prefix [0 ]
360
- command = cmd_match .group (' command' ).lower ()
361
- text = cmd_match .group (' text' ).strip ()
371
+ command_prefix = event .conn .config .get (" command_prefix" , "." )
372
+ prefix = cmd_match .group (" prefix" ) or command_prefix [0 ]
373
+ command = cmd_match .group (" command" ).lower ()
374
+ text = cmd_match .group (" text" ).strip ()
362
375
cmd_event = partial (
363
- CommandEvent , text = text , triggered_command = command , base_event = event , cmd_prefix = prefix
376
+ CommandEvent ,
377
+ text = text ,
378
+ triggered_command = command ,
379
+ base_event = event ,
380
+ cmd_prefix = prefix ,
364
381
)
365
382
if command in self .plugin_manager .commands :
366
383
command_hook = self .plugin_manager .commands [command ]
@@ -380,7 +397,9 @@ def add_hook(hook, _event, _run_before=False):
380
397
command_event = cmd_event (hook = command_hook )
381
398
add_hook (command_hook , command_event )
382
399
else :
383
- commands = sorted (command for command , plugin in potential_matches )
400
+ commands = sorted (
401
+ command for command , plugin in potential_matches
402
+ )
384
403
txt_list = formatting .get_text_list (commands )
385
404
event .notice ("Possible matches: {}" .format (txt_list ))
386
405
@@ -397,7 +416,9 @@ def add_hook(hook, _event, _run_before=False):
397
416
regex_match = regex .search (event .content )
398
417
if regex_match :
399
418
regex_matched = True
400
- regex_event = RegexEvent (hook = regex_hook , match = regex_match , base_event = event )
419
+ regex_event = RegexEvent (
420
+ hook = regex_hook , match = regex_match , base_event = event
421
+ )
401
422
if not add_hook (regex_hook , regex_event ):
402
423
# The hook has an action of Action.HALT* so stop adding new tasks
403
424
break
0 commit comments