[tell] Add more debugging

master
sfan5 2015-07-02 10:51:00 +02:00
parent c4904e4017
commit b39ebb013b
2 changed files with 17 additions and 8 deletions

4
log.py
View File

@ -17,12 +17,12 @@ loglevels = {
actionchannel = "##minetestbot" actionchannel = "##minetestbot"
actionhighlight = "sfan5" actionhighlight = "sfan5"
def log(level, text, phenny): def log(level, text, phenny=None):
level = level.upper() level = level.upper()
f = open("bot.log", "ab") f = open("bot.log", "ab")
f.write(bytes(time.strftime("%F %H:%M:%S %z") + "\t" + loglevels[level] + "\t" + text + "\n", 'utf-8', 'ignore')) f.write(bytes(time.strftime("%F %H:%M:%S %z") + "\t" + loglevels[level] + "\t" + text + "\n", 'utf-8', 'ignore'))
f.close() f.close()
if level == 'ACTION': if level == 'ACTION' and phenny is not None:
phenny.write(['PRIVMSG', actionchannel], actionhighlight + ": " + text) phenny.write(['PRIVMSG', actionchannel], actionhighlight + ": " + text)
def fmt_user(input): def fmt_user(input):

21
tell.py
View File

@ -7,6 +7,7 @@ Copyright 2013, sfan5
import random import random
import sqlite3 import sqlite3
import time import time
import calendar
import hashlib import hashlib
tell_list = [] tell_list = []
@ -18,21 +19,26 @@ def tell_diskwr():
c = db.cursor() c = db.cursor()
for tr in tell_pending: for tr in tell_pending:
if tr[0] == "del": if tr[0] == "del":
c.execute("DELETE FROM tell WHERE id = ?", (tr[1], )) r = c.execute("DELETE FROM tell WHERE id = ?", (tr[1], )).rowcount
if r != 1:
log.log("warning", "[tell] could not remove entry id %d from db?!?" % (tr[1], ))
else:
log.log("event", "[tell] removed entry id %d from db" % (tr[1], ))
elif tr[0] == "add": elif tr[0] == "add":
c.execute("INSERT INTO tell (nick, tellee, msg, time) VALUES (?,?,?,?)", tr[1]) c.execute("INSERT INTO tell (nick, tellee, msg, time) VALUES (?,?,?,?)", tr[1])
tell_list.append((c.lastrowid, ) + tr[1]) # We actually insert the entry into the list here tell_list.append((c.lastrowid, ) + tr[1]) # We actually insert the entry into the list here
log.log("event", "[tell] added entry %r to db, id=%d" % (tr[1], c.lastrowid))
else: else:
print("[Tell] Internal error: Unknown action type '%s'" % tr[0]) log.log("warning", "[tell] unknown action type %s" % (tr[0], ))
c.close() c.close()
db.commit() db.commit()
db.close() db.close()
tell_pending = [] tell_pending = []
def api_tell(teller, tellee, text): def api_tell(teller, tellee, text):
d = (teller, tellee, text, int(time.mktime(time.gmtime()))) d = (teller, tellee, text, int(calendar.timegm(time.gmtime())))
tell_pending.append(("add", d)) tell_pending.append(("add", d))
# We do not insert the entry into tell_list yet because we don't know the id it will have # We do not insert the entry into tell_list yet because we don't know which id it will have
tell_diskwr() # Write the change to disk tell_diskwr() # Write the change to disk
class SomeObject(object): class SomeObject(object):
@ -56,7 +62,7 @@ def tell(phenny, input):
text = " ".join(arg.split(" ")[1:]) text = " ".join(arg.split(" ")[1:])
if target.lower() == teller.lower(): if target.lower() == teller.lower():
return phenny.say("You can tell that to yourself") return phenny.say("You can tell that to yourself")
elif target.lower() == phenny.nick.lower(): if target.lower() == phenny.nick.lower():
return phenny.say("I'm not dumb, you know?") return phenny.say("I'm not dumb, you know?")
elif target[-1] == ":": elif target[-1] == ":":
return phenny.reply("Do not put an : at the end of nickname") return phenny.reply("Do not put an : at the end of nickname")
@ -81,7 +87,10 @@ def checktell(phenny, input):
time.gmtime(e[4])), time.gmtime(e[4])),
e[1], e[1],
e[3])) e[3]))
tell_list.remove(e) try:
tell_list.remove(e)
except:
log("warning", "[tell] could not remove entry %r from list?!?" % (e, ), phenny)
tell_pending.append(("del", e[0])) tell_pending.append(("del", e[0]))
tell_diskwr() # Write the change to disk tell_diskwr() # Write the change to disk
break break