Preventing Webhook Spam

I want to create a webhook that’ll log all commands being used in game into Discord with spam prevention so user can’t just spam “cmds” and fill up the channel.
I’m not really sure how to do that.
Nothing yet, most of the DevForum posts I looked at yielded no answers or were unrelated.

My code is in Basic Admin Essentials made by r_r. I’ll put the code but obviously remove the webhook.

Lua Code
local function onChatted(Message, Player)
	local Command = getCommand(Message,Player)
	if not Command then return end
	local Arguments = {}
	table.insert(Arguments, Player)
	for Segment in string.gmatch(Command, "%S+") do
		if not Arguments[2] then
			table.insert(Arguments,Segment:lower())
		else
			table.insert(Arguments,Segment)
		end
	end
	if Arguments[2] ~= nil then
		for a,b in next,Commands do
			local tempPerm = returnPermission(Player)
			if b[1]:lower() == Arguments[2]:lower() and tempPerm >= b[4] then
				if (tempPerm == 0 and b[4] == 0 and not sysTable.publicCommands) and b[3] ~= Funcs.Donor  then
					return
				end
				local Success,errorMessage = pcall(function()
					b[3](Arguments)
				end)
				if errorMessage then
					addLog(sysTable.debugLogs,'"'..Arguments[2]..'"| Error: '..errorMessage)
					essentialsEvent:FireClient(Player,'Message','Function Error','Name: "'..Arguments[2]..'"\n'..errorMessage)
				elseif Success then
					local cleanedMessage

					local Cleaned,newData = cleanUserData(Message,Player,false)
					if Cleaned and newData then
						cleanedMessage = newData
					elseif not Cleaned then
						cleanedMessage = newData
					end

					local webhook = "webhookwashere"
					local http = game:GetService("HttpService")
					local Data = http:JSONEncode({
						["username"] = "FBI | Admin Logger";
						["content"] = string.gsub("> # New log: \n**Username:** "..Player.Name.."\n**UserID:** "..Player.UserId.."\n**Command:** "..Message, "@", "(@)");
					})


					http:PostAsync(webhook, Data)

					if cleanedMessage then
						pluginEvent:Fire("Admin Logs",{Player,newData})
					else
						addLog(sysTable.Logs,{Sender = Player,Bypass = true,Data = '(super safechat) executed "'..tostring(b[1] or "???")..'"',Tag = true})
						return
					end

					addLog(sysTable.Logs,{Sender = Player,Data = cleanedMessage}) 
				end
				return
			end
		end
	end
end

Well, I guess you can put a cooldown, you can do that like this:

task.delay(5, http.PostAsync, webhook, Data)

If you’re looking for another alternative, you can just make specific commands like :cmds or commands that don’t affect others not be logged.

1 Like

Hey, I tried your solution but I found a few errors with them.

  1. lua task.delay(5, http.PostAsync, webhook, Data) should be lua task.delay(5, http.PostAsync(webhook, Data))

  2. When I fixed the first error, I got the error ServerScriptService.Basic Admin Essentials 2.0.MainModule:2600: invalid argument #2 to 'delay' (function or thread expected) (As seen in image.)

1 Like

Followup: Managed to solve the error by making Http:PostAsync(webhook, Data) into local function PostHttpMessage() task.wait(1) http:PostAsync(webhook,Data) end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.