cahplayer.py
                        
                             · 3.1 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            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 | 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 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 | |
| 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) | 
| 79 | 
                    
                        
                        player1.yml
                        
                             · 105 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 | 
| 6 | 
                    
                        
                        player2.yml
                        
                             · 105 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 | 
| 6 | 
| 1 | PyYAML==3.11 | 
| 2 | Twisted==14.0.2 | 
| 3 |