2016-05-01 16:47:35 +00:00
|
|
|
import random, time
|
2016-04-29 11:43:48 +00:00
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-04-29 11:43:48 +00:00
|
|
|
self.bot = bot
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command").on("quoteadd", "qadd").hook(
|
|
|
|
self.quote_add, min_args=1, help="Added a quote to a category",
|
2016-04-29 11:43:48 +00:00
|
|
|
usage="<category> = <quote>")
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command").on("quoteget", "qget").hook(
|
|
|
|
self.quote_get, min_args=1, help="Find a quote within a category",
|
2016-04-29 11:43:48 +00:00
|
|
|
usage="<category> = <search>")
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command").on("quotedel", "qdel").hook(
|
|
|
|
self.quote_del, min_args=1, help="Delete a quote from a category",
|
2016-04-29 11:43:48 +00:00
|
|
|
usage="<category> = <quote>")
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command").on("quote", "q").hook(self.quote,
|
2016-05-01 16:47:35 +00:00
|
|
|
help="Get a random quote from a category",
|
2018-09-19 12:25:12 +00:00
|
|
|
usage="<category>", min_args=1)
|
2016-04-29 11:43:48 +00:00
|
|
|
|
|
|
|
def category_and_quote(self, s):
|
|
|
|
if "=" in s:
|
|
|
|
return [part.strip() for part in s.split("=", 1)]
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
def quote_add(self, event):
|
|
|
|
category, quote = self.category_and_quote(event["args"])
|
|
|
|
if category and quote:
|
|
|
|
setting = "quotes-%s" % category
|
|
|
|
quotes = event["server"].get_setting(setting, [])
|
2016-05-01 21:47:59 +00:00
|
|
|
quotes.append([event["user"].name, int(time.time()), quote])
|
2016-04-29 11:43:48 +00:00
|
|
|
event["server"].set_setting(setting, quotes)
|
|
|
|
event["stdout"].write("Quote added")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Please provide a category AND quote")
|
|
|
|
|
|
|
|
def quote_get(self, event):
|
|
|
|
category, to_find = self.category_and_quote(event["args"])
|
|
|
|
if category and to_find:
|
|
|
|
to_find = to_find.lower()
|
|
|
|
quotes = event["server"].get_setting("quotes-%s" % category, [])
|
|
|
|
found = []
|
|
|
|
for nickname, time_added, quote in quotes:
|
|
|
|
if to_find in quote.lower():
|
|
|
|
found.append(quote)
|
|
|
|
if found:
|
|
|
|
event["stdout"].write("%d quote%s found: %s" % (len(found),
|
|
|
|
"s" if len(found) > 1 else "", found[0]))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No quotes found")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Please provide a category and a "
|
|
|
|
"part of a quote to find")
|
|
|
|
|
|
|
|
def quote_del(self, event):
|
|
|
|
category, remove_quote = self.category_and_quote(event["args"])
|
|
|
|
remove_quote_lower = remove_quote.lower()
|
|
|
|
if category and remove_quote:
|
|
|
|
setting = "quotes-%s" % category
|
|
|
|
quotes = event["server"].get_setting(setting, [])
|
|
|
|
removed = False
|
|
|
|
for nickname, time_added, quote in quotes[:]:
|
|
|
|
if quote.lower() == remove_quote_lower:
|
|
|
|
quotes.remove([nickname, time_added, quote])
|
|
|
|
removed = True
|
|
|
|
if removed:
|
|
|
|
event["server"].set_setting(setting, quotes)
|
|
|
|
event["stdout"].write("Removed quote")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Quote not found")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Please provide a category and a quote "
|
|
|
|
"to remove")
|
2016-05-01 16:47:35 +00:00
|
|
|
|
|
|
|
def quote(self, event):
|
|
|
|
category = event["args"].strip().lower()
|
|
|
|
quotes = event["server"].get_setting("quotes-%s" % category, [])
|
|
|
|
if quotes:
|
|
|
|
index = random.randint(0, len(quotes)-1)
|
|
|
|
nickname, time_added, quote = quotes[index]
|
|
|
|
event["stdout"].write("%s: %s" % (category, quote))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("There are no quotes for this category")
|