Rate Limiting

Hello everybody,

I've noticed a bunch of us are occasionally super excited for announcing
something on party and kind of end up spamming the heck out of it. Like,
blocking, has prismatic barrier!, or even just wanting somebody
burnt real crisp and toasty XD

Here's a rate limiter script to avoid repeating stuff before a defined
interval has passed. The default is 1 second but feel free to add more
definitions to the rateLimiter.interval table.

Script: rateLimiter
------------------------------------------------------------------------
rateLimiter = rateLimiter or {}
rateLimiter.last = rateLimiter.last or {}

rateLimiter.interval = {
    default = 1.0,
    blocking = 0.3,
}

rateLimiter.myTime = function()
    local time = getTime()
    local seconds = time.hour*3600 + time.min * 60 + time.sec + time.msec/1000
    return seconds
end

rateLimiter.Tried = function(action)
    local currenttime = rateLimiter.myTime()
    rateLimiter.last[action] = currenttime
end

rateLimiter.CanTry = function(action)
    local currenttime = rateLimiter.myTime()
    local interval = rateLimiter.interval[action]
    
    if not(rateLimiter.last[action]) then
        rateLimiter.last[action] = 0
    end
    
    if not(interval) then
        interval = rateLimiter.interval.default
    end

    return (currenttime - rateLimiter.last[action]) > interval
end
------------------------------------------------------------------------

How to use:

In your trigger/alias where you want to rate limit something, a pattern
like this applies:

if CanTry("pt-announce-prismatic") then
    send("PT Santar has prismatic barrier!")
    Tried("pt-announce-prismatic")
end

So just wrap anything you need rate limited in a CanTry()..Tried() block
and you're good to go! I hope this is useful, let there be less spam :-)

Love,
Ada
Sign In or Register to comment.