Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: decisionbot.py

Issue 29337738: Issue 3703 - Add "coin flip" feature to abpbot (Closed)
Left Patch Set: Created Feb. 25, 2016, 7:37 p.m.
Right Patch Set: Fixed a couple more nits Created Feb. 25, 2016, 8:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « abpbot.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 import random
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*$" %
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)
LEFTRIGHT
« abpbot.py ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld