2016-06-14 13:14:09 +02:00
local quotes = { }
2016-07-17 13:22:27 +02:00
require ( " ./miku/plugins/pasteee " )
2016-06-14 13:14:09 +02:00
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 )
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 )
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
function quotes : get_quote ( msg )
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
return ' Es wurden noch keine Zitate gespeichert. \n Speichere doch welche mit /addquote [Zitat] '
else
return quotes_table [ math.random ( 1 , # quotes_table ) ]
end
end
end
2016-09-27 23:31:16 +02:00
function quotes : callback ( callback , msg , self , config )
2016-06-14 13:14:09 +02:00
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 = " "
2016-09-27 23:31:16 +02:00
2016-06-14 13:14:09 +02:00
for num , quote in pairs ( quotes_table ) do
text = text .. num .. " ) " .. quote .. ' \n '
end
2016-09-27 23:31:16 +02:00
2016-06-14 13:14:09 +02:00
if not text or text == " " then
2016-09-27 23:31:16 +02:00
utilities.answer_callback_query ( callback , ' Es wurden noch keine Zitate gespeichert. ' , true )
2016-06-14 13:14:09 +02:00
else
2016-09-27 23:31:16 +02:00
-- In case the quote list is > 4096 chars
local text_len = string.len ( text )
while text_len > 4096 do
to_send_text = string.sub ( text , 1 , 4096 )
text = string.sub ( text , 4096 , text_len )
2016-09-27 23:40:38 +02:00
local res = utilities.send_message ( callback.from . id , to_send_text , true )
2016-09-27 23:31:16 +02:00
if not res then
utilities.answer_callback_query ( callback , ' Bitte starte den Bot zuerst privat! ' , true )
return
end
text_len = string.len ( text )
end
2016-09-27 23:40:38 +02:00
local res = utilities.send_message ( callback.from . id , text , true )
2016-09-27 23:31:16 +02:00
if not res then
utilities.answer_callback_query ( callback , ' Bitte starte den Bot zuerst privat! ' , true )
return
end
utilities.answer_callback_query ( callback , ' Zitatliste per PN verschickt ' )
end
else
utilities.answer_callback_query ( callback , ' Es wurden noch keine Zitate gespeichert. ' , true )
2016-06-14 13:14:09 +02:00
end
end
function quotes : action ( msg , config , matches )
2016-09-27 23:31:16 +02:00
if msg.chat . type == ' private ' then
utilities.send_reply ( msg , ' Dieses Plugin kann nur in Gruppen verwendet werden! ' )
return
end
2016-06-14 13:14:09 +02:00
if matches [ 1 ] == " quote " then
2016-12-15 19:24:08 +01:00
utilities.send_message ( msg.chat . id , quotes : get_quote ( msg ) , true , msg.message_id , false )
2016-06-14 13:14:09 +02:00
return
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-26 18:09:43 +02:00
utilities.send_reply ( msg , config.errors . sudo , true )
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
2016-12-12 13:31:19 +01:00
utilities.send_reply ( msg , config.errors . sudo , true )
2016-08-26 00:28:09 +02:00
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
2016-09-27 23:31:16 +02:00
utilities.send_reply ( msg , ' Bitte klicke hier unten auf diese attraktive Schaltfläche. ' , false , ' {"inline_keyboard":[[{"text":"Alle Zitate per PN","callback_data":"quotes:"}]]} ' )
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