Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 """ | 1 """ |
2 Decisionbot - A simple IRC bot to help make "coin flip" decisions. | 2 Decisionbot - A simple IRC bot to help make "coin flip" decisions. |
3 | 3 |
4 botname: x or y? | 4 botname: x or y? |
5 => x | 5 => x |
6 | 6 |
7 botname: a or b or c? | 7 botname: a or b or c? |
8 => b | 8 => b |
9 """ | 9 """ |
10 | |
10 import random | 11 import random |
Sebastian Noack
2016/02/25 20:47:53
Nit: Please add an empty line between the docstrin
kzar
2016/02/25 20:52:40
Done.
| |
11 import re | 12 import re |
12 | 13 |
13 from irclib import nm_to_n | 14 from irclib import nm_to_n |
14 | 15 |
15 | 16 |
16 class Decisionbot(): | 17 class Decisionbot(): |
17 def __init__(self, config, queue): | 18 def __init__(self, config, queue): |
18 self.queue = queue | 19 self.queue = queue |
19 | 20 |
20 nickname = config.get("main", "nickname") | 21 nickname = config.get("main", "nickname") |
21 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:47:53
I just realized that you don't use r"" strings. Bu
kzar
2016/02/25 20:52:40
Done.
| |
22 re.escape(nickname), re.IGNORECASE) | 23 re.escape(nickname), re.IGNORECASE) |
23 self.question_delim_regexp = re.compile("\s+or\s+", re.IGNORECASE) | 24 self.question_delim_regexp = re.compile(r"\s+or\s+", re.IGNORECASE) |
24 | 25 |
25 def on_pubmsg(self, connection, event): | 26 def on_pubmsg(self, connection, event): |
26 channel = event.target() | 27 channel = event.target() |
27 message = event.arguments()[0] | 28 message = event.arguments()[0] |
28 sender = nm_to_n(event.source()) | 29 sender = nm_to_n(event.source()) |
29 | 30 |
30 match = self.question_regexp.search(message) | 31 match = self.question_regexp.search(message) |
31 if (match): | 32 if (match): |
32 choices = self.question_delim_regexp.split(match.group(1).strip("? \t")) | 33 choices = self.question_delim_regexp.split(match.group(1).strip("? \t")) |
33 if len(choices) > 1: | 34 if len(choices) > 1: |
34 self.say_public(channel, "%s: %s" % (sender, random.choice(choices))) | 35 self.say_public(channel, "%s: %s" % (sender, random.choice(choices))) |
35 | 36 |
36 def say_public(self, channel, text): | 37 def say_public(self, channel, text): |
37 self.queue.send(text, channel) | 38 self.queue.send(text, channel) |
LEFT | RIGHT |