summaryrefslogtreecommitdiffstats
path: root/irc-bot2.py
blob: 57c5ad3432b427d282fed7029d4eed02907c348c (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import socket
import ssl
import random
from yt_dlp import YoutubeDL

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)
            case "youtube": youtube(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"
    if "http://youtube.com" in text_str or "https://youtube.com" in text_str:
        print("DEBUG, YOUTUBE")
        return "youtube"


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 youtube(irc, text_str, IrcDetails):
    split_str_foo = text_str.split('https://')
    vid_url_www = split_str_foo[1].strip()
    vid_url = ''.join(("https://", vid_url_www))
    with YoutubeDL() as ydl:
        info_dict = ydl.extract_info(vid_url, download=False)
        video_title = info_dict.get('title', None)
        channel_name = info_dict.get('channel', None)
        irc.send((bytes("PRIVMSG " + IrcDetails.channel + " :" + channel_name + ": " + video_title + "\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()