This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/remind.lua

99 lines
3.2 KiB
Lua
Raw Normal View History

2016-04-26 07:40:31 +02:00
local remind = {}
2016-04-26 07:40:31 +02:00
local bindings = require('bindings')
local utilities = require('utilities')
2016-04-26 07:40:31 +02:00
remind.command = 'remind <duration> <message>'
remind.doc = [[```
/remind <duration> <message>
Repeats a message after a duration of time, in minutes.
```]]
2016-04-26 07:40:31 +02:00
function remind:init()
self.database.reminders = self.database.reminders or {}
remind.triggers = utilities.triggers(self.info.username):t('remind', true).table
end
function remind:action(msg)
-- Ensure there are arguments. If not, send doc.
local input = msg.text:input()
if not input then
2016-04-26 07:40:31 +02:00
bindings.sendMessage(self, msg.chat.id, remind.doc, true, msg.message_id, true)
return
end
-- Ensure first arg is a number. If not, send doc.
2016-04-26 07:40:31 +02:00
local duration = utilities.get_word(input, 1)
if not tonumber(duration) then
2016-04-26 07:40:31 +02:00
bindings.sendMessage(self, msg.chat.id, remind.doc, true, msg.message_id, true)
return
end
-- Duration must be between one minute and one year (approximately).
duration = tonumber(duration)
if duration < 1 then
duration = 1
elseif duration > 526000 then
duration = 526000
end
-- Ensure there is a second arg.
local message = input:input()
if not message then
2016-04-26 07:40:31 +02:00
bindings.sendMessage(self, msg.chat.id, remind.doc, true, msg.message_id, true)
return
end
-- Make a database entry for the group/user if one does not exist.
2016-04-26 07:40:31 +02:00
self.database.reminders[msg.chat.id_str] = self.database.reminders[msg.chat.id_str] or {}
-- Limit group reminders to 10 and private reminders to 50.
2016-04-26 07:40:31 +02:00
if msg.chat.type ~= 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 9 then
bindings.sendReply(self, msg, 'Sorry, this group already has ten reminders.')
return
2016-04-26 07:40:31 +02:00
elseif msg.chat.type == 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 49 then
bindings.sendReply(msg, 'Sorry, you already have fifty reminders.')
return
end
-- Put together the reminder with the expiration, message, and message to reply to.
local reminder = {
time = os.time() + duration * 60,
message = message
}
2016-04-26 07:40:31 +02:00
table.insert(self.database.reminders[msg.chat.id_str], reminder)
local output = 'I will remind you in ' .. duration
if duration == 1 then
output = output .. ' minute!'
else
output = output .. ' minutes!'
end
2016-04-26 07:40:31 +02:00
bindings.sendReply(self, msg, output)
end
2016-04-26 07:40:31 +02:00
function remind:cron()
local time = os.time()
-- Iterate over the group entries in the reminders database.
2016-04-26 07:40:31 +02:00
for chat_id, group in pairs(self.database.reminders) do
local new_group = {}
-- Iterate over each reminder.
2016-04-26 07:40:31 +02:00
for _, reminder in ipairs(group) do
-- If the reminder is past-due, send it and nullify it.
-- Otherwise, add it to the replacement table.
if time > reminder.time then
2016-04-29 06:12:04 +02:00
local output = '*Reminder:*\n"' .. utilities.md_escape(reminder.message) .. '"'
local res = bindings.sendMessage(self, chat_id, output, true, nil, true)
-- If the message fails to send, save it for later.
2016-04-26 07:40:31 +02:00
if not res then
table.insert(new_group, reminder)
end
else
table.insert(new_group, reminder)
end
end
-- Nullify the original table and replace it with the new one.
2016-04-26 07:40:31 +02:00
self.database.reminders[chat_id] = new_group
-- Nullify the table if it is empty.
if #new_group == 0 then
2016-04-26 07:40:31 +02:00
self.database.reminders[chat_id] = nil
end
end
end
2016-04-26 07:40:31 +02:00
return remind