m4pl1mp/plugins/notes_plugin.py
2022-02-02 23:04:48 -06:00

166 lines
7.4 KiB
Python

# -*- coding: utf-8 -*-
from irc3.plugins.command import command
import irc3
from datetime import datetime
import dateutil.parser
import timeago
import uuid
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
#######################################################################################
#######################################################################################
def __init__(self, bot):
self.bot = bot
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def notes(self, *args, **kwargs):
"""list/write/del notes
%%notes [<cmd>] [<operation>]
"""
return self.note(*args)
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def note(self, mask, target, args):
"""list/write/del notes
%%note [<cmd>] [<operation>]
"""
cmd = args.get("<cmd>")
if not cmd:
msg = "Please specify read/write/delete/list"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
elif cmd in ["d","del","delete","remove"]:
self.note_del(mask, target, args)
elif cmd in ["w", "write","a", "add"]:
self.note_write(mask, target, args)
elif cmd in ["l", "list"]:
self.note_list(mask, target, args)
elif cmd in ["r", "read"]:
self.note_read(mask, target, args)
return
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def note_del(self, mask, target, args):
"""Delete Note or * for all notes
%%note <cmd> <operation>
"""
note_uuid_hash = args.get("<operation>")
if not note_uuid_hash:
msg = "Please specify note"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
if note_uuid_hash == "*":
self.bot.db.setlist("notes", [])
msg = "All notes cleared!"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
else:
note_list = self.bot.db.getlist("notes", [])
if not note_list:
msg = "{} Not Found".format(note_uuid_hash)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
else:
if note_uuid_hash in [item.get("uuid") for item in note_list]:
new_note_list = [item for item in note_list if item.get("uuid") != note_uuid_hash]
self.bot.db.setlist("notes", new_note_list)
msg = "{} Removed".format(note_uuid_hash)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def note_list(self, mask, target, args):
"""List All Note Names
%%note list
"""
note_list = self.bot.db.getlist("notes")
if not note_list:
msg = "No notes!"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
for note in note_list:
note_uuid = note.get("uuid")
note_from = note.get("from")
note_time = dateutil.parser.parse(note.get("time"))
note_time = timeago.format(note_time, datetime.now())
note_msg = note.get("note_msg")
msg = "<{}> {} | {} | {}".format(note_from, note_time, note_msg, note_uuid)
self.bot.privmsg(target, msg)
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def note_read(self, mask, target, args):
"""Display Note
%%note read <operation>
"""
note_name = args.get("<operation>")
if not note_name:
msg = "Please specify name or note id"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
note_list = self.bot.db.getlist("notes")
if not note_list:
msg = "No Notes in DB"
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
if not any(note_name in d["uuid"] for d in note_list):
msg = "No notes with {} found".format(note_name)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
else:
note = [d for d in note_list if d.get("uuid") == note_name]
if note:
note = note.pop()
else:
try:
note = note_list[int(note_name)]
except:
msg = "No notes with {} found".format(note_name)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
return
note_uuid = note.get("uuid")
note_from = note.get("from")
note_time = dateutil.parser.parse(note.get("time"))
note_time = timeago.format(note_time, datetime.now())
note_msg = note.get("note_msg")
msg = "<{}> {} | {} | {}".format(note_from, note_time, note_msg, note_uuid)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=True)
def note_write(self, mask, target, args):
"""Add Note
%%note write <note>
"""
note_msg = args.get("<operation>")
note_list = self.bot.db.getlist("notes")
if not note_list:
note_list = []
self.bot.db.setlist("notes", note_list)
note_uuid = uuid.uuid4().hex
note = {"time": datetime.now().isoformat(),
"from": mask.nick, "uuid": note_uuid,
"note_msg": note_msg}
note_list.append(note)
self.bot.db.setlist("notes", note_list)
msg = "Note Added! {}".format(note_uuid)
msg = self.bot.emo(msg)
self.bot.privmsg(target, msg)
#######################################################################################
#######################################################################################
###########################################################################################
###########################################################################################