0

I have the following auto-responder on my bot

on *:TEXT:*sparky*:*: { msg # $read(scripts/name-responses.txt) }
on *:ACTION:*sparky*:*: { msg # $read(scripts/name-responses.txt) }

I wanted to know how can I tell write a code, I'm guessing with an IF statement, that if a user types sparky more than twice that the user gets ignored for 120 seconds. This way, my bot doesn't flood the chat due to the auto-responder feature.

Any help would be appreciated!

Luis M
  • 104
  • 1
  • 11

1 Answers1

-1

I would recommend keeping track of all users that have used the command, and when they have last used it. This can easily be done by saving all data in an INI file.

You can save this information by using the writeini command. To write the data to this file, use something along the lines of the following:

writeini sparky.ini usage $nick $ctime

$ctime will evaluate to the number of seconds elapsed since 1970/01/01. This is generally the way to compare times of events.

Once a user triggers your script again, you can read the value from this INI file and compare it to the current time. If the difference between the times is less than 10 seconds (for example), it can send the command and then ignore them for 120 seconds. You would read the value of their last usage using:

$readini(sparky.ini, n, usage, $nick)

Your final script could look like something along the lines of the following script. I've moved the functionality to a separate alias (/triggerSparky <nick> <channel>) to avoid identical code in the on TEXT and on ACTION event listeners.

on *:TEXT:*sparky*:#: {
  triggerSparky
}

on *:ACTION:*sparky*:#: {
  triggerSparky
}

alias triggerSparky {      
  ; Send the message
  msg $chan $read(scripts/name-responses.txt, n)

  if ($calc($ctime - $readini(sparky.ini, n, usage, $nick)) < 10) {
    ; This user has recently triggered this script (10 seconds ago), ignore him for 120 seconds

    ignore -u120 $nick
    remini sparky.ini usage $nick
  }

  else {
    writeini sparky.ini usage %nick $ctime
  }
}

Of course, a slightly easier way to achieve a similar result is by simply ignoring them for a predefined time without saving their data in an INI file. This would stop you from checking whether they have triggered twice recently, but it would be a good way to only allow them to trigger it once per two minutes, for example.

Patrickdev
  • 2,341
  • 1
  • 21
  • 29
  • -1, there are a few issues but the most important one is the missing 'n' option to $readini()!!! That's inexcusable. Additionally, it's a bad idea to use $$ (i.e. required values) in aliases that are called from different places as they make debugging incredibly hard by unexpectedly halting the scripting engine. Thirdly, identifiers like $chan and $nick are inherited automatically by aliases that are called directly from events that have those identifiers. Passing them as arguments and storing them in variables makes code more complex for no good reason. Particularly in this very case. – Wiz Jun 29 '13 at 00:33
  • I forgot to mention the missing 'n' option from $read() as well. – Wiz Jun 29 '13 at 00:39
  • guess i'll look for an alternative then – Luis M Jul 02 '13 at 14:28
  • Why look for an alternative if you could simply apply Wiz' suggestions to the provided answer which apparently met your expectations? Furthermore, I agree that the 'n' switch should have been added to `$read()` to ensure safety as I'm unaware of its contents, but the rest won't affect the safety or usability in any way. Regardless, I've edited my answer. – Patrickdev Jul 02 '13 at 15:13