Page 1 of 1

Intercept Chat Commands

PostPosted: Mon Jun 20, 2016 19:13
by octacian
How can I intercept chat commands before they run? I am trying to complete a chat filter, but would like to filter other methods of communication such as /msg commands. However, when a message is sent, my filter does not seem to be run at all. If it is, the command is running before the filter rather than after making it useless.

I would also like to edit the /ban and /shutdown commands to be more extensive and include several options. I could just create a new command, but it would be far more convenient to intercept the commands and then run the needed function as I don't need to change any privileges, and can do that through the function anyway.

My basic filter function looks like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- if warnable word is used
for i, word in ipairs(WARNABLE) do
  minetest.register_on_chat_message(function(name, message)
    local msg = message:lower()
    -- ignore commands
    if message:sub(1, 1) ~= "/" then
      -- filter for word(s)
      if msg:find(word.warn) ~= nil then
        -- warn player
        minetest.chat_send_player(name, 'Please do not use emotional words, including "'..word.warn..'," in the chat.')
        return true -- prevent message from showing in chat
      end
    end
  end)
end


I also have two other tables and for loops to register words that kick or ban the player (with xban2) using the same basic method.

Thanks!

Re: Intercept Chat Commands

PostPosted: Mon Jun 20, 2016 20:34
by BrandonReese
Look at minetest.register_on_chat_message and I think if you return false in your callback function the chat message won't be sent out.

Re: Intercept Chat Commands

PostPosted: Fri Jun 24, 2016 11:21
by red-001
You could do something like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local banned_word = "test"
minetest.chatcommands["msg"].func = function(name, param)
      local sendto, message = param:match("^(%S+)%s(.+)$")
      if not sendto then
         return false, "Invalid usage, see /help msg."
      end
      if not core.get_player_by_name(sendto) then
         return false, "The player " .. sendto
               .. " is not online."
      end
      if message:find(banned_word) then
         return false, "Please do not use banned words, including '"..banned_word.."', in the chat."
      end
      core.log("action", "PM from " .. name .. " to " .. sendto
            .. ": " .. message)
      core.chat_send_player(sendto, "PM from " .. name .. ": "
            .. message)
      return true, "Message sent."
end