SHOW:
|
|
- or go back to the newest paste.
| 1 | import sys | |
| 2 | from twisted.words.protocols import irc | |
| 3 | from twisted.internet import protocol, reactor | |
| 4 | ||
| 5 | class VoteBot(irc.IRCClient): | |
| 6 | ||
| 7 | voted_users_set = set() | |
| 8 | picks = {'1':0, '2':0}
| |
| 9 | ||
| 10 | def _get_nickname(self): | |
| 11 | return self.factory.nickname | |
| 12 | nickname = property(_get_nickname) | |
| 13 | ||
| 14 | def _check_and_process_command(self, user, msg): | |
| 15 | msg_parts = msg.split() | |
| 16 | if len(msg_parts) == 2: | |
| 17 | command, choice = msg_parts | |
| 18 | if command == "!vote": | |
| 19 | if user not in self.voted_users_set: | |
| 20 | try: | |
| 21 | self.picks[choice] += 1 | |
| 22 | self.voted_users_set.add(user) | |
| 23 | except KeyError: | |
| 24 | pass | |
| 25 | ||
| 26 | def signedOn(self): | |
| 27 | print "Signed on as {}.".format(self.nickname)
| |
| 28 | self.join(self.factory.channel) | |
| 29 | ||
| 30 | def joined(self, channel): | |
| 31 | print "Joined {}.".format(channel)
| |
| 32 | ||
| 33 | def privmsg(self, user, channel, msg): | |
| 34 | print msg | |
| 35 | self._check_and_process_command(user, msg) | |
| 36 | print self.picks | |
| 37 | print self.voted_users_set | |
| 38 | ||
| 39 | ||
| 40 | class VoteBotFactory(protocol.ClientFactory): | |
| 41 | protocol = VoteBot | |
| 42 | ||
| 43 | def __init__(self, channel, nickname='VoteBot'): | |
| 44 | self.channel = channel | |
| 45 | self.nickname = nickname | |
| 46 | ||
| 47 | def clientConnectionLost(self, connector, reason): | |
| 48 | print "Lost connection ({}), reconnecting.".format(reason)
| |
| 49 | connector.connect() | |
| 50 | ||
| 51 | def clientConnectionFailed(self, connector, reason): | |
| 52 | print "Could not connect: []".format(reason) | |
| 53 | ||
| 54 | if __name__ == "__main__": | |
| 55 | channel = sys.argv[1] | |
| 56 | - | reactor.connectTCP('irc.p2p-network.net', 6666, VoteBotFactory(channel))
|
| 56 | + | reactor.connectTCP('irc.network.net', 6666, VoteBotFactory(channel))
|
| 57 | reactor.run() |