From b021bd8ae83c4f578e5450325f0b200f3e6f2795 Mon Sep 17 00:00:00 2001 From: wr34k Date: Fri, 29 Jun 2018 14:16:26 +0200 Subject: [PATCH] Fixed status command + log module --- fight.py | 16 +++++++---- irc.py | 78 +++++++++++++++++++++++++++++--------------------- ircCommands.py | 5 ++-- log.py | 4 ++- 4 files changed, 60 insertions(+), 43 deletions(-) diff --git a/fight.py b/fight.py index a1ea728..22540a9 100644 --- a/fight.py +++ b/fight.py @@ -100,12 +100,16 @@ class Fight(object): def getStatus(self, nick): - for f in self.fighters: - if f.nick == nick: - self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {} | Next action: {}".format(f.nick, f.hp, f.stance, f.nextAction)) - else: - self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {}".format(f.nick, f.hp, f.stance)) - + if self.state == 'inactive': + self.IRC.privmsg(nick, "Standby, waiting for a fight to start...") + elif self.state == 'waiting_fighter': + self.IRC.privmsg(nick, "Waiting for 2nd opponent, type {}fight to register for the next fight!".format(self.IRC.optkey)) + else: + for f in self.fighters: + if f.nick == nick: + self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {} | Next action: {}".format(f.nick, f.hp, f.stance, f.nextAction)) + else: + self.IRC.privmsg(nick, "Status for {} -> Current health: {} | Current stance: {}".format(f.nick, f.hp, f.stance)) def fightOver(self): diff --git a/irc.py b/irc.py index cf75b18..e321882 100644 --- a/irc.py +++ b/irc.py @@ -85,50 +85,62 @@ class Irc(object): def listen(self): while True: - if self.lag: - self.lag=False - data += self.sock.recv(1024).decode('utf-8', 'ignore') - else: - data = self.sock.recv(1024).decode('utf-8', 'ignore') + try: + if self.lag: + self.lag=False + data += self.sock.recv(1024).decode('utf-8', 'ignore') + else: + data = self.sock.recv(1024).decode('utf-8', 'ignore') - for line in [x.split() for x in data.split("\r\n") if len(x.split()) > 1]: - self.log.info("<< {}".format(' '.join(line))) + for line in [x.split() for x in data.split("\r\n") if len(x.split()) > 1]: + self.log.info("<< {}".format(' '.join(line))) - if line[0][1:] == 'ING': - self.raw("PONG {}".format(line[1])) + if line[0][1:] == 'ING': + self.raw("PONG {}".format(line[1])) - elif line[1] == '001': # connected - self.join() + 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] == '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] == '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] == '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: + 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] == '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:]) + 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 + + except KeyboardInterrupt: + self.log.warn("^C, Exiting...") + return + + except Exception as e: + self.log.error("Exception in listen()", e) + pass def join(self): self.log.info("Now joining {} ...".format(self.channel)) diff --git a/ircCommands.py b/ircCommands.py index d68d63f..e3e7910 100644 --- a/ircCommands.py +++ b/ircCommands.py @@ -55,6 +55,8 @@ class IrcCommands(object): self.IRC.raw(" ".join(args)) + if cmd == 'status': + self.fight.getStatus(chan) if self.fight.state in ('inactive', 'waiting_fighter'): if cmd == 'fight': @@ -69,9 +71,6 @@ class IrcCommands(object): else: self.IRC.privmsg(self.IRC.channel, "{}".format(self.IRC.mirc.color("Not here retard your opponent can see your next move!", self.IRC.mirc.colors.LIGHTRED))) - else: - if cmd == 'status': - self.fight.getStatus(chan) self.IRC.flood_flag[chan] = False self.IRC.flood_count[chan] = 0 diff --git a/log.py b/log.py index b1413aa..5f9d89e 100644 --- a/log.py +++ b/log.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import traceback class Colors(object): class Format(object): @@ -60,7 +61,7 @@ class Log(object): self.func = func def construct(self, *args): - return "".join(a for a in args) + return "".join(args) def info(self, msg): if self.debug: @@ -75,3 +76,4 @@ class Log(object): self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", msg ) ) if exception: self.func( self.construct( "[", self.colors.fg.LIGHTRED, "x", self.colors.fg.DEFAULT, "] ", str(exception) ) ) + traceback.print_tb(exception.__traceback__)