From effe34cb32b7d68fc9419e54a8b119480b075838 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Sun, 17 Apr 2016 23:28:55 -0400 Subject: [PATCH] about.lua: Less redundant if-else. administration.lua: Will now attempt to unban kicked users in supergrounds. remind.lua: New plugin! Set a reminder. control.lua: No more lost control messages. --- plugins/about.lua | 10 ++-- plugins/administration.lua | 15 ++++++ plugins/control.lua | 2 +- plugins/remind.lua | 102 +++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 plugins/remind.lua diff --git a/plugins/about.lua b/plugins/about.lua index f57dc28..030abf1 100755 --- a/plugins/about.lua +++ b/plugins/about.lua @@ -13,13 +13,9 @@ local action = function(msg) local message = config.about_text .. '\nBased on @otouto v'..version..' by topkecleon.' - if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then - sendMessage(msg.chat.id, message, true) - return - elseif msg.text_lower:match('^/about[@'..bot.username..']*') then - sendMessage(msg.chat.id, message, true) - return - elseif msg.text_lower:match('^/start') then + if (msg.new_chat_participant and msg.new_chat_participant.id == bot.id) + or msg.text_lower:match('^/about[@'..bot.username..']*') + or msg.text_lower:match('^/start') then sendMessage(msg.chat.id, message, true) return end diff --git a/plugins/administration.lua b/plugins/administration.lua index 81713bf..c974ccd 100644 --- a/plugins/administration.lua +++ b/plugins/administration.lua @@ -254,6 +254,9 @@ local commands = { return true end drua.kick_user(msg.chat.id, msg.from.id) + if msg.chat.type == 'supergroup' then + unbanChatMember(msg.chat.id, msg.from.id) + end local output = flags[2].kicked:gsub('GROUPNAME', msg.chat.title) sendMessage(msg.from.id, output) end @@ -282,6 +285,9 @@ local commands = { if group.flags[3] == true then if msg.from.name:match('[\216-\219][\128-\191]') or msg.from.name:match('‮') or msg.from.name:match('‏') then drua.kick_user(msg.chat.id, msg.from.id) + if msg.chat.type == 'supergroup' then + unbanChatMember(msg.chat.id, msg.from.id) + end local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title) sendMessage(msg.from.id, output) return @@ -320,6 +326,9 @@ local commands = { end if admin_temp.flood[msg.chat.id_str][msg.from.id_str] > 99 then drua.kick_user(msg.chat.id, msg.from.id) + if msg.chat.type == 'supergroup' then + unbanChatMember(msg.chat.id, msg.from.id) + end local output = flags[5].kicked:gsub('GROUPNAME', msg.chat.title) sendMessage(msg.from.id, output) admin_temp.flood[msg.chat.id_str][msg.from.id_str] = nil @@ -344,6 +353,9 @@ local commands = { if group.flags[3] == true then if msg.new_chat_participant.name:match('[\216-\219][\128-\191]') or msg.new_chat_participant.name:match('‮') or msg.new_chat_participant.name:match('‏') then drua.kick_user(msg.chat.id, msg.new_chat_participant.id) + if msg.chat.type == 'supergroup' then + unbanChatMember(msg.chat.id, msg.from.id) + end local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title) sendMessage(msg.new_chat_participant.id, output) return @@ -617,6 +629,9 @@ local commands = { return end drua.kick_user(msg.chat.id, target.id) + if msg.chat.type == 'supergroup' then + unbanChatMember(msg.chat.id, target.id) + end sendMessage(msg.chat.id, target.name .. ' has been kicked.') end }, diff --git a/plugins/control.lua b/plugins/control.lua index 663cc1e..e0a0a0f 100644 --- a/plugins/control.lua +++ b/plugins/control.lua @@ -9,7 +9,7 @@ local action = function(msg) return end - if msg.date < os.time() then return end + if msg.date < os.time() - 1 then return end if msg.text:match('^/reload') then bot_init() diff --git a/plugins/remind.lua b/plugins/remind.lua new file mode 100644 index 0000000..9618dfb --- /dev/null +++ b/plugins/remind.lua @@ -0,0 +1,102 @@ +database.reminders = database.reminders or {} + +local command = 'remind ' +local doc = [[``` +/remind +Repeats a message after a duration of time, in minutes. +```]] + +local triggers = { + '^/remind' +} + +local action = function(msg) + -- Ensure there are arguments. If not, send doc. + local input = msg.text:input() + if not input then + sendMessage(msg.chat.id, doc, true, msg.message_id, true) + return + end + -- Ensure first arg is a number. If not, send doc. + local duration = get_word(input, 1) + if not tonumber(duration) then + sendMessage(msg.chat.id, 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 + sendMessage(msg.chat.id, doc, true, msg.message_id, true) + return + end + -- Make a database entry for the group/user if one does not exist. + database.reminders[msg.chat.id_str] = database.reminders[msg.chat.id_str] or {} + -- Limit group reminders to 10 and private reminders to 50. + if msg.chat.type ~= 'private' and table_size(database.reminders[msg.chat.id_str]) > 9 then + sendReply(msg, 'Sorry, this group already has ten reminders.') + return + elseif msg.chat.type == 'private' and table_size(database.reminders[msg.chat.id_str]) > 49 then + 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 + } + table.insert(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 + sendReply(msg, output) +end + +local cron = function() + local time = os.time() + -- Iterate over the group entries in the reminders database. + for chat_id, group in pairs(database.reminders) do + local new_group = {} + -- Iterate over each reminder. + for i, 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 + local output = 'Reminder:\n"' .. reminder.message .. '"' + local res = sendMessage(chat_id, output, true) + -- If the message fails to send, save it for later. + if res then + reminder = nil + else + 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. + group = nil + database.reminders[chat_id] = new_group + -- Nullify the table if it is empty. + if #new_group == 0 then + database.reminders[chat_id] = nil + end + end +end + +return { + action = action, + triggers = triggers, + cron = cron, + command = command, + doc = doc +}