Using os.date to limit requests sent to a discord webhook

Hello!

I am currently working on making a discord webhook for premium features, which is going well so far! The problem is that users can send an infinite number of requests, I want to limit it to about 12 hours per request, how can I do this, and where do I start? I am familiar with datastores, it is just os.date I am unfamiliar with.

1 Like

Hia, nice seeing you here again~~

How would you feel about creating a datastore with the key being the current day of the year?
Each day the key would change.
Then to avoid datastore limits, whenever you send a webhook, cache the number of webhooks a player sent in a table and save the number in that table to the datastore when the player joins again.

Would look something like this:

local currentDate = os.date("!*t")

-- create a datastore key based on the current day of the year and the current year
-- this way the key will change daily
local datastoreName = "WebHooks"..currentDate.yday..currentDate.year 
local todaysDataStore = game:GetService("DatastoreService"):GetDatastore(datastoreName)

local numWebhooksPerPlayer = {}

game.Players.PlayerAdded:Connect(function(player)
	--TODO: Get the saved number of webhooks the player sent in todaysDataStore
	-- not going into detail bc you mentioned you already know a fair amount about datastore, let me know if you need help with this
	local numWebhooksSent = getNumWebHooksFromDataStore(player)

	-- save number of webhooks in a table while the player is in game
	numWebhooksPerPlayer[player.Name] = numWebhooksSent
end)

game.Players.PlayerRemoving:Connect(function(player)
	-- save webhooks to datastore
	UpdateToDatastore(numWebhooksPerPlayer[player.Name])
end

To stop players going over the limit, you’d now do something like this:

if (numWebhooksPerPlayer[player.Name] < 12) then
	sendWebHook();
	numWebhooksPerPlayer[player.Name] = numWebhooksPerPlayer[player.Name] + 1;
end
2 Likes