This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibDiscordWebhookSender.lua
91 lines (79 loc) · 3.96 KB
/
LibDiscordWebhookSender.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--[[
File name: LibDiscordWebhookSender.lua
Author: RadiatedExodus (RealEthanPlayz/RealEthanPlayzDev/ItzEthanPlayz_YT)
--]]
local serv = {
HttpService = game:GetService("HttpService");
}
local LibDiscordWebhookSender = {}
LibDiscordWebhookSender.__index = LibDiscordWebhookSender
LibDiscordWebhookSender.__metatable = "The metatable is locked"
LibDiscordWebhookSender.ClassName = "LibDiscordWebhookSender"
local function CheckHttpEnabled()
return pcall(function()
serv.HttpService:GetAsync("https://www.google.com")
return true
end)
end
--// function <boolean> LibDiscordWebhookSender:SendData()
function LibDiscordWebhookSender:SendData()
--// https://developer.roblox.com/en-us/api-reference/function/HttpService/RequestAsync
assert(CheckHttpEnabled(), "LibDiscordWebhookSender: Http requests are not enabled. Enable via game settings")
local sendsuccess, sendattempt, sendresult = false, 0, nil
sendsuccess, sendresult = pcall(function()
return serv.HttpService:RequestAsync({
Url = tostring(self.webhookurl);
Method = "POST";
Headers = { ["Content-Type"] = "application/json" };
Body = serv.HttpService:JSONEncode(self.webhookluatabledata)
})
end)
if sendsuccess and sendresult.Success == true then
return true
else
return error("LibDiscordWebhookSender: unable to send webhook data\nStatusCode: "..sendresult.StatusCode.."\nStatusMessage: "..sendresult.StatusMessage, 2)
end
end
--// function <LibDiscordWebhookSender> LibDiscordWebhookSender:SetUsername(newname: string)
function LibDiscordWebhookSender:SetUsername(newname: string)
assert(typeof(newname) == "string", [[LibDiscordWebhookSender: invalid argument #1 to 'SetUsername' (string expected, got ]]..typeof(newname)..[[)]])
self.webhookluatabledata.username = tostring(newname)
return self
end
--// function <LibDiscordWebhookSender> LibDiscordWebhookSender:SetAvatarUrl(newavatarurl: string)
function LibDiscordWebhookSender:SetAvatarUrl(newavatarurl: string)
assert(typeof(newavatarurl) == "string", [[LibDiscordWebhookSender: invalid argument #1 to 'SetAvatarUrl' (string expected, got ]]..typeof(newavatarurl)..[[)]])
self.webhookluatabledata.avatar_url = tostring(newavatarurl)
return self
end
--// function <LibDiscordWebhookSender> LibDiscordWebhookSender:SetAvatarUrl(newavatarurl: string)
function LibDiscordWebhookSender:SetContent(newcontent: string)
assert(typeof(newcontent) == "string", [[LibDiscordWebhookSender: invalid argument #1 to 'SetContent' (string expected, got ]]..typeof(newcontent)..[[)]])
self.webhookluatabledata.content = tostring(newcontent)
return self
end
--// function <LibDiscordWebhookSender> LibDiscordWebhookSender:AddEmbed(newembed: table)
function LibDiscordWebhookSender:AddEmbed(newembed: table)
assert(typeof(newembed) == "table", [[LibDiscordWebhookSender: invalid argument #1 to 'AddEmbed' (table expected, got ]]..typeof(newembed)..[[)]])
if newembed["ClassName"] == "LibDiscordEmbedCreator" then
table.insert(self.webhookluatabledata.embeds, #self.webhookluatabledata.embeds + 1, newembed.embedtable)
else
table.insert(self.webhookluatabledata.embeds, #self.webhookluatabledata.embeds + 1, newembed)
end
return self
end
return {
--// [constructor] function <LibDiscordWebhookSender> new(webhookurl: string)
new = function(webhookurl: string)
assert(typeof(webhookurl) == "string", [[LibDiscordWebhookSender: invalid argument #1 to 'new' (string expected, got ]]..typeof(webhookurl)..[[)]])
local self = setmetatable({}, LibDiscordWebhookSender)
self.webhookurl = webhookurl
self.webhookluatabledata = {
username = ""; --// Webhook username
avatar_url = ""; --// Webhook avatar
content = ""; --// Webhook message (max 2000 characters as defined by Discord)
embeds = {}; --// Webhook embeds
}
return self
end
}