Hourly Webhook (Roblox-Discord)

Hi,
I am trying to find a way to make it so a webhook is posted once to discord, the issue is that there will be many servers and it’s a matter of whoever posts the webhook first then other servers cannot post no more, but i don’t really know an effective method to take and do it, can somebody help me?

What im trying to achieve:
-A webhook system that posts hourly
-Prevent multi servers posting it many times (We’ll need some sort of DB, but idk what method to use)
-After hours time is up (3600) posts again, and repeat.

Here is some code:

local currentDay --current day variable
local HourWait = 1
local HourOffset = 1 --1 hour offset
local Offset = (60 * 60 * HourOffset)
local MainBanner
while true do
        wait(10)
	local day = math.floor((os.time() + Offset) / (60 * 60 * 1))
	local Time = (math.floor(os.time())) - Offset

	local BannerKey = "Banner"
	local WebhookPosted
	local TimeNow = os.time()
	local BannerStats
	local BannerTime

	local success, err = pcall(function() --Gets key and data, or set the data
		BannerStats = BannerDSS:GetAsync(BannerKey) or {BannerCD = false, Timer = 3600} 
	end)

	if BannerStats then
		WebhookPosted = BannerStats.BannerCD --Getting CD		
		print(BannerStats.Timer)
		if BannerStats.Timer == nil then
			BannerTime = 3600
			else
			BannerTime = BannerStats.Timer
		end

	else
		BannerDSS:SetAsync(BannerKey, {BannerCD = false, Timer = 3600})
	end


	if day ~= currentDay  then 
		currentDay = day
	end
	
	if (os.time() - BannerTime) >= 3600 then
		WebhookPosted = false
	end
	if not WebhookPosted then 
		local Data = {
			--[[Webhook content here]]--
		}
		HTTPService:PostAsync(WebHookURL, HTTPService:JSONEncode(Data)) --Sending wehook to discord
		local success, err = pcall(function()
			BannerDSS:SetAsync(BannerKey, {BannerCD = true, Timer = os.time()})
		end)
	end
end			
1 Like

While I’m not exactly sure how to solve this problem, you could definitely look into using MessagingService to communicate between every server.

I think you’re over-complicating a bit.

You could have a data store save an initial time, then subtract the initial time from os.date(), if it’s greater than 3600 then send the webhook request. Then after the webhook, set the data store’s value to os.time().

As for limiting it to one server per request, you would have to use messagingservice or datastores to determine whether a webhook was sent already or not.

Im on mobile atm but I can make an example for you tomorrow if you haven’t figured it out.

Well it is a matter of what you’re trying to post, if two servers start at the same time that could make collisions and well make it post twice.
I personally would use an external server, and handle it from there because I know it will always be 1 request.

1 Like

I humbly ask, please can you show me an example

Sorry, was busy today until now.

I’m thinking something like this could work, you wouldn’t have to use any external programs like a server to monitor whether it was posted or not. But in exchange, it won’t strictly be posted every hour unfortunately. Your best bet would be to use an external server but if it’s not a possibility then here’s something that might work.

local datastoreService = game:GetService('DataStoreService')

local webhookDatastore = datastoreService:GetDataStore('WebookDatastore')

local dateInfo = os.date('*t')
local hour = dateInfo.hour -- hour of current day
local day = dateInfo.day -- day
local year = dateInfo.year -- year

while true do
    local key = hour..'/'..day..'/'..year -- create a new key for the current hour of the current day of the current year
    local data -- check if it was already posted
    local success, errorMessage = pcall(function() 
        data = webhookDatastore:GetAsync(key) -- check if is posted for the current hour
    end)
    data = data or {isPosted = false, timestamp = os.time()} -- if data is nil then set the data variable to a default value which will contain this info
    local isPostedCurrentHour = data.isPosted
    if not isPostedCurrentHour then -- if it's not posted for the current hour then
        -- post here
        data.isPosted = true
        local success, errorMessage = pcall(function()
            webhookDatastore:UpdateAsync(key, function(oldData) -- update for the current hour
                return data
            end)
        end)
    end
    dateInfo = os.date('*t') -- override previous variables
    hour = dateInfo.hour
    day = dateInfo.day
    year = dateInfo.year
    wait(3600) -- wait an hour
end

Thank you ill try this solution and get back to you

Thank you! It’s works and a lot better

1 Like