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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import socket
import ssl
import random
import time
from yt_dlp import YoutubeDL
# TODO eradicate global variables
# TODO setup CertFP/SASL Auth
reconnect = 0
text_str = 0
hostname = "irc.libera.chat"
channel = "##tech-tangents"
botnick = "notcqst"
context = ssl.create_default_context()
user_str = ("USER " + botnick + " " + botnick + " " + botnick + " :" + botnick + "\n")
nick_str = ("NICK " + botnick + "\n")
channel_string = ("JOIN " + channel + "\n")
send_to_channel_str = ("PRIVMSG " + channel + " :Hello " + "\r\n")
def check_love(irc):
if "I <3 you notcqst" in text_str:
split_str = text_str.split('!')
irc.send((bytes("PRIVMSG " + channel + " :" + "I love you too, " + split_str[0] + "\r\n", 'utf-8')))
def d6_roll(irc):
roll = random.randint(1, 6)
irc.send((bytes("PRIVMSG " + channel + " :" + str(roll) + "\r\n", 'utf-8')))
def d20_roll(irc):
roll = random.randint(1, 20)
irc.send((bytes("PRIVMSG " + channel + " :" + str(roll) + "\r\n", 'utf-8')))
def coin_flip(irc):
roll = random.randint(0, 1)
if (roll):
irc.send((bytes("PRIVMSG " + channel + " :" + "heads" + "\r\n", 'utf-8')))
else:
irc.send((bytes("PRIVMSG " + channel + " :" + "tails" + "\r\n", 'utf-8')))
def ping(irc):
if "PING" in text_str:
split_str = text_str.split(" ")
print(split_str)
print(split_str)
print(split_str)
print(split_str)
print(split_str)
print("PONG\n")
irc.send((bytes("PONG :" + split_str[1] + "\r\n", 'utf-8')))
def check_youtube(irc):
if "https://youtube.com/" in text_str or "https://www.youtube.com/" in text_str:
split_str = text_str.split('https://')
vid_url_www = split_str[1].strip()
vid_url = ''.join(("https://", vid_url_www))
with YoutubeDL() as ydl:
try:
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 " + channel + " :" + channel_name + ": " + video_title + "\r\n", 'utf-8')))
return 0
except:
print("noop")
return 1
def command_choose(split_spaces, irc):
choices = split_spaces[2:]
decision = random.choice(choices)
irc.send((bytes("PRIVMSG " + channel + " :" + decision + "\r\n", 'utf-8')))
def command_hello(irc):
irc.send((bytes("PRIVMSG " + channel + " :" + "Hello, " + "\r\n", 'utf-8')))
def unknown_command(irc):
irc.send((bytes("PRIVMSG " + channel + " :" + "Sorry I don't understand." +
"\r\n", 'utf-8')))
def command_flag(irc):
if ":n!" in text_str:
split_text = text_str.split(':n!')
split_spaces = split_text[1].split(' ')
command = split_spaces[1].strip()
match command:
case "choose": command_choose(split_spaces, irc)
case "hello": command_hello(irc)
case "d6": d6_roll(irc)
case "d20": d20_roll(irc)
case "flip": coin_flip(irc)
case "h": irc.send((bytes("PRIVMSG " + channel + " :" + "choose hello d6 d20 flip h" + "\r\n", 'utf-8')))
case _: unknown_command(irc)
def parse_cmd(irc):
ping(irc)
command_flag(irc)
check_youtube(irc)
check_love(irc)
def connect_checked():
if reconnect >= 3:
exit(1)
else:
# irc.connect((hostname, 6697))
with socket.create_connection((hostname, 6697)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as irc:
irc.send(bytes(user_str, 'utf-8'))
irc.send(bytes(nick_str, 'utf-8'))
irc.send(bytes(channel_string, 'utf-8'))
irc.send(bytes(send_to_channel_str, 'utf-8'))
connect_loop(irc)
def connect_loop(irc):
while 1:
text = irc.recv(2040)
if not text:
break
print(text)
global text_str
text_str = text.decode("utf-8")
parse_cmd(irc)
irc.close()
global reconnect
reconnect += 1
time.sleep(10)
connect_checked()
print("connecting to: " + hostname)
connect_checked()
|