Skip to content

Commit cb3e1d9

Browse files
committed
Remove try: block in favor of asyncio logging module! o/ lukeroge
1 parent 7054b06 commit cb3e1d9

1 file changed

Lines changed: 63 additions & 67 deletions

File tree

cloudbot/bot.py

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -195,72 +195,68 @@ def process(self, event):
195195
"""
196196
:type event: Event
197197
"""
198-
try:
199-
run_before_tasks = []
200-
tasks = []
201-
command_prefix = event.conn.config.get('command_prefix', '.')
202-
203-
# Raw IRC hook
204-
for raw_hook in self.plugin_manager.catch_all_triggers:
205-
# run catch-all coroutine hooks before all others - TODO: Make this a plugin argument
206-
if not raw_hook.threaded:
207-
run_before_tasks.append(
208-
self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
198+
run_before_tasks = []
199+
tasks = []
200+
command_prefix = event.conn.config.get('command_prefix', '.')
201+
202+
# Raw IRC hook
203+
for raw_hook in self.plugin_manager.catch_all_triggers:
204+
# run catch-all coroutine hooks before all others - TODO: Make this a plugin argument
205+
if not raw_hook.threaded:
206+
run_before_tasks.append(
207+
self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
208+
else:
209+
tasks.append(self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
210+
if event.irc_command in self.plugin_manager.raw_triggers:
211+
for raw_hook in self.plugin_manager.raw_triggers[event.irc_command]:
212+
tasks.append(self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
213+
214+
# Event hooks
215+
if event.type in self.plugin_manager.event_type_hooks:
216+
for event_hook in self.plugin_manager.event_type_hooks[event.type]:
217+
tasks.append(self.plugin_manager.launch(event_hook, Event(hook=event_hook, base_event=event)))
218+
219+
if event.type is EventType.message:
220+
# Commands
221+
if event.chan.lower() == event.nick.lower(): # private message, no command prefix
222+
command_re = r'(?i)^(?:[{}]?|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
223+
else:
224+
command_re = r'(?i)^(?:[{}]|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
225+
226+
cmd_match = re.match(command_re, event.content)
227+
228+
if cmd_match:
229+
command = cmd_match.group(1).lower()
230+
if command in self.plugin_manager.commands:
231+
command_hook = self.plugin_manager.commands[command]
232+
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
233+
triggered_command=command, base_event=event)
234+
tasks.append(self.plugin_manager.launch(command_hook, command_event))
209235
else:
210-
tasks.append(self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
211-
if event.irc_command in self.plugin_manager.raw_triggers:
212-
for raw_hook in self.plugin_manager.raw_triggers[event.irc_command]:
213-
tasks.append(self.plugin_manager.launch(raw_hook, Event(hook=raw_hook, base_event=event)))
214-
215-
# Event hooks
216-
if event.type in self.plugin_manager.event_type_hooks:
217-
for event_hook in self.plugin_manager.event_type_hooks[event.type]:
218-
tasks.append(self.plugin_manager.launch(event_hook, Event(hook=event_hook, base_event=event)))
219-
220-
if event.type is EventType.message:
221-
# Commands
222-
if event.chan.lower() == event.nick.lower(): # private message, no command prefix
223-
command_re = r'(?i)^(?:[{}]?|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
236+
potential_matches = []
237+
for potential_match, plugin in self.plugin_manager.commands.items():
238+
if potential_match.startswith(command):
239+
potential_matches.append((potential_match, plugin))
240+
if potential_matches:
241+
if len(potential_matches) == 1:
242+
command_hook = potential_matches[0][1]
243+
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
244+
triggered_command=command, base_event=event)
245+
tasks.append(self.plugin_manager.launch(command_hook, command_event))
246+
else:
247+
event.notice("Possible matches: {}".format(
248+
formatting.get_text_list([command for command, plugin in potential_matches])))
249+
250+
# Regex hooks
251+
for regex, regex_hook in self.plugin_manager.regex_hooks:
252+
if not regex_hook.run_on_cmd and cmd_match:
253+
pass
224254
else:
225-
command_re = r'(?i)^(?:[{}]|{}[,;:]+\s+)(\w+)(?:$|\s+)(.*)'.format(command_prefix, event.conn.nick)
226-
227-
cmd_match = re.match(command_re, event.content)
228-
229-
if cmd_match:
230-
command = cmd_match.group(1).lower()
231-
if command in self.plugin_manager.commands:
232-
command_hook = self.plugin_manager.commands[command]
233-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
234-
triggered_command=command, base_event=event)
235-
tasks.append(self.plugin_manager.launch(command_hook, command_event))
236-
else:
237-
potential_matches = []
238-
for potential_match, plugin in self.plugin_manager.commands.items():
239-
if potential_match.startswith(command):
240-
potential_matches.append((potential_match, plugin))
241-
if potential_matches:
242-
if len(potential_matches) == 1:
243-
command_hook = potential_matches[0][1]
244-
command_event = CommandEvent(hook=command_hook, text=cmd_match.group(2).strip(),
245-
triggered_command=command, base_event=event)
246-
tasks.append(self.plugin_manager.launch(command_hook, command_event))
247-
else:
248-
event.notice("Possible matches: {}".format(
249-
formatting.get_text_list([command for command, plugin in potential_matches])))
250-
251-
# Regex hooks
252-
for regex, regex_hook in self.plugin_manager.regex_hooks:
253-
if not regex_hook.run_on_cmd and cmd_match:
254-
pass
255-
else:
256-
regex_match = regex.search(event.content)
257-
if regex_match:
258-
regex_event = RegexEvent(hook=regex_hook, match=regex_match, base_event=event)
259-
tasks.append(self.plugin_manager.launch(regex_hook, regex_event))
260-
261-
# Run the tasks
262-
yield from asyncio.gather(*run_before_tasks, loop=self.loop)
263-
yield from asyncio.gather(*tasks, loop=self.loop)
264-
265-
except:
266-
logger.exception("Error while processing event")
255+
regex_match = regex.search(event.content)
256+
if regex_match:
257+
regex_event = RegexEvent(hook=regex_hook, match=regex_match, base_event=event)
258+
tasks.append(self.plugin_manager.launch(regex_hook, regex_event))
259+
260+
# Run the tasks
261+
yield from asyncio.gather(*run_before_tasks, loop=self.loop)
262+
yield from asyncio.gather(*tasks, loop=self.loop)

0 commit comments

Comments
 (0)