Steven Smith revised this gist . Go to revision
4 files changed, 4 insertions, 3 deletions
cahplayer.py
| @@ -1,4 +1,3 @@ | |||
| 1 | - | # requirements: PyYAML==3.11 Twisted==14.0.2 | |
| 2 | 1 | from twisted.internet import reactor, task, defer, protocol | |
| 3 | 2 | from twisted.python import log | |
| 4 | 3 | from twisted.words.protocols import irc | |
player1.yml
| @@ -2,4 +2,4 @@ host: irc.esper.net | |||
| 2 | 2 | port: 6667 | |
| 3 | 3 | nick: CAHPlayer1 | |
| 4 | 4 | channels: ['#CardsAgainstHumanity'] | |
| 5 | - | gamebot: TheHumanity | |
| 5 | + | gamebot: TheHumanity | |
player2.yml
| @@ -2,4 +2,4 @@ host: irc.esper.net | |||
| 2 | 2 | port: 6667 | |
| 3 | 3 | nick: CAHPlayer2 | |
| 4 | 4 | channels: ['#CardsAgainstHumanity'] | |
| 5 | - | gamebot: TheHumanity | |
| 5 | + | gamebot: TheHumanity | |
requirements.txt(file created)
| @@ -0,0 +1,2 @@ | |||
| 1 | + | PyYAML==3.11 | |
| 2 | + | Twisted==14.0.2 | |
Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion
cahplayer.py
| @@ -1,3 +1,4 @@ | |||
| 1 | + | # requirements: PyYAML==3.11 Twisted==14.0.2 | |
| 1 | 2 | from twisted.internet import reactor, task, defer, protocol | |
| 2 | 3 | from twisted.python import log | |
| 3 | 4 | from twisted.words.protocols import irc | |
Steven Smith revised this gist . Go to revision
1 file changed, 2 insertions, 2 deletions
cahplayer.py
| @@ -12,8 +12,8 @@ class CAHPlayerProtocol(irc.IRCClient): | |||
| 12 | 12 | nickname = config['nick'] | |
| 13 | 13 | password = config['password'] if 'password' in config else None | |
| 14 | 14 | username = config['nick'] | |
| 15 | - | versionName = "CAHPlayer" | |
| 16 | - | versionNum = "v0.0.1" | |
| 15 | + | versionName = "CAHPlayer https://gist.github.com/blha303/557e23b23b0a7e32b3b6" | |
| 16 | + | versionNum = "v1" | |
| 17 | 17 | realname = config['nick'] | |
| 18 | 18 | cards = [] | |
| 19 | 19 | need = 0 | |
Steven Smith revised this gist . Go to revision
3 files changed, 88 insertions
cahplayer.py(file created)
| @@ -0,0 +1,78 @@ | |||
| 1 | + | from twisted.internet import reactor, task, defer, protocol | |
| 2 | + | from twisted.python import log | |
| 3 | + | from twisted.words.protocols import irc | |
| 4 | + | from twisted.application import internet, service | |
| 5 | + | import yaml, sys, random, re | |
| 6 | + | ||
| 7 | + | with open(sys.argv[1] if len(sys.argv) > 1 else 'config.yml') as f: | |
| 8 | + | config = yaml.load(f.read()) | |
| 9 | + | HOST, PORT = config['host'], config['port'] | |
| 10 | + | ||
| 11 | + | class CAHPlayerProtocol(irc.IRCClient): | |
| 12 | + | nickname = config['nick'] | |
| 13 | + | password = config['password'] if 'password' in config else None | |
| 14 | + | username = config['nick'] | |
| 15 | + | versionName = "CAHPlayer" | |
| 16 | + | versionNum = "v0.0.1" | |
| 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 | + | ||
| 66 | + | class CAHPlayerFactory(protocol.ReconnectingClientFactory): | |
| 67 | + | protocol = CAHPlayerProtocol | |
| 68 | + | channels = config["channels"] | |
| 69 | + | ||
| 70 | + | if __name__ == '__main__': | |
| 71 | + | reactor.connectTCP(HOST, PORT, CAHPlayerFactory()) | |
| 72 | + | log.startLogging(sys.stdout) | |
| 73 | + | reactor.run() | |
| 74 | + | ||
| 75 | + | elif __name__ == '__builtin__': | |
| 76 | + | application = service.Application('CAHPlayer') | |
| 77 | + | ircService = internet.TCPClient(HOST, PORT, CAHPlayerFactory()) | |
| 78 | + | ircService.setServiceParent(application) | |
player1.yml(file created)
| @@ -0,0 +1,5 @@ | |||
| 1 | + | host: irc.esper.net | |
| 2 | + | port: 6667 | |
| 3 | + | nick: CAHPlayer1 | |
| 4 | + | channels: ['#CardsAgainstHumanity'] | |
| 5 | + | gamebot: TheHumanity | |
player2.yml(file created)
| @@ -0,0 +1,5 @@ | |||
| 1 | + | host: irc.esper.net | |
| 2 | + | port: 6667 | |
| 3 | + | nick: CAHPlayer2 | |
| 4 | + | channels: ['#CardsAgainstHumanity'] | |
| 5 | + | gamebot: TheHumanity | |