util/bot.py

41 lines
1.4 KiB
Python
Raw Normal View History

2021-12-19 09:05:42 +00:00
#!/usr/bin/python3
2022-01-20 17:55:16 +00:00
import socket, asyncio, time, re, ssl, ircstates, importlib
2021-12-19 09:05:42 +00:00
from config import *
2022-01-20 17:55:16 +00:00
import shared
2021-12-19 09:05:42 +00:00
2022-01-20 17:55:16 +00:00
Parse = importlib.import_module('parse')
2022-01-09 09:16:29 +00:00
def _send(msg: str, log=True):
2022-01-20 17:55:16 +00:00
shared.sock.send(f"{msg}\n".encode('utf-8'))
2022-01-09 09:16:29 +00:00
if log == True: print(f"> {msg}")
2021-12-19 09:05:42 +00:00
2022-01-20 17:55:16 +00:00
if __name__ == '__main__':
srv = ircstates.Server(NETNAME)
shared.sock = socket.socket()
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
if tls == True:
shared.sock = ctx.wrap_socket(shared.sock)
2022-01-20 17:55:16 +00:00
shared.sock.connect((HOST, PORT))
_send(f"NICK {NICK}")
_send(f"USER {NICK} 0 * :{REALNAME}")
2022-01-20 17:55:16 +00:00
try:
while True:
recv_data = shared.sock.recv(1024)
recv_lines = srv.recv(recv_data)
for line in recv_lines:
srv.parse_tokens(line)
if line.command == 'PRIVMSG':
if line.params[1] == '%reload':
if Parse.is_admin(line.source):
oldprs = Parse
try:
prs = importlib.reload(Parse)
Parse = prs
2022-01-21 16:49:30 +00:00
except Exception as e:
_send(f"PRIVMSG {line.source.split('!')[0]} :Error reloading: Exception({e})")
2022-01-20 17:55:16 +00:00
Parse = oldprs
Parse.Parse(_send=_send, srv=srv, line=line)
except KeyboardInterrupt: _send("QUIT :^C")