Created
May 13, 2018 18:07
-
-
Save NexInfinite/00d99efc2225ddd9fe10e1b5186c15a5 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 logging | |
import socket | |
class IRC: | |
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def __init__(self, server, port, nick, channel, auth): | |
self.server = server | |
self.port = port | |
self.nick = nick | |
self.channel = channel | |
self.auth = auth | |
def format_msg(self, msg: str): | |
formatted_msg = f'PRIVMSG #{self.channel} :{msg}\r\n' | |
return formatted_msg.encode() | |
def ping(self): | |
self.irc.send(b'PONG :tmi.twitch.tv\r\n') | |
def send(self, msg): | |
if isinstance(msg, bytes): | |
msg = msg.decode('utf-8') | |
formatted_msg = self.format_msg(msg) | |
print(formatted_msg) | |
self.irc.send(self.format_msg(msg)) | |
@staticmethod | |
def get_user_from_msg(user): | |
return user.split('!') | |
def connect(self): | |
password = f'PASS {self.auth}\r\n'.encode() | |
nick = f'NICK {self.nick}\r\n'.encode() | |
channel = f'JOIN #{self.channel}\r\n'.encode() | |
self.irc.connect((self.server, self.port)) | |
self.irc.send(password) | |
self.irc.send(nick) | |
self.irc.send(channel) | |
def diconnect(self): | |
self.irc.send(f'PART #{self.channel}'.encode()) | |
logging.info(f'disconnected from channel {self.channel}') | |
def get_msg(self): | |
response = self.irc.recv(2048).decode().strip('\r\n') | |
print(response) | |
try: | |
user, msg = response.split(':', 2)[1:] | |
return self.get_user_from_msg(user), msg | |
except ValueError: | |
return '', response | |
mods = ['andrewgamer1937', 'comsciprogramming', 'gabelover23', 'gconnor911', 'hexploitation', 'learngamedev', 'malvious_mh', 'nex_infinite', 'nexchatbot', 'nexchatbottesting', 'nezamizero', 'nightbot', 'streamelements', 'sudokid', 'userman2', 'wizebot', 'yojimbozz'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment