From 33a4607a3ab93156cb150e8bdc40e97c291e1dce Mon Sep 17 00:00:00 2001 From: Akamaru Date: Tue, 28 Apr 2015 17:46:31 +0200 Subject: [PATCH] new plugin quotes --- plugins/quotes.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 plugins/quotes.lua diff --git a/plugins/quotes.lua b/plugins/quotes.lua new file mode 100644 index 0000000..0243200 --- /dev/null +++ b/plugins/quotes.lua @@ -0,0 +1,66 @@ + +local quotes_file = './data/quotes.lua' +local quotes_table + +function read_quotes_file() + local f = io.open(quotes_file, "r+") + + if f == nil then + print ('Erstelle neue Zitat Datei '..quotes_file) + serialize_to_file({}, quotes_file) + else + print ('Zitate geladen: '..quotes_file) + f:close() + end + return loadfile (quotes_file)() +end + +function save_quote(msg) + local to_id = tostring(msg.to.id) + + if msg.text:sub(11):isempty() then + return "Verwendung: /addquote [Text]" + end + + if quotes_table == nil then + quotes_table = {} + end + + if quotes_table[to_id] == nil then + print ('New quote key to_id: '..to_id) + quotes_table[to_id] = {} + end + + local quotes = quotes_table[to_id] + quotes[#quotes+1] = msg.text:sub(11) + + serialize_to_file(quotes_table, quotes_file) + + return "Neues Zitat hinzugefĆ¼gt!" +end + +function get_quote(msg) + local to_id = tostring(msg.to.id) + local quotes_phrases + + quotes_table = read_quotes_file() + quotes_phrases = quotes_table[to_id] + + return quotes_phrases[math.random(1,#quotes_phrases)] +end + +function run(msg, matches) + if string.match(msg.text, "/quote$") then + return get_quote(msg) + elseif string.match(msg.text, "/addquote (.+)$") then + quotes_table = read_quotes_file() + return save_quote(msg) + end +end + +return { + description = "Speichert Zitate", + usage = {"/addquote [Text]","/quote",}, + patterns = {"^/addquote (.+)$","^/quote$",}, + run = run +}