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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « abpbot.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: decisionbot.py
diff --git a/decisionbot.py b/decisionbot.py
new file mode 100644
index 0000000000000000000000000000000000000000..c6623b06b6f26485b3d79e2619f8e3096b790dc6
--- /dev/null
+++ b/decisionbot.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# 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.
+
+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.
+import re
+
+from irclib import nm_to_n
+
+"""
+ Decisionbot - A simple IRC bot to help make "coin flip" decisions.
+
+ botname: x or y?
+ => x
+
+ botname: a or b or c?
+ => b
+"""
+
+class Decisionbot():
+ def __init__(self, config, queue):
+ self.queue = queue
+
+ nickname = config.get("main", "nickname")
+ 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.
+ re.escape(nickname))
+
+ def on_pubmsg(self, connection, event):
+ channel = event.target()
+ message = event.arguments()[0]
+ sender = nm_to_n(event.source())
+
+ match = self.question_regexp.search(message)
+ if (match):
+ choices = re.split("\s+or\s+", match.group(1).strip("? \t"))
+ if len(choices) > 1:
+ self.say_public(channel, "%s: %s" % (sender, random.choice(choices)))
+
+ def say_public(self, channel, text):
+ self.queue.send(text, channel)
« no previous file with comments | « abpbot.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld