Last active 1440821209

Autoplayer for RoyalDev's Cards Against Humanity IRC bot

Revision 1481f94d1c077c1d0e5d501d4355db2631ad4573

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