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.
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
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.
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.