bot.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/python3
  2. import socket, asyncio, time, re, ssl, ircstates, importlib
  3. from config import *
  4. import shared
  5. Parse = importlib.import_module('parse')
  6. def _send(msg: str, log=True):
  7. shared.sock.send(f"{msg}\n".encode('utf-8'))
  8. if log == True: print(f"> {msg}")
  9. if __name__ == '__main__':
  10. srv = ircstates.Server(NETNAME)
  11. shared.sock = socket.socket()
  12. ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
  13. if tls == True:
  14. shared.sock = ctx.wrap_socket(shared.sock)
  15. shared.sock.connect((HOST, PORT))
  16. _send(f"NICK {NICK}")
  17. _send(f"USER {NICK} 0 * :{REALNAME}")
  18. try:
  19. while True:
  20. recv_data = shared.sock.recv(1024)
  21. recv_lines = srv.recv(recv_data)
  22. for line in recv_lines:
  23. srv.parse_tokens(line)
  24. if line.command == 'PRIVMSG':
  25. if line.params[1] == '%reload':
  26. if Parse.is_admin(line.source):
  27. oldprs = Parse
  28. try:
  29. prs = importlib.reload(Parse)
  30. Parse = prs
  31. except Exception as e:
  32. _send(f"PRIVMSG {line.source.split('!')[0]} :Error reloading: Exception({e})")
  33. Parse = oldprs
  34. Parse.Parse(_send=_send, srv=srv, line=line)
  35. except KeyboardInterrupt: _send("QUIT :^C")