fulvia/modules/tell.py

123 lines
3.0 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Leave a message for someone.
"""
import os
import sys
import time
import threading
from datetime import datetime
from sqlite3 import OperationalError
2019-10-08 12:39:13 -04:00
import config
2018-03-16 03:13:43 -04:00
from tools.time import relativeTime
from module import commands, example, hook
def load_database(bot):
"""
Loads all entries from the 'tell' table in the bot's database and
stores them in memory
"""
data = {}
tells = bot.db.execute("SELECT * FROM tell").fetchall()
for tell in tells:
tellee, teller, unixtime, message = tell
tell = (teller, unixtime, message)
try:
data[tellee].append(tell)
except KeyError:
data[tellee] = [tell]
return data
def insert_tell(bot, tellee, teller, unixtime, message):
"""
Inserts a new tell into the 'tell' table in the bot's database.
"""
bot.db.execute("INSERT INTO tell (tellee, teller, unixtime, message) "
"VALUES(?,?,?,?)", (tellee, teller, unixtime, message))
def delete_tell(bot, tellee):
"""
Deletes a tell from the 'tell' table in the bot's database, using
tellee as the key.
"""
bot.db.execute("DELETE FROM tell WHERE tellee = ?", (tellee,))
def setup(bot):
con = bot.db.connect()
cur = con.cursor()
try:
cur.execute("SELECT * FROM tell").fetchone()
except OperationalError:
cur.execute("CREATE TABLE tell("
"tellee TEXT, "
"teller TEXT,"
"unixtime INTEGER,"
"message TEXT)")
con.commit()
con.close()
bot.memory["tell"] = load_database(bot)
@commands('tell')
@example('.tell iou1name you broke something again.')
def tell(bot, trigger):
"""Give someone a message the next time they're seen"""
if not trigger.group(3):
return bot.reply("Tell whom?")
teller = trigger.nick
tellee = trigger.group(3).rstrip('.,:;')
message = trigger.group(2).replace(tellee, "", 1).strip()
if not message:
return bot.reply(f"Tell {tellee} what?")
if tellee == bot.nick:
return bot.reply("I'm here now, you can tell me whatever you want!")
if tellee == teller or tellee == "me":
return bot.reply("You can tell yourself that.")
unixtime = time.time()
if not tellee in bot.memory['tell']:
bot.memory['tell'][tellee] = [(teller, unixtime, message)]
else:
bot.memory['tell'][tellee].append((teller, unixtime, message))
insert_tell(bot, tellee, teller, unixtime, message)
response = f"I'll pass that on when {tellee} is around."
bot.reply(response)
@hook(True)
def tell_hook(bot, trigger):
"""
Hooks every line to see if a tellee has said something. If they have,
it gives them all of their tells.
"""
if not trigger.nick in bot.memory["tell"]:
return
tellee = trigger.nick
tells = []
for tell in bot.memory["tell"][tellee]:
teller, unixtime, message = tell
telldate = datetime.fromtimestamp(unixtime)
2019-10-08 12:39:13 -04:00
reltime = relativeTime(datetime.now(), telldate)
t_format = config.default_time_format
2018-03-16 03:13:43 -04:00
telldate = datetime.strftime(telldate, t_format)
msg = f"{tellee}: \x0310{message}\x03 (\x0308{teller}\x03) {telldate}" \
+ f" [\x0312{reltime} ago\x03]"
tells.append(msg)
for tell in tells:
bot.msg(tell)
bot.memory["tell"].pop(tellee)
delete_tell(bot, tellee)