module AiRateLimit
Constants
- DEFAULT_COOLDOWN_SEC
- KEY
- PLUGIN
Public Instance Methods
Source
# File lib/ai/rate_limit.rb, line 48 def cooldown_message(cooldown_sec: DEFAULT_COOLDOWN_SEC) "Slow down — wait #{cooldown_sec}s before another !ai request." end
Source
# File lib/ai/rate_limit.rb, line 23 def rate_limited?(network:, channel:, nick:, cooldown_sec: DEFAULT_COOLDOWN_SEC, store: RabbotDb, now: Time.now) network = store.normalize_network(network) channel = store.normalize_channel(channel) nick = store.normalize_nick(nick) return false if network.empty? || channel.empty? || nick.empty? return false if RabbotMetaAiCooldown.exempt?(network: network, channel: channel, nick: nick, store: store) limits = Hash(AiPluginStorage.get_json(key: KEY, network: network, channel: channel, default: {}, store: store)) last_at = store.parse_time(limits[storage_key(channel, nick)]) return false unless last_at (now - last_at) < cooldown_sec end
Source
# File lib/ai/rate_limit.rb, line 37 def record_request(network:, channel:, nick:, store: RabbotDb, now: Time.now) network = store.normalize_network(network) channel = store.normalize_channel(channel) nick = store.normalize_nick(nick) return if network.empty? || channel.empty? || nick.empty? limits = Hash(AiPluginStorage.get_json(key: KEY, network: network, channel: channel, default: {}, store: store)) limits[storage_key(channel, nick)] = store.iso_time(now) AiPluginStorage.set_json(key: KEY, network: network, channel: channel, value: limits, store: store) end
Source
# File lib/ai/rate_limit.rb, line 19 def storage_key(channel, nick) "#{channel}/#{nick}" end