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

Side by Side Diff: decisionbot.py

Issue 29337738: Issue 3703 - Add "coin flip" feature to abpbot (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « abpbot.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 """
2 Decisionbot - A simple IRC bot to help make "coin flip" decisions.
3
4 botname: x or y?
5 => x
6
7 botname: a or b or c?
8 => b
9 """
10
11 import random
12 import re
13
14 from irclib import nm_to_n
15
16
17 class Decisionbot():
18 def __init__(self, config, queue):
19 self.queue = queue
20
21 nickname = config.get("main", "nickname")
22 self.question_regexp = re.compile(r"^%s:?(.+\s+or\s+.+)\?+\s*$" %
23 re.escape(nickname), re.IGNORECASE)
24 self.question_delim_regexp = re.compile(r"\s+or\s+", re.IGNORECASE)
25
26 def on_pubmsg(self, connection, event):
27 channel = event.target()
28 message = event.arguments()[0]
29 sender = nm_to_n(event.source())
30
31 match = self.question_regexp.search(message)
32 if (match):
33 choices = self.question_delim_regexp.split(match.group(1).strip("? \t"))
34 if len(choices) > 1:
35 self.say_public(channel, "%s: %s" % (sender, random.choice(choices)))
36
37 def say_public(self, channel, text):
38 self.queue.send(text, channel)
OLDNEW
« no previous file with comments | « abpbot.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld