diff options
author | cqst <cqst@cqstia.com> | 2025-09-09 12:26:34 -0700 |
---|---|---|
committer | cqst <cqst@cqstia.com> | 2025-09-09 12:26:34 -0700 |
commit | 821d9d73c829d9c56fb8b06bd143a706362ecac1 (patch) | |
tree | 82c2d864997f8e3c86c7b70c213afcfb68a9c686 | |
parent | cff41844ba688d45c31040dd149d7e586ef8a406 (diff) | |
download | irc-bot-821d9d73c829d9c56fb8b06bd143a706362ecac1.tar.gz |
command_crash
-rw-r--r-- | irc-bot2.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/irc-bot2.py b/irc-bot2.py new file mode 100644 index 0000000..9e9390b --- /dev/null +++ b/irc-bot2.py @@ -0,0 +1,74 @@ +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() |