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
import random, json
CONFIG_FILE = "./config.json"
CONFIG_FILE = "assets/config.json"
class Fight(object):
def __init__(self, IRC):

View File

@ -2,10 +2,15 @@
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 mircformat import MIRCFormat
from ircReload import recompile
from ircCommands import IrcCommands
@ -22,10 +27,9 @@ optkey= "!"
timeout=0.4
DEBUG = True
sys.dont_write_bytecode = True
class Irc(object):
class IrcBot(object):
def __init__(self):
self.sock = None
@ -91,41 +95,14 @@ class Irc(object):
self.log.info("<< {}".format(' '.join(line)))
if line[0][1:] == 'ING':
self.raw("PONG {}".format(line[1]))
ircEvents.eventPING(self, line)
elif line[1] == '001': # connected
self.join()
elif line[1] == 'JOIN': # Someone joined a channel
self.log.info("{} JOIN to {}".format(line[0], line[2]))
else:
try:
getattr(ircEvents, "event{}".format(line[1].upper()))(self, line)
except:
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):
pass
@ -152,7 +129,7 @@ class Irc(object):
def isAdmin(self, ident):
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):
ret = True
return ret
@ -201,7 +178,3 @@ class Irc(object):
def action(self, chan, 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()