-
Notifications
You must be signed in to change notification settings - Fork 5
/
questions.py
40 lines (31 loc) · 1.28 KB
/
questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
import re
from ircbot.plugin import IRCPlugin
from ircbot.events import sendmessage
choose_regex = re.compile(r'^((?:(?:.+?), )*)(.+?),? or (.+?)\??$')
class Questions(IRCPlugin):
def directmessage(self, user, channel, message):
if not self.choose_one(user, channel, message):
self.eightball(user, channel, message)
def choose_one(self, user, channel, message):
options = choose_regex.match(message)
if not options:
return False
options = list(options.groups())
if options[0]:
#exclude the last element of options[0] because it's always blank
options = options[0].split(', ')[:-1] + options[1:]
else:
options = options[1:]
if random.randint(1, 500) == 500:
if len(options) == 2:
choice = 'Both.'
else:
choice = ' and '.join(random.sample(map(str,options), 2)) + '.'
else:
choice = random.choice(options)
self.fire(sendmessage(channel, '{}: {}'.format(user.nick, choice)))
return True
def eightball(self, user, channel, message):
if message[-1] == '?':
self.fire(sendmessage(channel, '{}: {}'.format(user.nick, random.choice(eightball_responses))))