notes: limit number of notes for an user

This commit is contained in:
dece 2020-10-12 19:19:50 +02:00
parent b87487deea
commit 263e9fd615
2 changed files with 18 additions and 2 deletions

View file

@ -42,7 +42,9 @@
"commands": ["note down"], "commands": ["note down"],
"content_regex": "for (?P<target>\\S+) (?P<note>.+)", "content_regex": "for (?P<target>\\S+) (?P<note>.+)",
"confirmation": "Ok.", "confirmation": "Ok.",
"deliver_format": "{dest}, {sender} tells you: {message}" "deliver_format": "{dest}, {sender} tells you: {message}",
"limit": 5,
"too_many_notes": "There are too many notes for this user."
}, },
"random": { "random": {
"commands": ["choose"], "commands": ["choose"],

View file

@ -5,7 +5,10 @@ from edmond.plugin import Plugin
class NotesPlugin(Plugin): class NotesPlugin(Plugin):
REQUIRED_CONFIGS = ["commands", "content_regex", "deliver_format"] REQUIRED_CONFIGS = [
"commands", "content_regex", "confirmation", "deliver_format", "limit",
"too_many_notes"
]
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
@ -48,6 +51,12 @@ class NotesPlugin(Plugin):
if any(k not in groups for k in ("target", "note")): if any(k not in groups for k in ("target", "note")):
return False return False
target = groups["target"] target = groups["target"]
# Check we did not reach our note limits.
if self.count_user_notes(target) >= self.config["limit"]:
self.bot.say(event.target, self.config["too_many_notes"])
return True
message = groups["note"] message = groups["note"]
self.bot.log_d(f"Noting for {target}: {message}") self.bot.log_d(f"Noting for {target}: {message}")
note = { note = {
@ -57,3 +66,8 @@ class NotesPlugin(Plugin):
} }
self.append_storage_list_value("notes", note) self.append_storage_list_value("notes", note)
self.bot.say(event.target, self.config["confirmation"]) self.bot.say(event.target, self.config["confirmation"])
def count_user_notes(self, nick):
"""Count the number of undelivered notes this user has."""
notes = self.get_storage_value("notes", [])
return sum(note["dest"] == nick for note in notes)