· 7 years ago · Jan 07, 2018, 06:38 AM
1#All hail RNG_MODS
2
3# USER STRUCTURE
4# user: [twitch user name]
5# stats: {"attack", "defense"}
6# analytics: {"chatlines"}
7
8import sys
9import string
10import socket
11import globals
12import userFunctions
13
14
15
16import arrow
17import requests
18from dateutil.relativedelta import relativedelta
19
20CLIENT_ID = ''
21OAUTH_TOKEN = '' # needs to be from narcissa account and have channel_check_subscription scope
22
23usedTimeouts = ['narcissa_bot']
24victims = ['narcissa_bot']
25
26def sub_data(username):
27 response = requests.get(
28 'https://api.twitch.tv/kraken/channels/narcissawright/subscriptions/{}'.format(username),
29 headers={
30 'Accept': 'application/vnd.twitchtv.v3+json',
31 'Client-Id': CLIENT_ID,
32 'Authorization': 'OAuth {}'.format(OAUTH_TOKEN)})
33 print (response.status_code)
34 if response.status_code == 200:
35 return response.json()
36
37def enable_membership(self):
38 self.sock.deliver('CAP REQ :twitch.tv/membership\r\n')
39
40def data_months_getter(data):
41 print(data['created_at'])
42 print(data['user']['name'])
43 created_at = arrow.get(data['created_at']).datetime
44 now = arrow.get().datetime
45 delta = relativedelta(now, created_at)
46 months = 12*delta.years + delta.months
47 return months
48
49def ircLoop():
50 global usedTimeouts
51 global victims
52
53 HOST = "irc.chat.twitch.tv"
54 PORT = 6667
55 PASSWORD = "oauth:"
56 NICK = "Narcissa_Bot"
57 IDENT = "Narcissa_Bot"
58 REALNAME = "Narcissa_Bot"
59 CHANNEL = "#narcissawright"
60
61 globals.twitchSocket.connect((HOST, PORT))
62 globals.twitchSocket.send(bytes("PASS %s\r\n" % PASSWORD, "UTF-8"))
63 globals.twitchSocket.send(bytes("NICK %s\r\n" % NICK, "UTF-8"))
64 globals.twitchSocket.send(bytes("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME), "UTF-8"))
65
66 globals.twitchSocket.send(bytes("JOIN %s\r\n" % globals.CHANNEL, "UTF-8"));
67 globals.twitchSocket.send(bytes("PRIVMSG %s :Hello World!~ \r\n" % globals.CHANNEL, "UTF-8"))
68
69 chatLines = 0
70 readbuffer = ""
71
72 def getSender(line):
73 sender = ""
74 for char in line[0]:
75 if(char == "!"):
76 break
77 if(char != ":"):
78 sender += char
79 return sender
80
81 def getVictim(message):
82 victim = message.partition(' ')[2] #grab the characters after the first space
83 if not victim: #if an empty string
84 return False #return False
85 victim = victim.lower() #make lowercase
86 if victim[0] == '@': #grab first character, check if it is an @ symbol (used for twitch chat autocomplete)
87 victim = victim[1:] #remove the first character if it is an @ symbol so it returns just the victim
88 return victim
89
90 while 1:
91 try:
92 readbuffer = readbuffer+globals.twitchSocket.recv(1024).decode("UTF-8")
93 temp = str.split(readbuffer, "\n")
94 readbuffer=temp.pop( )
95 except:
96 temp = ""
97
98
99 for line in temp: #for each line recieved,
100 line = str.rstrip(line)
101 line = str.split(line)
102
103 if(line[0] == "PING"): #if its a PING:
104 globals.twitchSocket.send(bytes("PONG %s\r\n" % line[1], "UTF-8"))
105 print("* PING")
106
107 if(line[1] == "353"): #if its a USER LIST:
108 line[5] = line[5].lstrip(":")
109 for i in range(5, len(line)):
110 globals.userlist.append(userFunctions.addMissingUser(line[i]))
111 print("* Populating globals.userlist. Total:", len(globals.userlist))
112
113 if(line[1] == "PRIVMSG"): #if its a MESSAGE:
114 sender = getSender(line)
115 size = len(line)
116 i = 3
117 message = ""
118 while(i < size):
119 message += line[i] + " "
120 i = i + 1
121 message = message.lstrip(":").lower().rstrip() #tidy up the message, remove the :, trailing character, and make lowercase.
122
123 ####################
124 ## ADMIN COMMANDS ##
125 ####################
126
127 if (sender == "narcissawright"): #if narcissawright types it:
128
129 if (message.partition(' ')[0] == "!end"): #if its ENDING THE PROGRAM
130 return
131
132 ###################
133 ## USER COMMANDS ##
134 ###################
135
136 if (message.partition(' ')[0] == "!timeout"): #if its a timeout
137 global usedTimeouts
138 global victims
139 sender = getSender(line)
140 data = sub_data(sender)
141
142 uses = usedTimeouts.count(sender)
143
144 #if sender in usedTimeouts:
145 # globals.twitchSocket.send(bytes("PRIVMSG %s :you've already used your timeout this stream. \r\n" % (globals.CHANNEL), "UTF-8"))
146 #else:
147 if data:
148 months = data_months_getter(data)
149 months += 1
150 totalUses = months // 6
151 if totalUses != 0:
152 if uses >= totalUses:
153 globals.twitchSocket.send(bytes("PRIVMSG %s :you've already used your timeouts this stream. \r\n" % (globals.CHANNEL), "UTF-8"))
154 else:
155 victim = getVictim(message)
156 data2 = sub_data(victim)
157 if not data2:
158 severity = victims.count(victim)
159 uses += 1
160 if severity == 0:
161 globals.twitchSocket.send(bytes("PRIVMSG %s :.timeout %s %s\r\n" % (globals.CHANNEL, victim, 300), "UTF-8")) #timeout for 5min
162 globals.twitchSocket.send(bytes("PRIVMSG %s :%s has been timed out for 5 minutes. (%s/%s) \r\n" % (globals.CHANNEL, victim, uses, totalUses), "UTF-8"))
163 if severity == 1:
164 globals.twitchSocket.send(bytes("PRIVMSG %s :.timeout %s %s\r\n" % (globals.CHANNEL, victim, 1200), "UTF-8")) #timeout for 20min
165 globals.twitchSocket.send(bytes("PRIVMSG %s :%s has been timed out for 20 minutes. (%s/%s) \r\n" % (globals.CHANNEL, victim, uses, totalUses), "UTF-8"))
166 if severity == 2:
167 globals.twitchSocket.send(bytes("PRIVMSG %s :.timeout %s %s\r\n" % (globals.CHANNEL, victim, 5400), "UTF-8")) #timeout for 90min
168 globals.twitchSocket.send(bytes("PRIVMSG %s :%s has been timed out for 90 minutes. (%s/%s) \r\n" % (globals.CHANNEL, victim, uses, totalUses), "UTF-8"))
169 if severity > 2:
170 globals.twitchSocket.send(bytes("PRIVMSG %s :.timeout %s %s\r\n" % (globals.CHANNEL, victim, 43200), "UTF-8")) #timeout for 12hrs
171 globals.twitchSocket.send(bytes("PRIVMSG %s :%s has been timed out for 12 hours. (%s/%s) \r\n" % (globals.CHANNEL, victim, uses, totalUses), "UTF-8"))
172 usedTimeouts.append(sender)
173 victims.append(victim)
174 else:
175 globals.twitchSocket.send(bytes("PRIVMSG %s :subscribers cannot time out subscribers.\r\n" % (globals.CHANNEL), "UTF-8"))
176 else:
177 globals.twitchSocket.send(bytes("PRIVMSG %s :you must be subscribed for 6 months to attain timeout power. (%s/6) \r\n" % (globals.CHANNEL, months), "UTF-8"))
178
179 if (message.partition(' ')[0] == "!sub"): #if its a HI
180 sender = getSender(line)
181 data = sub_data(sender)
182 if data:
183 months = data_months_getter(data)
184 months += 1
185 globals.twitchSocket.send(bytes("PRIVMSG %s :%s subbed for %s month(s). \r\n" % (globals.CHANNEL, sender, months), "UTF-8"))
186 #print('{} months subscribed'.format(12*delta.years + delta.months))
187 else:
188 globals.twitchSocket.send(bytes("PRIVMSG %s :%s hasn't subscribed. \r\n" % (globals.CHANNEL, sender), "UTF-8"))
189 #victim = getVictim(message)
190 ##userFunctions.attackUser(sqlite.getUser(str(sender)), sqlite.getUser(str(victim)))
191
192 if(line[1] == "JOIN"): #if its a JOIN:
193 sender = getSender(line)
194 if(sender != "rng_mods"):
195 globals.userlist.append(userFunctions.addMissingUser(sender))
196 print("*", sender, "joined. Total:", len(globals.userlist))
197
198 if(line[1] == "PART"): #if its a PART:
199 sender = getSender(line)
200 while sender in globals.userlist: globals.userlist.remove(sender)
201 print("*", sender, "left. Total:", len(globals.userlist))
202
203ircLoop()