util/bot.py

41 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python3
import socket, asyncio, time, re, ssl, ircstates, importlib
from config import *
import shared
Parse = importlib.import_module('parse')
def _send(msg: str, log=True):
shared.sock.send(f"{msg}\n".encode('utf-8'))
if log == True: print(f"> {msg}")
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)
shared.sock.connect((HOST, PORT))
_send(f"NICK {NICK}")
_send(f"USER {NICK} 0 * :{REALNAME}")
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
except Exception as e:
_send(f"PRIVMSG {line.source.split('!')[0]} :Error reloading: Exception({e})")
Parse = oldprs
Parse.Parse(_send=_send, srv=srv, line=line)
except KeyboardInterrupt: _send("QUIT :^C")