Last active 1440821209

Autoplayer for RoyalDev's Cards Against Humanity IRC bot

cahplayer.py Raw
1from twisted.internet import reactor, task, defer, protocol
2from twisted.python import log
3from twisted.words.protocols import irc
4from twisted.application import internet, service
5import yaml, sys, random, re
6
7with open(sys.argv[1] if len(sys.argv) > 1 else 'config.yml') as f:
8 config = yaml.load(f.read())
9HOST, PORT = config['host'], config['port']
10
11class CAHPlayerProtocol(irc.IRCClient):
12 nickname = config['nick']
13 password = config['password'] if 'password' in config else None
14 username = config['nick']
15 versionName = "CAHPlayer https://gist.github.com/blha303/557e23b23b0a7e32b3b6"
16 versionNum = "v1"
17 realname = config['nick']
18 cards = []
19 need = 0
20 phrase = ""
21 czar = False
22 choices = []
23
24 def signedOn(self):
25 for channel in self.factory.channels:
26 self.join(channel)
27
28 def noticed(self, user, channel, message):
29 nick, _, host = user.partition('!')
30 log.msg("NOTICE: {} <{}> {}".format(channel, nick, message))
31 if nick == config["gamebot"]:
32 if message[:2] == "1.":
33 self.cards = re.split("\x02|\x0f", message)[1::2]
34 picks = [random.choice(self.cards) for x in xrange(1,self.need+1)]
35 nums = [str(self.cards.index(p)+1) for p in picks]
36# self.msg(self.factory.channels[0], self.phrase.format(*picks))
37 self.msg(self.factory.channels[0], "!pick " + " ".join(nums))
38
39 def privmsg(self, user, channel, message):
40 if not channel in self.factory.channels:
41 return
42 nick, _, host = user.partition('!')
43 log.msg("{} <{}> {}".format(channel, nick, message))
44 split = message.strip().split(" ")
45 if nick == config["gamebot"]:
46 if "<BLANK>" in message:
47 self.phrase = message.replace("<BLANK>", "{}").replace("\x02", "")
48 self.need = message.count("<BLANK>")
49 elif self.nickname[1:] in message and "is the card czar." in message:
50 self.czar = True
51 elif self.nickname[1:] in message and "is picking a winner" in message:
52 self.czar = False
53 log.msg(", ".join(self.choices))
54 self.msg(channel, "!pick " + random.choice(self.choices).split(".")[0])
55 elif self.czar:
56 self.choices.append(message.replace("\x02", "").replace("\x0f", ""))
57 if self.nickname in split[0]:
58 if split[1].lower() == "say" and nick == "blha303":
59 self.msg(channel, " ".join(split[2:]))
60 if split[1].lower() == "join":
61 self.msg(channel, "!join")
62 if split[1].lower() == "stop":
63 self.msg(channel, "!stop")
64
65
66class CAHPlayerFactory(protocol.ReconnectingClientFactory):
67 protocol = CAHPlayerProtocol
68 channels = config["channels"]
69
70if __name__ == '__main__':
71 reactor.connectTCP(HOST, PORT, CAHPlayerFactory())
72 log.startLogging(sys.stdout)
73 reactor.run()
74
75elif __name__ == '__builtin__':
76 application = service.Application('CAHPlayer')
77 ircService = internet.TCPClient(HOST, PORT, CAHPlayerFactory())
78 ircService.setServiceParent(application)
79
player1.yml Raw
1host: irc.esper.net
2port: 6667
3nick: CAHPlayer1
4channels: ['#CardsAgainstHumanity']
5gamebot: TheHumanity
6
player2.yml Raw
1host: irc.esper.net
2port: 6667
3nick: CAHPlayer2
4channels: ['#CardsAgainstHumanity']
5gamebot: TheHumanity
6
requirements.txt Raw
1PyYAML==3.11
2Twisted==14.0.2
3