Better event system, better structure

This commit is contained in:
wr34k 2018-07-03 11:15:38 +02:00
parent 6f8b1ac7e9
commit 1bcccfc1d7
10 changed files with 67 additions and 42 deletions

View File

@ -3,7 +3,7 @@
from fighter import Fighter from fighter import Fighter
import random, json import random, json
CONFIG_FILE = "./config.json" CONFIG_FILE = "assets/config.json"
class Fight(object): class Fight(object):
def __init__(self, IRC): def __init__(self, IRC):

View File

@ -2,10 +2,15 @@
import sys, socket, ssl, time, os, re import sys, socket, ssl, time, os, re
sys.dont_write_bytecode = True
os.chdir(sys.path[0] or ".")
sys.path += ("..", "../IRCUFC")
import ircEvents
from log import Colors, Log from log import Colors, Log
from mircformat import MIRCFormat from mircformat import MIRCFormat
from ircReload import recompile from ircReload import recompile
from ircCommands import IrcCommands from ircCommands import IrcCommands
@ -22,10 +27,9 @@ optkey= "!"
timeout=0.4 timeout=0.4
DEBUG = True DEBUG = True
sys.dont_write_bytecode = True
class Irc(object): class IrcBot(object):
def __init__(self): def __init__(self):
self.sock = None self.sock = None
@ -91,41 +95,14 @@ class Irc(object):
self.log.info("<< {}".format(' '.join(line))) self.log.info("<< {}".format(' '.join(line)))
if line[0][1:] == 'ING': if line[0][1:] == 'ING':
self.raw("PONG {}".format(line[1])) ircEvents.eventPING(self, line)
elif line[1] == '001': # connected else:
self.join() try:
getattr(ircEvents, "event{}".format(line[1].upper()))(self, line)
elif line[1] == 'JOIN': # Someone joined a channel except:
self.log.info("{} JOIN to {}".format(line[0], line[2]))
pass pass
elif line[1] == 'PART': # Someone emopart a channel
self.log.info("{} PART from {}".format(line[0], line[2]))
pass
elif line[1] == '433': # Nick already in use
self.nick += "_"
self.updateNick()
elif line[1] == 'KICK': #Got kicked lmao
if line[0][1:].split("@")[0].split("!") == self.nick:
self.log.warn("Got kicked from {} !".format(line[2]))
chan = line[2]
if chan == self.channel:
self.join()
elif line[1] == 'INVITE':
self.log.info("{} invited the bot to {}".format(line[0][1:].split("!")[0], line[3][1:]))
self.join(line[3][1:])
elif line[1] == 'PRIVMSG':
nick,user = line[0][1:].split("@")[0].split("!")
user = user[1:] if user[0] == '~' else user
host = line[0].split("@")[1]
self.handle_msg(line[2], self.isAdmin(line[0][1:]), nick, user, host, ' '.join(line[3:])[1:])
except (UnicodeDecodeError, UnicodeEncodeError): except (UnicodeDecodeError, UnicodeEncodeError):
pass pass
@ -152,7 +129,7 @@ class Irc(object):
def isAdmin(self, ident): def isAdmin(self, ident):
ret = False ret = False
for line in [line.strip() for line in open('admins', 'r').readlines() if line]: for line in [line.strip() for line in open('assets/admins', 'r').readlines() if line]:
if re.compile(line.replace('*', '.*')).search(ident): if re.compile(line.replace('*', '.*')).search(ident):
ret = True ret = True
return ret return ret
@ -201,7 +178,3 @@ class Irc(object):
def action(self, chan, msg): def action(self, chan, msg):
self.privmsg(chan, "\x01ACTION {}\x01".format(msg)) self.privmsg(chan, "\x01ACTION {}\x01".format(msg))
if __name__=='__main__':
Irc().run()

41
irc/ircEvents.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
import time
def eventPING(IRC, line):
IRC.raw("PONG {}".format(line[1]))
def event001(IRC, line):
IRC.join()
def eventJOIN(IRC, line):
IRC.log.info("{} JOIN to {}".format(line[0], line[2]))
def eventPART(IRC, line):
IRC.log.info("{} PART from {}".format(line[0], line[2]))
def event433(IRC, line):
IRC.nick += "_"
IRC.updateNick()
def eventKICK(IRC, line):
if line[3] == IRC.nick:
IRC.log.warn("Got kicked from {} !".format(line[2]))
chan = line[2]
if chan == IRC.channel:
IRC.join()
def eventQUIT(IRC, line):
if line[0][1:].split("@")[0].split("!")[0] == IRC.nick:
IRC.log.warn("quit ! Reconnecting in 15 seconds..")
time.sleep(15)
IRC.run()
def eventINVITE(IRC, line):
IRC.log.info("{} invited the bot to {}".format(line[0][1:].split("!")[0], line[3][1:]))
def eventPRIVMSG(IRC, line):
nick,user = line[0][1:].split("@")[0].split("!")
user = user[1:] if user[0] == '~' else user
host = line[0].split("@")[1]
IRC.handle_msg(line[2], IRC.isAdmin(line[0][1:]), nick, user, host, ' '.join(line[3:])[1:])

11
main.py Normal file
View File

@ -0,0 +1,11 @@
import os, sys
sys.dont_write_bytecode = True
os.chdir(sys.path[0] or ".")
sys.path += ("irc", "IRCUFC")
if __name__ == '__main__':
import irc
irc.IrcBot().run()