OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # coding: utf-8 |
| 3 |
| 4 import random |
| 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*$" % |
| 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) |
OLD | NEW |