Zitate können nun gelistet werden

This commit is contained in:
Akamaru 2015-11-14 23:48:54 +01:00
parent d0a32f0f1e
commit 7d9e98d9b7
1 changed files with 29 additions and 7 deletions

View File

@ -2,7 +2,7 @@ do
local function save_quote(msg) local function save_quote(msg)
if msg.text:sub(11):isempty() then if msg.text:sub(11):isempty() then
return "Benutzung: !addquote [Zitat]" return "Benutzung: /addquote [Zitat]"
end end
local quote = msg.text:sub(11) local quote = msg.text:sub(11)
@ -14,7 +14,7 @@ end
local function delete_quote(msg) local function delete_quote(msg)
if msg.text:sub(11):isempty() then if msg.text:sub(11):isempty() then
return "Benutzung: !delquote [Zitat]" return "Benutzung: /delquote [Zitat]"
end end
local quote = msg.text:sub(11) local quote = msg.text:sub(11)
@ -34,17 +34,35 @@ local function get_quote(msg)
if hash then if hash then
print('Getting quote from redis set '..hash) print('Getting quote from redis set '..hash)
quotes_table = redis:smembers(hash) local quotes_table = redis:smembers(hash)
if not quotes_table[1] then if not quotes_table[1] then
return 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit !addquote [Zitat]' return 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit /addquote [Zitat]'
else else
return quotes_table[math.random(1,#quotes_table)] return quotes_table[math.random(1,#quotes_table)]
end end
end end
end end
local function list_quotes(msg)
local hash = get_redis_hash(msg, 'quotes')
if hash then
print('Getting quotes from redis set '..hash)
local quotes_table = redis:smembers(hash)
local text = ""
for num,quote in pairs(quotes_table) do
text = text..num..") "..quote..'\n'
end
if not text or text == "" then
return 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit /addquote [Zitat]'
else
return text
end
end
end
local function run(msg, matches) local function run(msg, matches)
if string.match(msg.text, "/quote$") then if matches[1] == "quote" then
return get_quote(msg) return get_quote(msg)
elseif matches[1] == "addquote" then elseif matches[1] == "addquote" then
return save_quote(msg) return save_quote(msg)
@ -54,6 +72,8 @@ local function run(msg, matches)
else else
return delete_quote(msg) return delete_quote(msg)
end end
elseif matches[1] == "listquotes" then
return list_quotes(msg)
end end
end end
@ -62,12 +82,14 @@ return {
usage = { usage = {
"/addquote [Zitat]: Fügt Zitat hinzu.", "/addquote [Zitat]: Fügt Zitat hinzu.",
"/quote: Gibt zufälliges Zitat aus.", "/quote: Gibt zufälliges Zitat aus.",
"/delquote [Zitat]: Löscht das Zitat (nur Superuser)" "/delquote [Zitat]: Löscht das Zitat (nur Superuser)",
"/listsquotes: Listet alle Zitate auf"
}, },
patterns = { patterns = {
"^/(delquote) (.+)$", "^/(delquote) (.+)$",
"^/(addquote) (.+)$", "^/(addquote) (.+)$",
"^/quote$" "^/(quote)$",
"^/(listquotes)$"
}, },
run = run run = run
} }