minetestbot-modules/tell.py

111 lines
2.8 KiB
Python
Raw Normal View History

2013-11-17 10:31:50 -08:00
#!/usr/bin/env python
"""
seen.py - Phenny Seen Module
Copyright 2013, sfan5
"""
import random
from thread import start_new_thread, allocate_lock
import sqlite3
import time
import hashlib
2013-11-17 10:31:50 -08:00
tell_list = []
tell_pending = []
tell_lastdiskwrite = 0
tell_lastlisthash = ''
tell_diskwriteinterval = 60 # seconds
2013-11-17 10:31:50 -08:00
def tell(phenny, input):
for x in phenny.bot.commands["high"].values():
if x[0].__name__ == "aa_hook":
if x[0](phenny, input):
return # Abort function
arg = input.group(2)
if not arg:
return phenny.reply("Need a nickname...")
if not ' ' in arg:
return phenny.reply("...and text")
teller = input.nick
target = arg.split(" ")[0]
text = " ".join(arg.split(" ")[1:])
if target.lower() == teller.lower():
return phenny.say("You can tell that to yourself")
if target.lower() == phenny.nick.lower():
return phenny.say("I'm not dumb, you know?")
2013-11-17 10:31:50 -08:00
d = (teller, target, text, int(time.time()))
2014-02-01 10:03:01 -08:00
tell_list.append(d)
tell_pending.append(("INSERT INTO tell (nick, tellee, msg, time) VALUES (?,?,?,?)", d))
2013-11-17 10:31:50 -08:00
response = "I'll pass that on when %s is around" % target
rand = random.random()
if rand > 0.85: response = "yeah, sure, whatever"
elif rand > 0.75: response = "yeah, yeah"
2013-11-17 10:31:50 -08:00
phenny.reply(response)
tell.commands = ["tell"]
def checktell(phenny, input):
2014-02-01 09:58:47 -08:00
global tell_diskwriteinterval, tell_lastdiskwrite, tell_lastlisthash, tell_pending
2013-11-17 10:36:08 -08:00
for e in tell_list:
2014-02-04 06:23:35 -08:00
if e[1].lower() == input.nick.lower():
2014-03-21 06:08:24 -07:00
print("tell hit! %r" % (e,))
2014-03-21 05:58:10 -07:00
phenny.say("%s: %s <%s> %s" % (
input.nick,
2014-03-21 06:08:24 -07:00
time.strftime('%m-%d %H:%M UTC',
time.gmtime(e[3])),
2014-03-21 05:58:10 -07:00
e[0],
e[2]))
2013-11-17 10:36:08 -08:00
tell_list.remove(e)
2014-02-04 06:23:35 -08:00
tell_pending.append(("DELETE FROM tell WHERE nick = ? AND tellee = ? AND msg = ? AND time = ?", e))
break
if time.time() - tell_diskwriteinterval > tell_lastdiskwrite:
tell_lastdiskwrite = time.time()
2014-02-04 06:23:35 -08:00
current_hash = hashlib.sha1('\n'.join(repr(e) for e in tell_list)).hexdigest()
2014-03-21 06:08:24 -07:00
print("testing write to disk (old, new) = (%s, %s)" % (tell_lastlisthash, current_hash))
if current_hash == tell_lastlisthash:
2013-11-17 10:31:50 -08:00
return
tell_lastlisthash = current_hash
db = sqlite3.connect("tell.sqlite")
c = db.cursor()
for tr in tell_pending:
c.execute(*tr)
c.close()
db.commit()
db.close()
tell_pending = []
2013-11-17 10:31:50 -08:00
def note(phenny, input):
if input.sender.startswith('#'):
checktell(phenny, input)
note.rule = r'.*'
note.priority = 'low'
def note_join(phenny, input):
if input.sender.startswith('#'):
checktell(phenny, input)
note_join.rule = r'.*'
note_join.event = 'JOIN'
note_join.priority = 'low'
db = sqlite3.connect("tell.sqlite")
c = db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS tell (id INTEGER PRIMARY KEY, nick TEXT, tellee TEXT, msg TEXT, time INTEGER)")
c.execute("SELECT * FROM tell")
2013-11-17 10:31:50 -08:00
while True:
e = c.fetchone()
if not e:
break
tell_list.append(e)
c.close()
db.commit()
2013-11-17 10:31:50 -08:00
db.close()
if __name__ == '__main__':
print __doc__.strip()