Command Spam Prevention

Post Reply
Khonsu
Posts: 4
Joined: Mon May 02, 2022 2:29 am

Command Spam Prevention

Post by Khonsu »

Maybe I'm reinventing the wheel but I could not find an option like in mushclient that sends another command after 'x' number of similar commands. This is for some MUD's that kick you out after repeating a command 30 or 50 times etc. So I figured I would make an alias that captures anything sent and checks if it is the same.

This works fine except it will cause any other alias command to be repeated. Is there some way to write a check for another alias without having to write a bunch of if statements for each alias that might be used?

current alias code is:

pattern: (.+)

Code: Select all

currentCommand = matches[2]

if currentCommand == lastCommand then
  command_count(1)
else
  command_count(0)
end

lastCommand = currentCommand

send(matches[1],false)
command_count is a function:

Code: Select all

function command_count(counter)
  if commandCountKill == nil then
    commandCountKill = 1
  end --if
  
  if counter == 0 then
    commandCountKill = 0
  end --if
  
  commandCountKill = commandCountKill + counter

  if commandCountKill >= 28 then
   send("look")
   commandCountKill = 1
  end --if
end --fxn

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Command Spam Prevention

Post by Jor'Mox »

Instead of using an alias, you can create an event handler for the sysDataSendRequest event, and handle it that way. There is an example in the wiki, located here: https://wiki.mudlet.org/w/Manual:Lua_Fu ... urrentSend

Khonsu
Posts: 4
Joined: Mon May 02, 2022 2:29 am

Re: Command Spam Prevention

Post by Khonsu »

Thank you! That was exactly what I needed. Here is the new script:

Code: Select all

function command_count(event, command)
  if commandCountKill == nil then
    commandCountKill = 1
  end --if
  
  currentCommand = command

  if currentCommand == lastCommand then
    commandCountKill = commandCountKill + 1
  else
    commandCountKill = 0
  end

  lastCommand = command

  if commandCountKill >= 28 then
   send("look")
   commandCountKill = 0
  end --if
end --fxn

registerAnonymousEventHandler("sysDataSendRequest", "command_count")

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Command Spam Prevention

Post by Jor'Mox »

I rewrote some of your if statements just to highlight the sorts of things you can do with Lua to simplify your code.

Code: Select all

function command_count(event, command)
  commandCountKill = commandCountKill or 1
  
  currentCommand = command

  commandCountKill = currentCommand == lastCommand and commandCountKill + 1 or 0

  lastCommand = command

  if commandCountKill >= 28 then
   send("look")
   commandCountKill = 0
  end --if
end --fxn

registerAnonymousEventHandler("sysDataSendRequest", "command_count")

Post Reply