diff --git a/otouto/plugins/quotes.lua b/otouto/plugins/quotes.lua index 527d617..510f460 100644 --- a/otouto/plugins/quotes.lua +++ b/otouto/plugins/quotes.lua @@ -50,16 +50,30 @@ function quotes:delete_quote(msg) end end -function quotes:get_quote(msg) +function quotes:get_quote(msg, num) 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.\nSpeichere doch welche mit /addquote [Zitat]' + return nil, 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit /addquote [Zitat]' else - return quotes_table[math.random(1,#quotes_table)] + local totalquotes = #quotes_table + if num then + selected_quote = tonumber(num) + else + selected_quote = math.random(1,totalquotes) + end + local prev_num = selected_quote - 1 + if prev_num == 0 then + prev_num = totalquotes -- last quote + end + local next_num = selected_quote + 1 + if next_num > totalquotes then + next_num = 1 + end + return prev_num, quotes_table[selected_quote], next_num end end end @@ -82,10 +96,21 @@ function quotes:list_quotes(msg) end end -function quotes:action(msg, config, matches) - if matches[1] == "quote" then - utilities.send_message(self, msg.chat.id, quotes:get_quote(msg), true) - return +function quotes:action(msg, config, matches, num, self_plz) + if num or matches[1] == "quote" then + if not self.BASE_URL then self = self_plz end + local prev_num, selected_quote, next_num = quotes:get_quote(msg, num) + if prev_num == next_num or not next_num or not prev_num then + keyboard = nil + else + keyboard = '{"inline_keyboard":[[{"text":"« '..prev_num..'","callback_data":"quotes:'..prev_num..'"},{"text":"'..next_num..' »","callback_data":"quotes:'..next_num..'"}]]}' + end + if num then + local result = utilities.edit_message(self, msg.chat.id, msg.message_id, selected_quote, true, false, keyboard) + return + end + utilities.send_message(self, msg.chat.id, selected_quote, true, false, false, keyboard) + return elseif matches[1] == "addquote" and matches[2] then utilities.send_reply(self, msg, quotes:save_quote(msg), true) return @@ -108,4 +133,9 @@ function quotes:action(msg, config, matches) utilities.send_reply(self, msg, quotes.doc, true) end +function quotes:callback(callback, msg, self, config, num) + utilities.answer_callback_query(self, callback) + quotes:action(msg, config, nil, num, self) +end + return quotes