blacklist.lua rewritten to remove toggle and support multiple arguments.

The /reload command no longer has stupid, undocumented behavior (though there is now an opt-in for such
stupid, undocumented behavior for those of you who like stupid, undocumented behavior).
Slight modification to reactions.lua to prevent accidents.
New plugin: rmspic.lua. why
This commit is contained in:
topkecleon 2016-07-13 21:57:23 -04:00
parent 9ebdbd9d3c
commit 67f7d7e12f
5 changed files with 135 additions and 32 deletions

View File

@ -95,7 +95,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
end
for _, plugin in ipairs(self.plugins) do
for _, trigger in pairs(plugin.triggers or {}) do
for _, trigger in ipairs(plugin.triggers or {}) do
if string.match(msg.text_lower, trigger) then
local success, result = pcall(function()
return plugin.action(self, msg, config)

View File

@ -16,36 +16,96 @@ blacklist.triggers = {
}
function blacklist:action(msg, config)
if self.database.blacklist[tostring(msg.from.id)] then return end
if self.database.blacklist[tostring(msg.chat.id)] then return end
if not msg.text:match('^'..config.cmd_pat..'blacklist') then return true end
if msg.from.id ~= config.admin then return end
local target = utilities.user_from_message(self, msg)
if target.err then
utilities.send_reply(self, msg, target.err)
if self.database.blacklist[tostring(msg.from.id)] or self.database.blacklist[tostring(msg.chat.id)] then
return
end
if tonumber(target.id) < 0 then
target.name = 'Group'
if not (
msg.from.id == config.admin
and (
msg.text:match('^'..config.cmd_pat..'blacklist')
or msg.text:match('^'..config.cmd_pat..'unblacklist')
)
) then
return true
end
if self.database.blacklist[tostring(target.id)] then
self.database.blacklist[tostring(target.id)] = nil
utilities.send_reply(self, msg, target.name .. ' has been removed from the blacklist.')
if config.drua_block_on_blacklist then
require('drua-tg').unblock(target.id)
end
local targets = {}
if msg.reply_to_message then
table.insert(targets, {
id_str = tostring(msg.reply_to_message.from.id),
name = utilities.build_name(msg.reply_to_message.from.first_name, msg.reply_to_message.from.last_name)
})
else
self.database.blacklist[tostring(target.id)] = true
utilities.send_reply(self, msg, target.name .. ' has been added to the blacklist.')
if config.drua_block_on_blacklist then
require('drua-tg').block(target.id)
local input = utilities.input(msg.text)
if input then
for _, user in ipairs(utilities.index(input)) do
if self.database.users[user] then
table.insert(targets, {
id = self.database.users[user].id,
id_str = tostring(self.database.users[user].id),
name = utilities.build_name(self.database.users[user].first_name, self.database.users[user].last_name)
})
elseif tonumber(user) then
local t = {
id_str = user,
id = tonumber(user)
}
if tonumber(user) < 0 then
t.name = 'Group (' .. user .. ')'
else
t.name = 'Unknown (' .. user .. ')'
end
table.insert(targets, t)
elseif user:match('^@') then
local u = utilities.resolve_username(self, user)
if u then
table.insert(targets, {
id = u.id,
id_str = tostring(u.id),
name = utilities.build_name(u.first_name, u.last_name)
})
else
table.insert(targets, { err = 'Sorry, I do not recognize that username ('..user..').' })
end
else
table.insert(targets, { err = 'Invalid username or ID ('..user..').' })
end
end
else
utilities.send_reply(self, msg, 'Please specify a user or users via reply, username, or ID, or a group or groups via ID.')
return
end
end
local output = ''
if msg.text:match('^'..config.cmd_pat..'blacklist') then
for _, target in ipairs(targets) do
if target.err then
output = output .. target.err .. '\n'
elseif self.database.blacklist[target.id_str] then
output = output .. target.name .. ' is already blacklisted.\n'
else
self.database.blacklist[target.id_str] = true
output = output .. target.name .. ' is now blacklisted.\n'
if config.drua_block_on_blacklist and target.id > 0 then
require('drua-tg').block(target.id)
end
end
end
elseif msg.text:match('^'..config.cmd_pat..'unblacklist') then
for _, target in ipairs(targets) do
if target.err then
output = output .. target.err .. '\n'
elseif not self.database.blacklist[target.id_str] then
output = output .. target.name .. ' is not blacklisted.\n'
else
self.database.blacklist[target.id_str] = nil
output = output .. target.name .. ' is no longer blacklisted.\n'
if config.drua_block_on_blacklist and target.id > 0 then
require('drua-tg').unblock(target.id)
end
end
end
end
utilities.send_reply(self, msg, output)
end
end
return blacklist
return blacklist

View File

@ -28,9 +28,11 @@ function control:action(msg, config)
package.loaded['otouto.bindings'] = nil
package.loaded['otouto.utilities'] = nil
package.loaded['config'] = nil
if msg.text_lower:match('%+config') then for k, v in pairs(require('config')) do
config[k] = v
end end
if not msg.text_lower:match('%-config') then
for k, v in pairs(require('config')) do
config[k] = v
end
end
bot.init(self, config)
utilities.send_reply(self, msg, 'Bot reloaded!')
elseif msg.text_lower:match('^'..cmd_pat..'halt') then

View File

@ -29,10 +29,17 @@ function reactions:init(config)
-- Generate a "help" message triggered by "/reactions".
help = 'Reactions:\n'
reactions.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('reactions').table
local username = self.info.username:lower()
for trigger,reaction in pairs(mapping) do
help = help .. '' .. config.cmd_pat .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, config.cmd_pat..trigger)
table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..self.info.username:lower())
table.insert(reactions.triggers, '^'..config.cmd_pat..trigger)
table.insert(reactions.triggers, '^'..config.cmd_pat..trigger..'@'..username)
table.insert(reactions.triggers, config.cmd_pat..trigger..'$')
table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'$')
table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger)
table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger..'@'..username)
table.insert(reactions.triggers, config.cmd_pat..trigger..'\n')
table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'\n')
end
end

34
otouto/plugins/rmspic.lua Normal file
View File

@ -0,0 +1,34 @@
local https = require('ssl.https')
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
local rms = {}
function rms:init(config)
rms.BASE_URL = 'https://rms.sexy/img/'
rms.LIST = {}
local s, r = https.request(rms.BASE_URL)
if r ~= 200 then
print('Error connecting to rms.sexy.\nrmspic.lua will not be enabled.')
return
end
for link in s:gmatch('<a href=".-%.%a%a%a">(.-)</a>') do
table.insert(rms.LIST, link)
end
rms.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('rms').table
end
function rms:action(msg, config)
bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'upload_photo' })
local choice = rms.LIST[math.random(#rms.LIST)]
local filename = '/tmp/' .. choice
local image_file = io.open(filename)
if image_file then
image_file:close()
else
utilities.download_file(rms.BASE_URL .. choice, filename)
end
bindings.sendPhoto(self, { chat_id = msg.chat.id }, { photo = filename })
end
return rms