Intercept Chat Commands

User avatar
octacian
Member
 
Posts: 408
Joined: Mon Dec 21, 2015 22:18
GitHub: octacian
IRC: octacian
In-game: octacian

Intercept Chat Commands

by octacian » Mon Jun 20, 2016 19:13

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!
God isn't dead!

My Coolest Mods:
MicroExpansion, Working Computers, Interchangeable Hands

Check out my YouTube channel! (octacian)
 

User avatar
BrandonReese
Member
 
Posts: 836
Joined: Wed Sep 12, 2012 00:44
GitHub: bremaweb
IRC: BrandonReese
In-game: BrandonReese

Re: Intercept Chat Commands

by BrandonReese » Mon Jun 20, 2016 20:34

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.
 

red-001
Member
 
Posts: 126
Joined: Tue Jan 26, 2016 20:15
GitHub: red-001
IRC: red-001

Re: Intercept Chat Commands

by red-001 » Fri Jun 24, 2016 11:21

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
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron