From cdebb495ceb1dc77ea45089246f44e9b5e008d7e Mon Sep 17 00:00:00 2001 From: Norberto A Date: Sun, 1 Nov 2020 16:53:00 +0000 Subject: [PATCH] Rewrite --- .gitignore | 7 ++ Example.lua | 21 ---- RoHook.lua | 205 -------------------------------------- src/Classes/RichEmbed.lua | 76 ++++++++++++++ src/RoHook.lua | 39 ++++++++ src/test.server.lua | 5 + 6 files changed, 127 insertions(+), 226 deletions(-) create mode 100644 .gitignore delete mode 100644 Example.lua delete mode 100644 RoHook.lua create mode 100644 src/Classes/RichEmbed.lua create mode 100644 src/RoHook.lua create mode 100644 src/test.server.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e45f2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.vscode + +# selene +*.toml + +# rojo +default.project.json \ No newline at end of file diff --git a/Example.lua b/Example.lua deleted file mode 100644 index 7cc5c74..0000000 --- a/Example.lua +++ /dev/null @@ -1,21 +0,0 @@ --- Script type: Script --- Writen by github.com/Limesey, @Norbunny on Roblox. - - -local roHook = require(script.Parent.RoHook) - -local message = roHook.newMessage() -e = message.addEmbed("Title", "Description") -e.addField("Name", "Value") -e.setAuthor("Norbunny","https://www.roblox.com/headshot-thumbnail/image?userId=76860346&width=420&height=420&format=png", "https://www.roblox.com/headshot-thumbnail/image?userId=76860346&width=420&height=420&format=png") -e.setThumbnail("https://www.roblox.com/headshot-thumbnail/image?userId=76860346&width=420&height=420&format=png") -e.setImage("https://www.roblox.com/headshot-thumbnail/image?userId=76860346&width=420&height=420&format=png") -e.setTimestamp() -e.setFooter("Developed by Norbunny", "https://www.roblox.com/headshot-thumbnail/image?userId=76860346&width=420&height=420&format=png") -e.setColor(Color3.new(0, 255, 0)) - -local success, err = roHook:send(message) - -if(not success) then - warn(err) -end diff --git a/RoHook.lua b/RoHook.lua deleted file mode 100644 index f8d7de2..0000000 --- a/RoHook.lua +++ /dev/null @@ -1,205 +0,0 @@ --- Script type: Module --- Writen by github.com/Limesey, @Norbunny on Roblox. - --- services -local httpService = game:GetService("HttpService") - -local module = {} - -module.webhookConfig = { - webhookUrl = "", - overrideUsername = false, - overrideAvatar = false, - - username = nil, - avatar = nil -} - -local function getDate() - local date = os.date("!*t") - - local function format(i) - if(string.len(i) == 1) then - return string.format("0%s", i) - else - return i - end - end - - local str = string.format("%s-%s-%sT%s:%s:%s.000Z", date.year, date.month, date.day, format(date.hour), format(date.min), format(date.sec)) - return str -end - -local function rgbToDec(rgb) - local rgbTotal = rgb.R * 65536 + rgb.G * 256 + rgb.B - - return rgbTotal -end - - -local function getUsername() - local config = module.webhookConfig - - if(config.overrideUsername) then - return config.username - end -end - -local function getAvatar() - local config = module.webhookConfig - - if(config.overrideAvatar) then - return config.avatar - end -end - -function module.newMessage(message) - - if(message and string.len(message) > 2000) then - warn("A message cannot be longer than 2000 characters.") - message = string.sub(message, 1, 2000) - end - - local content = { - content = message, - embeds = {}, - username = getUsername() or "", - avatar_url = getAvatar() or "", - } - - function content.setWebhookUsername(name) - if(not name) then error("Name cannot be nil.") end - - content.username = name - end - - function content.setWebhookAvatar(url) - if(not url) then error("URL cannot be nil.") end - - content.avatar_url = url - end - - function content.addEmbed(title, description) - if(title and string.len(title) > 256) then - warn("Title cannot be longer than 265 characters.") - title = string.sub(title, 1, 256) - end - - if(description and string.len(description) > 2048) then - warn("Description cannot be longer than 2048 characters.") - description = string.sub(description, 1, 2048) - end - - local embed = { - title = title, - description = description, - color = 0, - fields = {}, - thumbnail = { - url= "" - }, - image= { - url = "" - }, - author = { - name = "", - url = "", - icon_url = "" - }, - footer = { - text = "", - icon_url = "" - }, - timestamp = "" --"YYYY-MM-DDTHH:MM:SS.MSSZ" - } - - table.insert(content.embeds, embed) - - function embed.setColor(rgb) - embed.color = rgbToDec(rgb) - end - - function embed.addField(name, value, inline) - if(not name or not value) then error("Name and value cannot be nil") end - - if(string.len(name) > 256) then - warn("Field name cannot be longer than 256 characters.") - name = string.sub(name, 1, 256) - end - - if(string.len(value) > 1024) then - warn("Field value cannot be longer than 1024 characters.") - value = string.sub(value, 1, 1024) - end - - inline = inline or false - - local field = { - name = name, - value = value, - inline = inline - } - - table.insert(embed.fields, field) - return field - end - - function embed.setAuthor(name, url, icon_url) - if(not name and icon_url) then error("Name cannot be nil") end - - if(string.len(name) > 256) then - warn("Author name cannot be longer than 256 characters.") - name = string.sub(name, 1, 256) - end - - embed.author.name = name - embed.author.url = url - embed.author.icon_url = icon_url - end - - function embed.setThumbnail(url) - if(not url) then error("URL cannot be nil") end - - embed.thumbnail.url = url - end - - function embed.setImage(url) - if(not url) then error("URL cannot be nil") end - - embed.image.url = url - end - - function embed.setFooter(text, url) - if(not text) then error("Text cannot be nil!") end - - if(string.len(text) > 2048) then - warn("Footer text cannot be longer than 2048 characters.") - text = string.sub(text, 1, 2048) - end - - embed.footer.text = text - embed.footer.icon_url = url - end - - function embed.setTimestamp() - embed.timestamp = getDate() - end - - return embed - end - - return content -end - -function module:send(content) - - content = httpService:JSONEncode(content) - - local success, data = pcall(function() - return httpService:PostAsync(module.webhookConfig.webhookUrl, content) - end) - - return success, data -end - -return module diff --git a/src/Classes/RichEmbed.lua b/src/Classes/RichEmbed.lua new file mode 100644 index 0000000..e185102 --- /dev/null +++ b/src/Classes/RichEmbed.lua @@ -0,0 +1,76 @@ +local RichEmbed = {} +RichEmbed.ClassName = "RichEmbed" +RichEmbed.__index = RichEmbed + +function RichEmbed.new(title, description) + -- Title is limited to 256 characters + -- Description is limited to 2048 characters + + if title then + assert(string.len(title) <= 256, "Title cannot be longer than 256 characters.") + end + + if description then + assert(string.len(description) <= 2048, "Description cannot be longer than 2048 characters.") + end + + local self = { + title = title, + description = description + } + + setmetatable(self, RichEmbed) + + return self +end + +function RichEmbed:setTimestmap() +end + +function RichEmbed:setColor() +end + +function RichEmbed:setFooter(text, iconUrl) + -- footer.text is limited to 2048 characters +end + +function RichEmbed:setImage() +end + +function RichEmbed:setThumbnail() +end + +function RichEmbed:setAuthor() + -- author.name is limited to 256 characters +end + +function RichEmbed:addField(name, value, inline) + -- 25 fields at most + -- feld.name is limited to 256 characters + -- field.value is limited to 1024 + + if name then + assert(string.len(name) <= 256, "Name cannot be longer than 256 characters.") + end + + if value then + assert(string.len(value) <= 1024, "Value cannot be longer than 1024 characters.") + end + + local fields = self.fields or {} + print(#fields) + assert(#fields + 1 <= 25, "RichEmbed cannot contain more than 25 fields.") + + local field = { + name = name, + value = value, + inline = inline + } + + table.insert(fields, #fields + 1, field) + self.fields = fields + + return field +end + +return RichEmbed \ No newline at end of file diff --git a/src/RoHook.lua b/src/RoHook.lua new file mode 100644 index 0000000..0d6581e --- /dev/null +++ b/src/RoHook.lua @@ -0,0 +1,39 @@ +-- Norbunny +-- November 1, 2020 + +local httpService = game:GetService("HttpService") + +local RoHook = {} +RoHook.__index = RoHook + +function RoHook.create(url, username, avatar) + local hook = {} + + setmetatable(hook, RoHook) + + hook.avatarUrl = avatar or "" + hook.username = username or "" + hook.url = url + + return hook +end + +function RoHook:setUsername() + -- +end + +function RoHook:setAvatar(url) +end + +function RoHook.send(data) + local request = nil + + if data.ClassName == "RichEmbed" then + print("RichEmbed") + + elseif data.ClassName == "Message" then + print("Message") + end +end + +return RoHook \ No newline at end of file diff --git a/src/test.server.lua b/src/test.server.lua new file mode 100644 index 0000000..ab41c4b --- /dev/null +++ b/src/test.server.lua @@ -0,0 +1,5 @@ +local classes = script.Parent.Classes +local RichEmbedClass = require(classes.RichEmbed) + +local embed = RichEmbedClass.new("Hi", "Bunny") +local field = embed:addField("Test field", "its value") \ No newline at end of file