[tell] Rework tell API and finally make it work

master
sfan5 2016-03-19 11:08:36 +01:00
parent 0405c88e18
commit 65fa622ead
1 changed files with 39 additions and 28 deletions

51
tell.py
View File

@ -35,20 +35,34 @@ def tell_diskwr():
db.close() db.close()
tell_pending = [] tell_pending = []
def api_tell(teller, tellee, text): class TellApi:
# Tell <tellee> <text> (from <teller>)
@staticmethod
def tell(teller, tellee, text):
d = (teller, tellee, text, int(calendar.timegm(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 which 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 change to disk tell_diskwr() # Write change to disk
# Get list of not yet fulfilled "tells"
class SomeObject(object): # Returns [(tell_id, teller, tellee, text, unixtime), ...]
pass @staticmethod
def list():
tell_api = SomeObject() return tell_list
tell_api.tell = api_tell # Remove tell_id from database
@staticmethod
def remove(tell_id, internal=False):
tell_pending.append(("del", tell_id))
try:
# Find list index of the tell entry with id == tell_id
idx = next(filter(lambda x: x != -1, (i if e[0] == tell_id else -1 for i, e in enumerate(tell_list))))
del tell_list[idx]
except StopIteration:
log.log("warning", "[tell] could not remove entry id %d from list?!?" % (tell_id, ), phenny)
if not internal:
tell_diskwr() # Write change to disk
_export = { _export = {
'tell': tell_api, 'tell': TellApi,
} }
# Can't be named "tell" because that would interfere with the tell api # Can't be named "tell" because that would interfere with the tell api
@ -68,7 +82,7 @@ def tell_cmd(phenny, input):
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")
api_tell(teller, target, text) TellApi.tell(teller, target, text)
response = "I'll pass that on when %s is around" % target response = "I'll pass that on when %s is around" % target
rand = random.random() rand = random.random()
@ -80,22 +94,19 @@ def tell_cmd(phenny, input):
tell_cmd.commands = ["tell"] tell_cmd.commands = ["tell"]
def checktell(phenny, input): def checktell(phenny, input):
write = False r = []
for e in tell_list: for e in tell_list:
if e[2].lower() == input.nick.lower(): if e[2].lower() != input.nick.lower():
continue
phenny.say("%s: %s <%s> %s" % ( phenny.say("%s: %s <%s> %s" % (
input.nick, input.nick,
time.strftime('%m-%d %H:%M UTC', time.strftime('%m-%d %H:%M UTC', time.gmtime(e[4])),
time.gmtime(e[4])),
e[1], e[1],
e[3])) e[3]))
try: r.append(e[0])
tell_list.remove(e) if len(r) > 0:
except: for tell_id in r:
log("warning", "[tell] could not remove entry %r from list?!?" % (e, ), phenny) TellApi.remove(tell_id, internal=True)
tell_pending.append(("del", e[0]))
write = True
if write:
tell_diskwr() # Write changes to disk tell_diskwr() # Write changes to disk
def note(phenny, input): def note(phenny, input):