Last active
February 7, 2019 20:27
-
-
Save Mm2PL/992d2cf3a41c8ff84e4aafc454bde1f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
import re | |
# Works on python 3.6 and 3.7 | |
# =================================================================================== | |
# Modify this part | |
# =================================================================================== | |
NICK = "" # The account Name | |
PASS = "" # http://www.twitchapps.com/tmi/ | |
CHAN = "" # Channel name | |
# =================================================================================== | |
HOST = "irc.chat.twitch.tv" | |
PORT = 6667 | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
def send(msg): | |
print('>', msg) | |
s.send(msg) | |
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") | |
RATE = 1/2 | |
STATES = { | |
-1: 'Disconnected', | |
0: 'Connected', | |
1: 'Hello-ed', | |
2: 'Parting', | |
3: 'Quiting', | |
4: 'Quit', | |
-10: 'USER' | |
} | |
state = -1 # Disconnected | |
def sta(new_state): | |
global state | |
print(' ~(C)~ State {} ({}) -> {} ({})'.format(STATES[state], state, STATES[new_state], new_state)) | |
state = new_state | |
def process_msg(msg): | |
parts = msg.split(' ', 3) | |
info = {} | |
# info = {m.split('=')[0]: m.split('=')[1] for m in parts[0][1:].split(';')} | |
# print(msg) | |
try: | |
for m in parts[0][1:].split(';'): | |
m = m.split('=') | |
info[m[0]] = m[1] | |
except (KeyError, IndexError): | |
return '?', parts | |
cmd = parts[2] | |
args = parts[3] | |
# print(cmd) | |
if cmd == 'PRIVMSG': | |
# 'msg', info, channel, message, user, parts | |
return 'msg', info, args[0], args[1], parts[1].split('!')[0][1:], parts | |
return '?', info, cmd, args, parts | |
send(bytes("PASS {}\r\n".format(PASS), 'utf-8')) | |
send(bytes("NICK {}\r\n".format(NICK), 'utf-8')) | |
send(b'CAP REQ :twitch.tv/tags twitch.tv/commands\r\n') | |
sta(-1) | |
time_banned = -1 | |
listen = True | |
last_message = ('msg', {'display-name': 'None'}, '', '', ['']) | |
bots = {'linkus_ban_me': {'resp': last_message}} | |
last_message_time = -1 | |
while True: | |
try: | |
if state != -10: | |
listen = True | |
if state == -10: | |
listen = False | |
try: | |
inp = input('(RAW) >>>') | |
except EOFError: | |
sta(2) | |
inp = '' | |
if inp.lower() == 'l': | |
listen = True | |
inp = '' | |
print('\033[A', end='') | |
if inp != '': | |
send(bytes(inp + '\r\n', 'utf-8')) | |
response = '' | |
if state == 3: | |
send(b'QUIT\r\n') | |
listen = True | |
sta(4) | |
if state == 2: | |
send(b'PART ALL\r\n') | |
listen = True | |
sta(3) | |
if listen: | |
response = s.recv(1024).decode("utf-8") | |
else: | |
print('* No listening') | |
resp = process_msg(response) | |
if resp[0] == 'msg': | |
print('<', resp[-1][1].split('!')[0][1:], '@', resp[-1][-1], '>') | |
else: | |
print('<', response) | |
if state == 3: | |
continue | |
if state == 4: | |
exit() | |
if response == "PING :tmi.twitch.tv\r\n": | |
send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) | |
if state == -1: | |
send(bytes("JOIN #{}\r\n".format(CHAN.lower()), 'utf-8')) | |
send(bytes('CAP REQ :twitch.tv/membership\r\n', 'utf-8')) | |
sta(0) | |
elif state == 0: | |
send(bytes("PRIVMSG #{} :Bot detector started MrDestructoid (pls dont ban me)\r\n".format(CHAN.lower()), 'utf-8')) | |
sta(1) | |
if resp[0] == 'msg': | |
print(repr(resp)) | |
the_message = resp[-1][-1].split(' :', 1)[-1][:-2] | |
lmsg = last_message[-1][-1].split(' :', 1)[-1][:-2] | |
print('\naaaaaa', repr(lmsg), repr(the_message), '\n') | |
if lmsg == the_message and round(last_message_time) == round(time.time()): | |
# resp[-2] name | |
offender = resp[1]['display-name'] | |
if offender in bots: | |
bots[offender]['resp'] += [resp] | |
print('{} might be a bot!!!!\n'.format(offender)) | |
if len(bots[offender]['resp']) >= 2: | |
send(bytes('PRIVMSG #{0} :/ban {1}\r\nPRIVMSG #{0} :Auto-banned {1}.\r\n'.format(CHAN.lower(), offender), 'utf-8')) | |
else: | |
bots[offender] = {'resp': [resp]} | |
last_message = resp | |
last_message_time = time.time() | |
print('\n Sleeping', end='...\n') | |
time.sleep(1 / RATE) | |
print('\033[A Receiving', end='...\n') | |
except KeyboardInterrupt: | |
print('\033[2D', end='') | |
sta(-10 if state != -10 else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment