import socket import ssl import random def connect(): context = ssl.create_default_context() class IrcDetails: hostname = "irc.libera.chat" channel = "##tech-tangents" botnick = "notnotcqst" user_str = ("USER " + botnick + " " + botnick + " " + botnick + " :" + botnick + "\n") nick_str = ("NICK " + botnick + "\n") channel_string = ("JOIN " + channel + "\n") with socket.create_connection((IrcDetails.hostname, 6697)) as sock: with context.wrap_socket(sock, server_hostname=IrcDetails.hostname) as irc: irc.send(bytes(IrcDetails.user_str, 'utf-8')) irc.send(bytes(IrcDetails.nick_str, 'utf-8')) irc.send(bytes(IrcDetails.channel_string, 'utf-8')) command_loop(irc, IrcDetails) def command_loop(irc, IrcDetails): while 1: text = irc.recv(2040) print(text) text_str = text.decode("utf-8") match parse_text(text_str): case "ping": ping(irc, text_str) case "command": bot_command(irc, text_str, IrcDetails) def parse_text(text_str): if "PING" in text_str: return "ping" if ":n!" in text_str: print("DEBUG, COMMAND") return "command" def ping(irc, text_str): print("DEBUG, PINGOUT") split_str = text_str.split(" ") irc.send((bytes("PONG :" + split_str[1] + "\r\n", 'utf-8'))) def bot_command(irc, text_str, IrcDetails): text_str_clean = text_str.rstrip() text_str_split = text_str_clean.split(" ") print("DEBUG TEXT_STR_SPLIT FOUR" + text_str_split[4]) class BotCommands: def command_crash(): print("DEBUG CRASH!!!!!") # TODO get rid of this raise KeyboardInterrupt def command_choose(text_str_split): print("debug choose") text_str_args_list = text_str_split[5:] text_str_args_hack = ' '.join(text_str_args_list) text_str_args_hack_hack = text_str_args_hack.split('|') choices = text_str_args_hack_hack decision = random.choice(choices) irc.send((bytes("PRIVMSG " + IrcDetails.channel + " :" + decision + "\r\n", 'utf-8'))) match text_str_split[4]: case "crash": BotCommands.command_crash() case "choose": BotCommands.command_choose(text_str_split) #connect()