add !quotegrab to take the last message from a user and quote it (quotes.py)

This commit is contained in:
jesopo 2019-01-26 15:55:20 +00:00
parent e1e4fbf9bc
commit 5b4954a96c

View file

@ -7,6 +7,11 @@ class Module(ModuleManager.BaseModule):
return [part.strip() for part in s.split("=", 1)] return [part.strip() for part in s.split("=", 1)]
return None, None return None, None
def _get_quotes(self, server, category):
return server.get_setting("quotes-%s" % category, [])
def _set_quotes(self, server, category, quotes):
server.set_setting("quote-%s" % category, quotes)
@utils.hook("received.command.qadd", alias_of="quoteadd") @utils.hook("received.command.qadd", alias_of="quoteadd")
@utils.hook("received.command.quoteadd", min_args=1) @utils.hook("received.command.quoteadd", min_args=1)
def quote_add(self, event): def quote_add(self, event):
@ -16,10 +21,9 @@ class Module(ModuleManager.BaseModule):
""" """
category, quote = self.category_and_quote(event["args"]) category, quote = self.category_and_quote(event["args"])
if category and quote: if category and quote:
setting = "quotes-%s" % category quotes = self._get_quotes(event["server"], category)
quotes = event["server"].get_setting(setting, [])
quotes.append([event["user"].name, int(time.time()), quote]) quotes.append([event["user"].name, int(time.time()), quote])
event["server"].set_setting(setting, quotes) self._set_quotes(event["server"], quotes)
event["stdout"].write("Quote added") event["stdout"].write("Quote added")
else: else:
event["stderr"].write("Please provide a category AND quote") event["stderr"].write("Please provide a category AND quote")
@ -89,3 +93,17 @@ class Module(ModuleManager.BaseModule):
event["stdout"].write("%s: %s" % (category, quote)) event["stdout"].write("%s: %s" % (category, quote))
else: else:
event["stderr"].write("There are no quotes for this category") event["stderr"].write("There are no quotes for this category")
@utils.hook("received.command.quotegrab", min_args=1, channel_only=True)
def quote_grab(self, event):
line = event["target"].buffer.find_from(event["args_split"][0])
if line:
quotes = self._get_quotes(event["server"], line.sender)
if line.action:
quotes.append("* %s %s" % (line.sender, line.message))
else:
quotes.append("<%s> %s" % (line.sender, line.message))
self._set_quotes(event["server"], quotes)
event["stdout"].write("Quote added")
else:
event["stderr"].write("Nothing found to quote")