Index: decisionbot.py |
diff --git a/decisionbot.py b/decisionbot.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bba1b90ba8f46d9ee145814b94da4061942ba044 |
--- /dev/null |
+++ b/decisionbot.py |
@@ -0,0 +1,39 @@ |
+#!/usr/bin/env python |
+# coding: utf-8 |
+ |
+import random |
+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*$" % |
+ 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) |