From 1bcccfc1d71fe2c724e801e40c70273ac05a5194 Mon Sep 17 00:00:00 2001 From: wr34k Date: Tue, 3 Jul 2018 11:15:38 +0200 Subject: [PATCH] Better event system, better structure --- fight.py => IRCUFC/fight.py | 2 +- fighter.py => IRCUFC/fighter.py | 0 admins => assets/admins | 0 config.json => assets/config.json | 0 irc.py => irc/irc.py | 55 +++++++--------------------- ircCommands.py => irc/ircCommands.py | 0 irc/ircEvents.py | 41 +++++++++++++++++++++ ircReload.py => irc/ircReload.py | 0 mircformat.py => irc/mircformat.py | 0 main.py | 11 ++++++ 10 files changed, 67 insertions(+), 42 deletions(-) rename fight.py => IRCUFC/fight.py (99%) rename fighter.py => IRCUFC/fighter.py (100%) rename admins => assets/admins (100%) rename config.json => assets/config.json (100%) rename irc.py => irc/irc.py (73%) rename ircCommands.py => irc/ircCommands.py (100%) create mode 100644 irc/ircEvents.py rename ircReload.py => irc/ircReload.py (100%) rename mircformat.py => irc/mircformat.py (100%) create mode 100644 main.py diff --git a/fight.py b/IRCUFC/fight.py similarity index 99% rename from fight.py rename to IRCUFC/fight.py index 22540a9..5297d4e 100644 --- a/fight.py +++ b/IRCUFC/fight.py @@ -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): diff --git a/fighter.py b/IRCUFC/fighter.py similarity index 100% rename from fighter.py rename to IRCUFC/fighter.py diff --git a/admins b/assets/admins similarity index 100% rename from admins rename to assets/admins diff --git a/config.json b/assets/config.json similarity index 100% rename from config.json rename to assets/config.json diff --git a/irc.py b/irc/irc.py similarity index 73% rename from irc.py rename to irc/irc.py index e027669..249a5f0 100644 --- a/irc.py +++ b/irc/irc.py @@ -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,40 +95,13 @@ 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])) - 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:]) + else: + try: + getattr(ircEvents, "event{}".format(line[1].upper()))(self, line) + except: + pass 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() diff --git a/ircCommands.py b/irc/ircCommands.py similarity index 100% rename from ircCommands.py rename to irc/ircCommands.py diff --git a/irc/ircEvents.py b/irc/ircEvents.py new file mode 100644 index 0000000..90bcf88 --- /dev/null +++ b/irc/ircEvents.py @@ -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:]) diff --git a/ircReload.py b/irc/ircReload.py similarity index 100% rename from ircReload.py rename to irc/ircReload.py diff --git a/mircformat.py b/irc/mircformat.py similarity index 100% rename from mircformat.py rename to irc/mircformat.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..cf99193 --- /dev/null +++ b/main.py @@ -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()