summaryrefslogtreecommitdiffstats
path: root/irc-bot2.py
blob: 9e9390bbf2faeec234c70bbd3ff798b9f665ef3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()