2016-04-26 07:40:31 +02:00
|
|
|
local remind = {}
|
2016-04-18 05:28:55 +02:00
|
|
|
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-18 05:28:55 +02:00
|
|
|
|
2016-04-26 07:40:31 +02:00
|
|
|
remind.command = 'remind <duration> <message>'
|
2016-04-18 05:28:55 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function remind:init(config)
|
2016-04-26 07:40:31 +02:00
|
|
|
self.database.reminders = self.database.reminders or {}
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
remind.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('remind', true).table
|
|
|
|
remind.doc = [[```
|
|
|
|
]]..config.cmd_pat..[[remind <duration> <message>
|
|
|
|
Repeats a message after a duration of time, in minutes.
|
|
|
|
```]]
|
2016-04-26 07:40:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function remind:action(msg)
|
2016-04-18 05:28:55 +02:00
|
|
|
-- Ensure there are arguments. If not, send doc.
|
2016-04-29 06:36:35 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2016-04-18 05:28:55 +02:00
|
|
|
if not input then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
|
2016-04-18 05:28:55 +02:00
|
|
|
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)
|
2016-04-18 05:28:55 +02:00
|
|
|
if not tonumber(duration) then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
|
2016-04-18 05:28:55 +02:00
|
|
|
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.
|
2016-04-29 06:36:35 +02:00
|
|
|
local message = utilities.input(input)
|
2016-04-18 05:28:55 +02:00
|
|
|
if not message then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
|
2016-04-18 05:28:55 +02:00
|
|
|
return
|
|
|
|
end
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
local chat_id_str = tostring(msg.chat.id)
|
2016-04-18 05:28:55 +02:00
|
|
|
-- Make a database entry for the group/user if one does not exist.
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
self.database.reminders[chat_id_str] = self.database.reminders[chat_id_str] or {}
|
2016-04-18 05:28:55 +02:00
|
|
|
-- Limit group reminders to 10 and private reminders to 50.
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
if msg.chat.type ~= 'private' and utilities.table_size(self.database.reminders[chat_id_str]) > 9 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, 'Sorry, this group already has ten reminders.')
|
2016-04-18 05:28:55 +02:00
|
|
|
return
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
elseif msg.chat.type == 'private' and utilities.table_size(self.database.reminders[chat_id_str]) > 49 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(msg, 'Sorry, you already have fifty reminders.')
|
2016-04-18 05:28:55 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Put together the reminder with the expiration, message, and message to reply to.
|
|
|
|
local reminder = {
|
|
|
|
time = os.time() + duration * 60,
|
|
|
|
message = message
|
|
|
|
}
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
table.insert(self.database.reminders[chat_id_str], reminder)
|
2016-04-18 05:28:55 +02:00
|
|
|
local output = 'I will remind you in ' .. duration
|
|
|
|
if duration == 1 then
|
|
|
|
output = output .. ' minute!'
|
|
|
|
else
|
|
|
|
output = output .. ' minutes!'
|
|
|
|
end
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, output)
|
2016-04-18 05:28:55 +02:00
|
|
|
end
|
|
|
|
|
2016-04-26 07:40:31 +02:00
|
|
|
function remind:cron()
|
2016-04-18 05:28:55 +02:00
|
|
|
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
|
2016-04-18 05:28:55 +02:00
|
|
|
local new_group = {}
|
|
|
|
-- Iterate over each reminder.
|
2016-04-26 07:40:31 +02:00
|
|
|
for _, reminder in ipairs(group) do
|
2016-04-18 05:28:55 +02:00
|
|
|
-- 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) .. '"'
|
2016-05-29 19:08:39 +02:00
|
|
|
local res = utilities.send_message(self, chat_id, output, true, nil, true)
|
2016-04-18 05:28:55 +02:00
|
|
|
-- If the message fails to send, save it for later.
|
2016-04-26 07:40:31 +02:00
|
|
|
if not res then
|
2016-04-18 05:28:55 +02:00
|
|
|
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
|
2016-04-18 05:28:55 +02:00
|
|
|
-- 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
|
2016-04-18 05:28:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 07:40:31 +02:00
|
|
|
return remind
|