· 7 years ago · Feb 05, 2018, 09:38 PM
1import sys
2import irc.bot
3import requests
4import time
5from random import randint
6from string import rstrip
7
8
9class TwitchBot(irc.bot.SingleServerIRCBot):
10 def __init__(self, username, client_id, token, channel):
11 self.client_id = client_id
12 self.token = token
13 self.channel = '#' + channel
14
15 # Get the channel id, we will need this for v5 API calls
16 url = 'https://api.twitch.tv/kraken/users?login=' + channel
17 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
18 r = requests.get(url, headers=headers).json()
19 self.channel_id = r['users'][0]['_id']
20
21 # Create IRC bot connection
22 server = 'irc.chat.twitch.tv'
23 port = 6667
24 print 'Connecting to ' + server + ' on port ' + str(port) + '...'
25 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
26
27
28 def on_welcome(self, c, e):
29 # You must request specific capabilities before you can use them
30 c.cap('REQ', ':twitch.tv/membership')
31 c.cap('REQ', ':twitch.tv/tags')
32 c.cap('REQ', ':twitch.tv/commands')
33 c.join(self.channel)
34
35 global mode
36 global cp_timer
37 while 1:
38 print "1. Default mode that copy pastes 300+ character messages & responds to TriHard and cmonBruh"
39 print "2. Copypasta war - pastes a random copypasta every set time."
40 #print "3. Mode 2 and 3 combined."
41 mode = raw_input("Choose mode: ")
42 if not mode:
43 mode = "1"
44 print 'Mode 1 choosen.'
45 break
46 elif mode == '2' or mode == "1": # or mode == '3':
47 print "Mode " + mode + ' choosen.'
48 break
49 else:
50 print "Choose mode 1, 2!"
51
52 if mode == "2": # or mode == "3":
53 while 1:
54 cp_timer = raw_input("Enter at what interval (in seconds) the bot should copy paste: ")
55 if cp_timer.isdigit():
56 print 'The bot will post random copypasta every: ' + cp_timer + ' seconds.'
57 cp_timer = int(cp_timer)
58 self.copypasta_war(e)
59 break
60 else:
61 print 'It has to be a number! Try again!'
62
63 print '-------------'
64
65 print 'Joining ' + self.channel
66 print '-------------'
67 c.privmsg(self.channel, 'Xike71_bot v1.0.1 is loading...')
68 time.sleep(2)
69 c.privmsg(self.channel, 'Loading Matrix...')
70 time.sleep(3)
71 c.privmsg(self.channel, 'Matrix has loaded successfully Kreygasm')
72 time.sleep(2)
73 c.privmsg(self.channel, 'Xike71_bot is fully operational and ready to spam TriHard \'s')
74
75
76 def copypasta_war(self, e):
77 c = self.connection
78 copypasta_file = open('copypasta.dat', 'r')
79 copypasta_list = copypasta_file.readlines()
80 copypasta_file.close
81 n_of_lines = len(copypasta_list) - 1
82 while 1:
83 time.sleep(cp_timer)
84 r_cp = randint(0, n_of_lines)
85 list_element = copypasta_list[r_cp]
86 list_element = list_element.rstrip()
87 print list_element
88 c.privmsg(self.channel, list_element)
89 self.on_pubmsg(c, e)
90
91
92
93
94 def on_pubmsg(self, c, e):
95 cmd = e.arguments[0]
96 sep = '!'
97 rest = e.source.split(sep, 1)[0]
98 print rest + ": " + cmd
99 #print mode
100 if mode != "2":
101 self.do_command(e, cmd)
102
103
104
105 def do_command(self, e, cmd):
106 c = self.connection
107 if cmd == "@Xike71_bot TriHard" or cmd == "@Xike71 TriHard":
108 sep = '!'
109 rest = e.source.split(sep, 1)[0]
110 print ('@' + rest + ' TriHard 7')
111 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
112
113 elif cmd == "@Xike71_bot TriHard 7" or cmd == "@xike71_bot TriHard 7":
114 sep = '!'
115 rest = e.source.split(sep, 1)[0]
116 print ('@' + rest + ' TriHard 7')
117 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
118
119 elif (len(cmd) > 215): #props to Bullseiify for this
120 self.connection.privmsg(self.channel, "Copypasta bot reporting for duty TriHard 7")
121 time.sleep(1)
122 print (cmd)
123 self.connection.privmsg(self.channel, cmd)
124 #copypasta_file_save = open('copypasta.dat', 'a')
125 #copypasta_file_save.write(cmd)
126 #copypasta_file_save.close()
127
128 elif cmd == "@Xike71_bot TriHard /" or cmd == "@xike71_bot TriHard /":
129 sep = '!'
130 rest = e.source.split(sep, 1)[0]
131 print ('@' + rest + ' TriHard /')
132 c.privmsg(self.channel, '@' + rest + ' TriHard /')
133
134 elif cmd == "@Xike71 TriHard 7" or cmd == "@xike71 TriHard 7":
135 sep = '!'
136 rest = e.source.split(sep, 1)[0]
137 print ('@' + rest + ' TriHard 7')
138 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
139
140 elif cmd == "@xike71 TriHard /" or cmd == "@Xike71 TriHard /":
141 sep = '!'
142 rest = e.source.split(sep, 1)[0]
143 print ('@' + rest + ' TriHard /')
144 c.privmsg(self.channel, '@' + rest + ' TriHard /')
145
146 elif "TriHard /" in cmd:
147 sep = '!'
148 rest = e.source.split(sep, 1)[0]
149 print ('@' + rest + ' TriHard /')
150 c.privmsg(self.channel, '@' + rest + ' TriHard /')
151
152 elif "TriHard 7" in cmd:
153 sep = '!'
154 rest = e.source.split(sep, 1)[0]
155 print ('@' + rest + ' TriHard 7')
156 c.privmsg(self.channel, '@' + rest + ' TriHard 7')
157
158 elif "TriHard" in cmd:
159 print (' TriHard')
160 c.privmsg(self.channel, ' TriHard ')
161
162
163 elif "black" in cmd or "Black" in cmd:
164 sep = '!'
165 rest = e.source.split(sep, 1)[0]
166 print ('@' + rest + ' chu mean black cmonBruh')
167 c.privmsg(self.channel, '@' + rest + ' chu mean black cmonBruh')
168
169 elif "ResidentSleeper" in cmd:
170 print ('ResidentSleeper')
171 c.privmsg(self.channel, 'ResidentSleeper')
172
173 elif "mojanga" in cmd or "Mojanga" in cmd:
174 print ('TriHard M TriHard O TriHard J TriHard A TriHard N TriHard G TriHard A TriHard ')
175 c.privmsg(self.channel, 'TriHard M TriHard O TriHard J TriHard A TriHard N TriHard G TriHard A TriHard ')
176
177 elif "cmonBruh" in cmd:
178 print ('cmonBruh')
179 c.privmsg(self.channel, " cmonBruh ")
180
181 elif " Golden Kappa" in cmd:
182 print ('Golden Kappa! Pogchamp')
183 c.privmsg(self.channel, " Golden Kappa! PogChamp ")
184
185 elif cmd=="LUL":
186 print ('LUL')
187 c.privmsg(self.channel, " LUL ")
188
189
190 elif cmd == "!asscrack":
191 sep = '!'
192 rest = e.source.split(sep, 1)[0]
193 c.privmsg(self.channel, "@" + rest + " " + "http://i.imgur.com/dYL7mm0.gif & https://i.imgur.com/01Dc8DY.gif LUL ")
194 print ("@" + rest + " " + "http://i.imgur.com/dYL7mm0.gif & https://i.imgur.com/01Dc8DY.gif LUL ")
195
196 elif cmd == "!cmds":
197 print ("Available commands: !asscrack !yourownchatbot")
198 self.connection.privmsg(self.channel, "Available commands: !asscrack !yourownchatbot")
199
200 elif cmd == "!yourownchatbot":
201 print ("Contact Xike71 through Twitch message.")
202 self.connection.privmsg(self.channel, "Contact Xike71 through Twitch message.")
203
204
205 #elif "!asscrack" in cmd:
206 #c.privmsg(self.channel, " ")
207
208 # The command was not recognized
209 #else:
210 #c.privmsg(self.channel, "Did not understand command: " + cmd)
211
212def main():
213 #if len(sys.argv) != 5:
214 #print("Usage: twitchbot <username> <client id> <token> <channel>")
215 #sys.exit(1)
216
217 #username = sys.argv[1]
218 #client_id = sys.argv[2]
219 #token = sys.argv[3]
220 #channel = sys.argv[4]
221
222 #bot = TwitchBot(username, client_id, token, channel)
223 #bot.start()
224
225 username = "#"
226 client_id = "#"
227 token = "#"
228 channel = sys.argv[1]
229
230 try:
231 bot = TwitchBot(username, client_id, token, channel)
232 bot.start()
233 except KeyboardInterrupt:
234 print("")
235 quit()
236
237if __name__ == "__main__":
238 main()