Browse Source

better error handling

master
jcao219 14 years ago
parent
commit
2afda11e1b
  1. 1
      var.py
  2. 14
      wolfbot.py
  3. 77
      wolfgame.py

1
var.py

@ -14,6 +14,7 @@ DAY_TIME_LIMIT_CHANGE = 120 # After DAY_TIME_LIMIT_WARN has passed @@ -14,6 +14,7 @@ DAY_TIME_LIMIT_CHANGE = 120 # After DAY_TIME_LIMIT_WARN has passed
START_WITH_DAY = False
KILL_IDLE_TIME = 300
WARN_IDLE_TIME = 180
LOG_FILENAME = ""
BARE_LOG_FILENAME = "barelog.txt"

14
wolfbot.py

@ -30,7 +30,14 @@ def on_privmsg(cli, rawnick, chan, msg): @@ -30,7 +30,14 @@ def on_privmsg(cli, rawnick, chan, msg):
if chan != botconfig.NICK: #not a PM
if "" in wolfgame.COMMANDS.keys():
for fn in wolfgame.COMMANDS[""]:
try:
fn(cli, rawnick, chan, msg)
except Exception as e:
if botconfig.DEBUG_MODE:
raise e
else:
logging.error(traceback.format_exc())
cli.msg(chan, "An error has occurred and has been logged.")
# Now that is always called first.
for x in wolfgame.COMMANDS.keys():
if x and msg.lower().startswith(botconfig.CMD_CHAR+x):
@ -70,7 +77,14 @@ def __unhandled__(cli, prefix, cmd, *args): @@ -70,7 +77,14 @@ def __unhandled__(cli, prefix, cmd, *args):
for i,arg in enumerate(largs):
if isinstance(arg, bytes): largs[i] = arg.decode('ascii')
for fn in wolfgame.HOOKS[cmd]:
try:
fn(cli, prefix, *largs)
except Exception as e:
if botconfig.DEBUG_MODE:
raise e
else:
logging.error(traceback.format_exc())
cli.msg(botconfig.CHANNEL, "An error has occured and has been logged.")
else:
logging.debug('Unhandled command {0}({1})'.format(cmd, [arg.decode('utf_8')
for arg in args

77
wolfgame.py

@ -991,8 +991,6 @@ def on_nick(cli, prefix, nick): @@ -991,8 +991,6 @@ def on_nick(cli, prefix, nick):
if prefix in v:
v.remove(prefix)
v.append(nick)
else:
return
def leave(cli, what, nick, why=""):
@ -1344,8 +1342,14 @@ def shoot(cli, nick, chan, rest): @@ -1344,8 +1342,14 @@ def shoot(cli, nick, chan, rest):
return
pl = var.list_players()
pll = [x.lower() for x in pl]
if victim not in pll:
cli.notice(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
victim = pl[pll.index(victim)]
if victim == nick:
@ -1441,19 +1445,26 @@ def kill(cli, nick, rest): @@ -1441,19 +1445,26 @@ def kill(cli, nick, rest):
return
pl = var.list_players()
pll = [x.lower() for x in pl]
if victim not in pll:
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
victim = pl[pll.index(victim)]
if victim == nick:
if victim == nick.lower():
cli.msg(nick, "Suicide is bad. Don't do it.")
return
if victim in var.ROLES["wolf"]+var.ROLES["werecrow"]:
cli.msg(nick, "You may only kill villagers, not other wolves")
cli.msg(nick, "You may only kill villagers, not other wolves.")
return
var.KILLS[nick] = victim
cli.msg(nick, "You have selected \u0002{0}\u0002 to be killed.".format(victim))
var.LOGGER.logBare(nick, "SELECT", victim)
var.KILLS[nick] = pl[pll.index(victim)]
cli.msg(nick, "You have selected \u0002{0}\u0002 to be killed.".format(pl[pll.index(victim)]))
var.LOGGER.logBare(nick, "SELECT", pl[pll.index(victim)])
var.ACTED_WOLVES.add(nick)
chk_nightdone(cli)
@ -1483,7 +1494,13 @@ def guard(cli, nick, rest): @@ -1483,7 +1494,13 @@ def guard(cli, nick, rest):
return
pl = var.list_players()
pll = [x.lower() for x in pl]
if victim not in pll:
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
if victim == nick.lower():
@ -1517,7 +1534,13 @@ def observe(cli, nick, rest): @@ -1517,7 +1534,13 @@ def observe(cli, nick, rest):
return
pl = var.list_players()
pll = [x.lower() for x in pl]
if victim not in pll:
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
victim = pl[pll.index(victim)]
@ -1561,7 +1584,13 @@ def investigate(cli, nick, rest): @@ -1561,7 +1584,13 @@ def investigate(cli, nick, rest):
return
pl = var.list_players()
pll = [x.lower() for x in pl]
if victim not in pll:
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
victim = pl[pll.index(victim)]
@ -1601,15 +1630,21 @@ def hvisit(cli, nick, rest): @@ -1601,15 +1630,21 @@ def hvisit(cli, nick, rest):
if not victim:
cli.msg(nick, "Not enough parameters")
return
pl = [x.lower() for x in var.list_players()]
if victim not in pl:
pll = [x.lower() for x in var.list_players()]
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
if nick.lower() == victim: # Staying home
var.HVISITED[nick] = None
cli.msg(nick, "You have chosen to stay home for the night.")
else:
var.HVISITED[nick] = var.list_players()[pl.index(victim)]
var.HVISITED[nick] = var.list_players()[pll.index(victim)]
cli.msg(nick, ("You are spending the night with \u0002{0}\u0002. "+
"Have a good time!").format(var.HVISITED[nick]))
cli.msg(var.HVISITED[nick], ("You are spending the night with \u0002{0}"+
@ -1647,7 +1682,13 @@ def see(cli, nick, rest): @@ -1647,7 +1682,13 @@ def see(cli, nick, rest):
if not victim:
cli.msg(nick, "Not enough parameters")
return
if victim not in pll:
for player in pll:
if victim == player:
break
if player.startswith(victim):
victim = player
break
else:
cli.msg(nick,"\u0002{0}\u0002 is currently not playing.".format(victim))
return
victim = pl[pll.index(victim)]

Loading…
Cancel
Save