Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/env python | |
2 # coding: utf-8 | |
Sebastian Noack
2016/02/25 20:09:42
Nit: We recently decided to not use coding declara
kzar
2016/02/25 20:29:51
Done.
| |
3 | |
4 import random | |
Sebastian Noack
2016/02/25 20:09:42
Nit; also from PEP-8: Imports are always put at th
kzar
2016/02/25 20:29:51
Done.
| |
5 import re | |
6 | |
7 from irclib import nm_to_n | |
8 | |
9 """ | 1 """ |
10 Decisionbot - A simple IRC bot to help make "coin flip" decisions. | 2 Decisionbot - A simple IRC bot to help make "coin flip" decisions. |
11 | 3 |
12 botname: x or y? | 4 botname: x or y? |
13 => x | 5 => x |
14 | 6 |
15 botname: a or b or c? | 7 botname: a or b or c? |
16 => b | 8 => b |
17 """ | 9 """ |
18 | 10 |
11 import random | |
12 import re | |
13 | |
14 from irclib import nm_to_n | |
15 | |
16 | |
19 class Decisionbot(): | 17 class Decisionbot(): |
20 def __init__(self, config, queue): | 18 def __init__(self, config, queue): |
21 self.queue = queue | 19 self.queue = queue |
22 | 20 |
23 nickname = config.get("main", "nickname") | 21 nickname = config.get("main", "nickname") |
24 self.question_regexp = re.compile("^%s:?(.+\s+or\s+.+)\?+\s*$" % | 22 self.question_regexp = re.compile(r"^%s:?(.+\s+or\s+.+)\?+\s*$" % |
Sebastian Noack
2016/02/25 20:09:42
Perhaps we should use the case-insensitive flag fo
kzar
2016/02/25 20:29:51
Done.
| |
25 re.escape(nickname)) | 23 re.escape(nickname), re.IGNORECASE) |
24 self.question_delim_regexp = re.compile(r"\s+or\s+", re.IGNORECASE) | |
26 | 25 |
27 def on_pubmsg(self, connection, event): | 26 def on_pubmsg(self, connection, event): |
28 channel = event.target() | 27 channel = event.target() |
29 message = event.arguments()[0] | 28 message = event.arguments()[0] |
30 sender = nm_to_n(event.source()) | 29 sender = nm_to_n(event.source()) |
31 | 30 |
32 match = self.question_regexp.search(message) | 31 match = self.question_regexp.search(message) |
33 if (match): | 32 if (match): |
34 choices = re.split("\s+or\s+", match.group(1).strip("? \t")) | 33 choices = self.question_delim_regexp.split(match.group(1).strip("? \t")) |
35 if len(choices) > 1: | 34 if len(choices) > 1: |
36 self.say_public(channel, "%s: %s" % (sender, random.choice(choices))) | 35 self.say_public(channel, "%s: %s" % (sender, random.choice(choices))) |
37 | 36 |
38 def say_public(self, channel, text): | 37 def say_public(self, channel, text): |
39 self.queue.send(text, channel) | 38 self.queue.send(text, channel) |
LEFT | RIGHT |