diff --git a/bot.lua b/bot.lua index a2918e7..edf51f2 100755 --- a/bot.lua +++ b/bot.lua @@ -40,13 +40,15 @@ on_msg_receive = function(msg) -- The fn run whenever a message is received. if msg.date < os.time() - 5 then return end -- Do not process old messages. if not msg.text then msg.text = msg.caption or '' end - msg.chat.id_str = tostring(msg.chat.id) - msg.from.id_str = tostring(msg.from.id) - msg.text_lower = msg.text:lower() - for i,v in ipairs(plugins) do for k,w in pairs(v.triggers) do - if string.match(msg.text_lower, w) then + if string.match(msg.text:lower(), w) then + + -- a few shortcuts + msg.chat.id_str = tostring(msg.chat.id) + msg.from.id_str = tostring(msg.from.id) + msg.text_lower = msg.text:lower() + local success, result = pcall(function() return v.action(msg) end) diff --git a/config.lua b/config.lua index f2f5e01..33f4ff8 100755 --- a/config.lua +++ b/config.lua @@ -65,7 +65,7 @@ telegram.me/otouto plugins = { 'blacklist.lua', 'floodcontrol.lua', - 'admin.lua', + 'control.lua', 'about.lua', 'ping.lua', 'whoami.lua', diff --git a/plugins/moderation.lua b/plugins/moderation.lua index 3b97aef..f94d2f3 100755 --- a/plugins/moderation.lua +++ b/plugins/moderation.lua @@ -7,10 +7,10 @@ '^/modhelp[@'..bot.username..']*$', '^/modlist[@'..bot.username..']*$', '^/modcast[@'..bot.username..']*', - '^/add[@'..bot.username..']*$', - '^/remove[@'..bot.username..']*$', - '^/promote[@'..bot.username..']*$', - '^/demote[@'..bot.username..']*', + '^/modadd[@'..bot.username..']*$', + '^/modrem[@'..bot.username..']*$', + '^/modprom[@'..bot.username..']*$', + '^/moddem[@'..bot.username..']*', '^/modkick[@'..bot.username..']*', '^/modban[@'..bot.username..']*', } @@ -94,7 +94,7 @@ local commands = { end, - ['^/add[@'..bot.username..']*$'] = function(msg) + ['^/modadd[@'..bot.username..']*$'] = function(msg) if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -112,7 +112,7 @@ local commands = { end, - ['^/remove[@'..bot.username..']*$'] = function(msg) + ['^/modrem[@'..bot.username..']*$'] = function(msg) if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -130,7 +130,7 @@ local commands = { end, - ['^/promote[@'..bot.username..']*$'] = function(msg) + ['^/modprom[@'..bot.username..']*$'] = function(msg) local moddat = load_data('moderation.json') @@ -164,7 +164,7 @@ local commands = { end, - ['^/demote[@'..bot.username..']*'] = function(msg) + ['^/moddem[@'..bot.username..']*'] = function(msg) local moddat = load_data('moderation.json') @@ -279,7 +279,7 @@ local commands = { local action = function(msg) for k,v in pairs(commands) do - if string.match(msg.text, k) then + if string.match(msg.text_lower, k) then local output = v(msg) if output then sendReply(msg, output) diff --git a/utilities.lua b/utilities.lua index 7cf5c5f..b7d6183 100755 --- a/utilities.lua +++ b/utilities.lua @@ -1,28 +1,14 @@ -- utilities.lua -- Functions shared among plugins. -function get_word(str, idx) -- get the indexed word in a string +function get_word(s, i) -- get the indexed word in a string - local str = str:gsub('^%s*(.-)%s*$', '%1') - - str = string.gsub(str, '\n', ' ') - if not string.find(str, ' ') then - if idx == 1 then - return str - else - return false - end + local t = {} + for w in s:gmatch('%g+') do + table.insert(t, w) end - str = str .. ' ' - if idx ~= 1 then - for i = 2, idx do - str = string.sub(str, string.find(str, ' ') + 1) - end - end - str = str:sub(1, str:find(' ')) - - return str:sub(1, -2) + return t[i] or false end