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: Ensure IRC bot username is at the beginning of a question string Created Feb. 25, 2016, 7:52 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 #!/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 """
10 Decisionbot - A simple IRC bot to help make "coin flip" decisions.
11
12 botname: x or y?
13 => x
14
15 botname: a or b or c?
16 => b
17 """
18
19 class Decisionbot():
20 def __init__(self, config, queue):
21 self.queue = queue
22
23 nickname = config.get("main", "nickname")
24 self.question_regexp = re.compile("^%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))
26
27 def on_pubmsg(self, connection, event):
28 channel = event.target()
29 message = event.arguments()[0]
30 sender = nm_to_n(event.source())
31
32 match = self.question_regexp.search(message)
33 if (match):
34 choices = re.split("\s+or\s+", match.group(1).strip("? \t"))
35 if len(choices) > 1:
36 self.say_public(channel, "%s: %s" % (sender, random.choice(choices)))
37
38 def say_public(self, channel, text):
39 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