Webhook Module Issues

I’m trying to make a webhook module using OOP, but it’s not working.

Code:

local Webhook = {}
Webhook.__index = Webhook

local HttpService = game:GetService("HttpService")


local function CheckHttp()
	local HttpEnabled = pcall(function()
		HttpService:GetAsync("https://google.com")
	end)
	return HttpEnabled
end

assert(CheckHttp(), "Turn on HttpRequests in Game Settings!")

function Webhook.new(Url)
	local newWebhook = setmetatable({}, Webhook)
	newWebhook.Url = Url
	return newWebhook
end

function Webhook:Post(Data)
	local success, err = pcall(function()
		Data = HttpService:JSONEncode(Data)
		HttpService:PostAsync(self.Url, Data)
	end)
	if success then
		warn("Successfully Posted!")
	else
		warn("Error: "..err)
	end
end

function Webhook:Destroy()
	self.Url = nil
	self = nil
end

return Webhook

Code to test:

local HttpService = game:GetService("HttpService")


local Webhook = require(script.WebhookModule)

print("LUA")

local MyHook = Webhook.new("")

print("YA 2!")

game.Players.PlayerAdded:Connect(function(Player)
	print(Player.Name.." joined!")
	local Data = {
		["content"] = "",
		["embeds"] = {{
			["title"] = string.format("**Wassup, %s?**", Player.Name),
			["description"] = string.format("Wassup, %s?", Player.Name)
		}}
	}
	Data = HttpService:JSONEncode(Data)
	MyHook:Post(Data)
end)

The above code however, doesn’t work. No errors, and it prints the ‘LUA’ and ‘YA 2!’ fine.

Does it print the contents inside the function?

Could it because you’re Encoding it twice? You encode it before using the Post method, and then encode it again in the Post method

I tried removing the double encode before, it didn’t make a difference. Will still try it again

Nope, it just kinda “hangs” the script when I use Webhook.new()

Webhook Post returns a silent result. Meaning that your webhook is malformed. Return the result of the PostAsync. Once you get that result you’ll see what’s going on with the service and if the actual webhook was successful.

function Webhook:Post(Data)
	local success, data = pcall(function()
		Data = HttpService:JSONEncode(Data)
		return HttpService:PostAsync(self.Url, Data)
	end)
	if success then
		warn("Successfully Posted! Meta:", data)
	else
		warn("Error: "..err)
	end
end
1 Like

Unfortunately, it’s still not working.

You get no data/result from the pcall?
The result of the Post should tell you the problem.

image
I put this at the end of it, it doesn’t print.

That is because you dont return anything inside of your Post Function.

function Webhook:Post(Data)
	local success, data = pcall(function()
		Data = HttpService:JSONEncode(Data)
		return HttpService:PostAsync(self.Url, Data)
	end)

	return success, data
end

This will allow the person to see the data and the result.

image

image

I’m not sure I can understand that code, But yeah if you were to separate both of them into their correct scripts. See what that returns.

They’re separate scripts, and nothing prints.

then do

print(HttpService:PostAsync(self.Url, Data))

If it doesn’t print then it’s a pcall, service error.

im surprised you make a parameter using . and not :

It’s usually used in OOP because it’s not a method.

image

Doesn’t print.

Uhm why not? Whenever self us not required, . functions are used rather than :… or am I wrong somehow? (Im still new to OOP…) this was a question rather.

whats the Success, Data ? Does it return false, error?

I can’t check since for some reason, the print in my main script isn’t working.