Skip to content

Instantly share code, notes, and snippets.

@SuelTheDev
Created February 6, 2023 19:38
Show Gist options
  • Save SuelTheDev/a480a08dffae36ab902eb9cad30a3de4 to your computer and use it in GitHub Desktop.
Save SuelTheDev/a480a08dffae36ab902eb9cad30a3de4 to your computer and use it in GitHub Desktop.
discordembed.lua
DiscordEmbed = {}
DiscordEmbed.__index = DiscordEmbed
EmbedType = {}
local function joinTables(t1, t2)
for i = 1, #t2 do
t1[#t1 + 1] = t2[i]
end
return t1
end
function EmbedType.Footer(text, icon_url, proxy_icon_url)
assert(text and text:len() > 0, "Text need be a string and length > 0")
return {
text = text,
icon_url = icon_url or nil,
proxy_icon_url = proxy_icon_url or nil
}
end
function EmbedType.Field(name, value, inline)
return {
name = name,
value = value,
inline = inline or false
}
end
function EmbedType.Author(name, url, icon_url, proxy_url)
assert(name and name:len() > 0, "Name is required.")
return {
name = name,
url = url,
icon_url = icon_url,
proxy_icon_url = proxy_url
}
end
function EmbedType.Provider(name, url)
return {
name = name,
url = url
}
end
function EmbedType.Thumbnail(url, proxy_url, height, width)
return {
url = url,
proxy_url = proxy_url,
height = height,
width = width
}
end
function EmbedType.Video(url, proxy_url, height, width)
return {
url = url,
proxy_url = proxy_url,
height = height,
width = width
}
end
function EmbedType.Image(url, proxy_url, height, width)
return {
url = url,
proxy_url = proxy_url,
height = height,
width = width
}
end
function DiscordEmbed:New()
local obj = {
title = nil,
description = nil,
url = nil,
timestamp = nil,
color = nil,
footer = nil,
image = nil,
thumbnail = nil,
video = nil,
provider = nil,
author = nil,
fields = nil
}
setmetatable(obj, self)
return obj
end
---comment
---@param fields EmbedType.Field | EmbedType.Field[]
---@return DiscordEmbed
function DiscordEmbed:addFields(fields)
if not self.fields then
self.fields = {}
end
local is_array = #fields > 0
if is_array then
self.fields = joinTables(self.fields, fields)
else
self.fields[#self.fields + 1] = fields
end
return self
end
---Set the Author of Embed
---@param author EmbedType.Author
---@return DiscordEmbed
function DiscordEmbed:setAuthor(author)
self.author = author
return self
end
---Set the color
---@param color integer
---@return DiscordEmbed
function DiscordEmbed:setColor(color)
self.color = color
return self
end
---Set the description
---@param description string
---@return DiscordEmbed
function DiscordEmbed:setDescription(description)
self.description = description
return self
end
---Set the fields
---@param fields EmbedType.Field[]
---@return DiscordEmbed
function DiscordEmbed:setFields(fields)
self.fields = fields
return self
end
---Set the footer
---@param footer EmbedType.Footer
---@return DiscordEmbed
function DiscordEmbed:setFooter(footer)
self.footer = footer
return self
end
---set the image
---@param image EmbedType.Image
---@return DiscordEmbed
function DiscordEmbed:setImage(image)
self.image = image
return self
end
---Set the thumbnail
---@param image EmbedType.Thumbnail
---@return DiscordEmbed
function DiscordEmbed:setThumbnail(thumbnail)
self.thumbnail = thumbnail
return self
end
---Set the timestamp can be a number or ISO 8601 string
---@return DiscordEmbed
function DiscordEmbed:setTimestamp(timestamp)
if not timestamp then
self.timestamp = os.date("!%Y-%m-%dT%XZ")
elseif type(timestamp) == "number" then
self.timestamp = os.date("!%Y-%m-%dT%XZ", timestamp)
elseif type(timestamp) == "string" then
self.timestamp = timestamp
end
return self
end
---Set the title
---@param title string
---@return DiscordEmbed
function DiscordEmbed:setTitle(title)
self.title = title
return self
end
---set the url
---@param url string
---@return DiscordEmbed
function DiscordEmbed:setURL(url)
self.url = url
return self
end
---@return string
function DiscordEmbed:toJSON()
return json.encode(self)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment