2016-06-14 13:14:09 +02:00
local quotes = { }
require ( " ./otouto/plugins/pasteee " )
function quotes : init ( config )
quotes.triggers = {
" ^/(delquote) (.+)$ " ,
" ^/(addquote) (.+)$ " ,
" ^/(quote)$ " ,
2016-08-26 00:28:09 +02:00
" ^/(listquotes)$ " ,
" ^/(delquote)$ "
2016-06-14 13:14:09 +02:00
}
quotes.doc = [ [ *
] ] .. config.cmd_pat .. [ [ addquote * _ < Zitat > _ : Fügt Zitat hinzu .
* ] ] .. config.cmd_pat .. [ [ delquote * _ < Zitat > _ : Löscht das Zitat ( nur Superuser )
* ] ] .. config.cmd_pat .. [ [ quote * : Gibt zufälliges Zitat aus
* ] ] .. config.cmd_pat .. [ [ listquotes * : Listet alle Zitate auf
] ]
end
quotes.command = ' quote '
function quotes : save_quote ( msg )
if msg.text : sub ( 11 ) : isempty ( ) then
return " Benutzung: /addquote [Zitat] "
end
local quote = msg.text : sub ( 11 )
local hash = get_redis_hash ( msg , ' quotes ' )
print ( ' Saving quote to redis set ' .. hash )
redis : sadd ( hash , quote )
return ' *Gespeichert!* '
end
function quotes : delete_quote ( msg )
if msg.text : sub ( 11 ) : isempty ( ) then
return " Benutzung: /delquote [Zitat] "
end
local quote = msg.text : sub ( 11 )
local hash = get_redis_hash ( msg , ' quotes ' )
print ( ' Deleting quote from redis set ' .. hash )
if redis : sismember ( hash , quote ) == true then
redis : srem ( hash , quote )
return ' *Zitat erfolgreich gelöscht!* '
else
return ' Dieses Zitat existiert nicht. '
end
end
2016-07-05 17:53:15 +02:00
function quotes : get_quote ( msg )
2016-06-14 13:14:09 +02:00
local hash = get_redis_hash ( msg , ' quotes ' )
if hash then
print ( ' Getting quote from redis set ' .. hash )
local quotes_table = redis : smembers ( hash )
if not quotes_table [ 1 ] then
2016-07-05 17:53:15 +02:00
return ' Es wurden noch keine Zitate gespeichert. \n Speichere doch welche mit /addquote [Zitat] '
2016-06-14 13:14:09 +02:00
else
2016-07-05 17:53:15 +02:00
return quotes_table [ math.random ( 1 , # quotes_table ) ]
2016-06-14 13:14:09 +02:00
end
end
end
function quotes : 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
2016-07-03 13:59:49 +02:00
return ' *Es wurden noch keine Zitate gespeichert.* \n Speichere doch welche mit `/addquote [Zitat]` ' , true
2016-06-14 13:14:09 +02:00
else
return upload ( text )
end
end
end
2016-07-05 17:53:15 +02:00
function quotes : action ( msg , config , matches )
if matches [ 1 ] == " quote " then
2016-08-24 15:38:29 +02:00
utilities.send_message ( msg.chat . id , quotes : get_quote ( msg ) , true )
2016-07-05 17:53:15 +02:00
return
2016-06-14 13:14:09 +02:00
elseif matches [ 1 ] == " addquote " and matches [ 2 ] then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , quotes : save_quote ( msg ) , true )
2016-06-14 13:14:09 +02:00
return
elseif matches [ 1 ] == " delquote " and matches [ 2 ] then
2016-08-26 00:28:09 +02:00
if not is_sudo ( msg , config ) then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , config.errors . sudo )
2016-06-14 13:14:09 +02:00
return
end
2016-08-26 00:28:09 +02:00
utilities.send_reply ( msg , quotes : delete_quote ( msg ) , true )
return
elseif matches [ 1 ] == " delquote " and not matches [ 2 ] then
if not is_sudo ( msg , config ) then
utilities.send_reply ( msg , config.errors . sudo )
return
end
if msg.reply_to_message then
local msg = msg.reply_to_message
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , quotes : delete_quote ( msg ) , true )
2016-06-14 13:14:09 +02:00
return
2016-08-26 00:28:09 +02:00
end
2016-06-14 13:14:09 +02:00
elseif matches [ 1 ] == " listquotes " then
local link , iserror = quotes : list_quotes ( msg )
if iserror then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , link , true )
2016-06-14 13:14:09 +02:00
return
end
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Ich habe eine Liste aller Zitate hochgeladen. ' , false , ' {"inline_keyboard":[[{"text":"Alle Zitate abrufen","url":" ' .. link .. ' "}]]} ' )
2016-06-14 13:14:09 +02:00
return
end
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , quotes.doc , true )
2016-06-14 13:14:09 +02:00
end
return quotes