· 7 years ago · Jan 28, 2018, 10:16 PM
1import sys
2import irc.bot
3import requests
4import time
5from random import randint
6
7
8class TwitchBot(irc.bot.SingleServerIRCBot):
9 def __init__(self, username, client_id, token, channel):
10 self.client_id = client_id
11 self.token = token
12 self.channel = '#' + channel
13
14 # Get the channel id, we will need this for v5 API calls
15 url = 'https://api.twitch.tv/kraken/users?login=' + channel
16 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
17 r = requests.get(url, headers=headers).json()
18 self.channel_id = r['users'][0]['_id']
19
20 # Create IRC bot connection
21 server = 'irc.chat.twitch.tv'
22 port = 6667
23 print 'Connecting to ' + server + ' on port ' + str(port) + '...'
24 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
25
26
27 def on_welcome(self, c, e):
28 # You must request specific capabilities before you can use them
29 c.cap('REQ', ':twitch.tv/membership')
30 c.cap('REQ', ':twitch.tv/tags')
31 c.cap('REQ', ':twitch.tv/commands')
32 c.join(self.channel)
33
34 global mode
35 global cp_timer
36 while 1:
37 print "1. Default mode that copy pastes 300+ character messages & responds to TriHard and cmonBruh"
38 print "2. Copypasta war - pastes a random copypasta every set time."
39 print "3. Mode 2 and 3 combined."
40 mode = raw_input("Choose mode: ")
41 if not mode:
42 mode = "1"
43 break
44 elif mode == '2' or mode == '3':
45 print "Mode " + mode + ' choosen.'
46 break
47 else:
48 print "Choose mode 1, 2 or 3!"
49
50 if mode == "2" or mode == "3":
51 while 1:
52 cp_timer = raw_input("Enter at what interval (in seconds) the bot should copy paste: ")
53 if cp_timer.isdigit():
54 print 'The bot will post random copypasta every: ' + cp_timer + ' seconds.'
55 cp_timer = int(cp_timer)
56 self.copypasta_war(e)
57 break
58 else:
59 print 'It has to be a number! Try again!'
60
61
62 print '-------------'
63
64 print 'Joining ' + self.channel
65 print '-------------'
66
67
68
69
70
71 def on_pubmsg(self, c, e):
72 cmd = e.arguments[0]
73 sep = '!'
74 rest = e.source.split(sep, 1)[0]
75 print rest + ": " + cmd
76 print mode
77
78 if mode != "2":
79 self.do_command(e, cmd)
80 #return
81
82
83 def copypasta_war(self, e):
84 c = self.connection
85 copypasta_file = open('copypasta.dat', 'r')
86 copypasta_list = copypasta_file.readlines()
87 number_of_copypastas = len(copypasta_list)
88 print copypasta_list
89 while 1:
90 time.sleep(cp_timer)
91 r_cp = randint(0, number_of_copypastas)
92 c.privmsg(self.channel, ''.join(copypasta_list[r_cp]))
93 print copypasta_list[r_cp]
94
95
96
97 def do_command(self, e, cmd):
98 c = self.connection
99
100 if cmd == "@Xike71 TriHard":
101 sep = '!'
102 rest = e.source.split(sep, 1)[0]
103 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
104
105 elif cmd == "@Xike71 TriHard 7":
106 sep = '!'
107 rest = e.source.split(sep, 1)[0]
108 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
109
110 elif cmd == "@xike71 TriHard 7":
111 sep = '!'
112 rest = e.source.split(sep, 1)[0]
113 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
114
115 elif cmd == "@Xike71 TriHard /":
116 sep = '!'
117 rest = e.source.split(sep, 1)[0]
118 c.privmsg(self.channel, '@' + rest + ' TriHard /')
119
120 elif cmd == "@xike71 TriHard /":
121 sep = '!'
122 rest = e.source.split(sep, 1)[0]
123 c.privmsg(self.channel, '@' + rest + ' TriHard /')
124
125 elif cmd == ("cmonBruh"):
126 c.privmsg(self.channel, " cmonBruh ")
127
128 elif cmd == "TriHard":
129 c.privmsg(self.channel, " TriHard ")
130
131 elif (len(cmd) > 300):
132 self.connection.privmsg(self.channel, cmd)
133
134 #elif " cmonBruh" in cmd:
135 #c.privmsg(self.channel, " cmonBruh ")
136
137 # The command was not recognized
138 #else:
139 #c.privmsg(self.channel, "Did not understand command: " + cmd)
140
141def main():
142 #if len(sys.argv) != 5:
143 #print("Usage: twitchbot <username> <client id> <token> <channel>")
144 #sys.exit(1)
145
146 #username = sys.argv[1]
147 #client_id = sys.argv[2]
148 #token = sys.argv[3]
149 #channel = sys.argv[4]
150
151 #bot = TwitchBot(username, client_id, token, channel)
152 #bot.start()
153
154 username = "xike71"
155 client_id = ""
156 token = ""
157 channel = sys.argv[1]
158
159 try:
160 bot = TwitchBot(username, client_id, token, channel)
161 bot.start()
162 except KeyboardInterrupt:
163 print("")
164 quit()
165
166if __name__ == "__main__":
167 main()