cahplayer.py
· 3.2 KiB · Python
Raw
# requirements: PyYAML==3.11 Twisted==14.0.2
from twisted.internet import reactor, task, defer, protocol
from twisted.python import log
from twisted.words.protocols import irc
from twisted.application import internet, service
import yaml, sys, random, re
with open(sys.argv[1] if len(sys.argv) > 1 else 'config.yml') as f:
config = yaml.load(f.read())
HOST, PORT = config['host'], config['port']
class CAHPlayerProtocol(irc.IRCClient):
nickname = config['nick']
password = config['password'] if 'password' in config else None
username = config['nick']
versionName = "CAHPlayer https://gist.github.com/blha303/557e23b23b0a7e32b3b6"
versionNum = "v1"
realname = config['nick']
cards = []
need = 0
phrase = ""
czar = False
choices = []
def signedOn(self):
for channel in self.factory.channels:
self.join(channel)
def noticed(self, user, channel, message):
nick, _, host = user.partition('!')
log.msg("NOTICE: {} <{}> {}".format(channel, nick, message))
if nick == config["gamebot"]:
if message[:2] == "1.":
self.cards = re.split("\x02|\x0f", message)[1::2]
picks = [random.choice(self.cards) for x in xrange(1,self.need+1)]
nums = [str(self.cards.index(p)+1) for p in picks]
# self.msg(self.factory.channels[0], self.phrase.format(*picks))
self.msg(self.factory.channels[0], "!pick " + " ".join(nums))
def privmsg(self, user, channel, message):
if not channel in self.factory.channels:
return
nick, _, host = user.partition('!')
log.msg("{} <{}> {}".format(channel, nick, message))
split = message.strip().split(" ")
if nick == config["gamebot"]:
if "<BLANK>" in message:
self.phrase = message.replace("<BLANK>", "{}").replace("\x02", "")
self.need = message.count("<BLANK>")
elif self.nickname[1:] in message and "is the card czar." in message:
self.czar = True
elif self.nickname[1:] in message and "is picking a winner" in message:
self.czar = False
log.msg(", ".join(self.choices))
self.msg(channel, "!pick " + random.choice(self.choices).split(".")[0])
elif self.czar:
self.choices.append(message.replace("\x02", "").replace("\x0f", ""))
if self.nickname in split[0]:
if split[1].lower() == "say" and nick == "blha303":
self.msg(channel, " ".join(split[2:]))
if split[1].lower() == "join":
self.msg(channel, "!join")
if split[1].lower() == "stop":
self.msg(channel, "!stop")
class CAHPlayerFactory(protocol.ReconnectingClientFactory):
protocol = CAHPlayerProtocol
channels = config["channels"]
if __name__ == '__main__':
reactor.connectTCP(HOST, PORT, CAHPlayerFactory())
log.startLogging(sys.stdout)
reactor.run()
elif __name__ == '__builtin__':
application = service.Application('CAHPlayer')
ircService = internet.TCPClient(HOST, PORT, CAHPlayerFactory())
ircService.setServiceParent(application)
| 1 | # requirements: PyYAML==3.11 Twisted==14.0.2 |
| 2 | from twisted.internet import reactor, task, defer, protocol |
| 3 | from twisted.python import log |
| 4 | from twisted.words.protocols import irc |
| 5 | from twisted.application import internet, service |
| 6 | import yaml, sys, random, re |
| 7 | |
| 8 | with open(sys.argv[1] if len(sys.argv) > 1 else 'config.yml') as f: |
| 9 | config = yaml.load(f.read()) |
| 10 | HOST, PORT = config['host'], config['port'] |
| 11 | |
| 12 | class 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 | |
| 67 | class CAHPlayerFactory(protocol.ReconnectingClientFactory): |
| 68 | protocol = CAHPlayerProtocol |
| 69 | channels = config["channels"] |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | reactor.connectTCP(HOST, PORT, CAHPlayerFactory()) |
| 73 | log.startLogging(sys.stdout) |
| 74 | reactor.run() |
| 75 | |
| 76 | elif __name__ == '__builtin__': |
| 77 | application = service.Application('CAHPlayer') |
| 78 | ircService = internet.TCPClient(HOST, PORT, CAHPlayerFactory()) |
| 79 | ircService.setServiceParent(application) |
| 80 |
player1.yml
· 104 B · YAML
Raw
host: irc.esper.net
port: 6667
nick: CAHPlayer1
channels: ['#CardsAgainstHumanity']
gamebot: TheHumanity
| 1 | host: irc.esper.net |
| 2 | port: 6667 |
| 3 | nick: CAHPlayer1 |
| 4 | channels: ['#CardsAgainstHumanity'] |
| 5 | gamebot: TheHumanity |
player2.yml
· 104 B · YAML
Raw
host: irc.esper.net
port: 6667
nick: CAHPlayer2
channels: ['#CardsAgainstHumanity']
gamebot: TheHumanity
| 1 | host: irc.esper.net |
| 2 | port: 6667 |
| 3 | nick: CAHPlayer2 |
| 4 | channels: ['#CardsAgainstHumanity'] |
| 5 | gamebot: TheHumanity |