OLD | NEW |
(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) |
OLD | NEW |