Discord Webhook Module

This is a simple discord module.
I’ll show examples and how you’d use the module to accomplish an API webhook.

Feel free to claim or download it as your own. It is a open module that is built to help you manage the API.

Example Script
local webhookModule = require(game.ReplicatedStorage:WaitForChild("WebhookModule"))
local embed = webhookModule.newEmbed()
local webhookSettings = webhookModule.newWebhookSettings()

local field = webhookModule.newField()

field.name = "Cool Title!"
field.value = "Cool Description!"

embed.title = "abc"
embed.description = "123"
embed.color = webhookModule.colors.red

table.insert(embed.fields, field)

webhookModule.send("https://Cool Discord Webhook", webhookSettings, embed)
Module Src
local httpService = game:GetService("HttpService")
local embedModule = {}

embedModule.colors = {}
embedModule.colors.aqua = 0x00FFFF
embedModule.colors.black = 0x000000
embedModule.colors.blue = 0x0000FF
embedModule.colors.fuchsia = 0xFF00FF
embedModule.colors.grey = 0x808080
embedModule.colors.green = 0x008000
embedModule.colors.lime = 0x00FF00
embedModule.colors.maroon = 0x800000
embedModule.colors.navy = 0x000080
embedModule.colors.olive = 0x808000
embedModule.colors.purple = 0x800080
embedModule.colors.red = 0xFF0000
embedModule.colors.silver = 0xC0C0C0
embedModule.colors.teal = 0x008080
embedModule.colors.white = 0xFFFFFF
embedModule.colors.yellow = 0xFFFF00

function embedModule.getISO8601Timestamp()
	-- CREDITS: https://www.roblox.com/users/17828104
	-- CREDITS: https://devforum.roblox.com/u/rogchamp
	-- MODIFIED: I have modified the origional code.
	
	local osDate = os.date("!*t")
	local year = osDate["year"]
	local month = string.format("%0" .. 2 .. "i", osDate["month"])
	local day = string.format("%0" .. 2 .. "i", osDate["day"])
	local hour = string.format("%0" .. 2 .. "i", osDate["hour"])
	local min = string.format("%0" .. 2 .. "i", osDate["min"])
	local second = string.format("%0" .. 2 .. "i", osDate["sec"])	
	
	return year .. "-" .. month .. "-" .. day .. "T" .. hour .. ":" .. min .. ":" .. second .. "Z"
end

function embedModule.newFooter()
	return {
		iron_url = "";
		text = "";
	}
end

function embedModule.newThumbnail()
	return {
		url = "";
	}
end

function embedModule.newImage()
	return {
		url = "";
	}
end

function embedModule.newAuthor()
	return {
		name = "";
		url = "";
		icon_url = "";
	}
end

function embedModule.newField()
	return {
		name = "";
		value = "";
		inline = false;
	}
end

function embedModule.newEmbed()
	return {
		title = "";
		description = "";
		url = "";
		color = 0;
		timestamp = embedModule.getISO8601Timestamp();
		footer = embedModule.newFooter();
		thumbnail = embedModule.newThumbnail();
		image = embedModule.newImage();
		author = embedModule.newAuthor();
		fields = {};
	}
end

function embedModule.newWebhookSettings()
	return {
		content = nil;
		username = nil;
		avatar_url = nil;
		tts = false;
	}
end

function embedModule.send(url, webhookSettings, embed)
	local httpPacket
	
	pcall(function() local _1 = webhookSettings; pcall(function() local _2 = _1; _1 = nil; httpPacket = _2 end) end)
	
	httpPacket.embeds = {embed}
	return httpService:PostAsync(url, httpService:JSONEncode(webhookSettings))
end

return embedModule

This module is just a helpful guide. Managing and handling embeds can be hard. This should help simplify it. I’ve programmed it in a way that new users may be able to understand and too. Get used to the discord API.

12 Likes

Thanks for the Resource. Its a shame because discord doesn’t exactly love how we send webhooks from roblox to discord plus sending too many may get it blocked.

I’d say it’s not really useful unless you make it respect Discord’s ratelimits.

As a suggestion, you should add a way to attach an allowed_mentions object to the webhook message as well.

1 Like

Using RequestAsync to post instead will let you rate limit much easier. A different Discord module on this site has that feature.

Discord returns a dictionary when you do this to indicate your rate limit leaky bucket details.

Don’t use PostAsync if you want to rate limit your webhook messages.

All in all. Seeing discord webhooks being sent via roblox is a bad practice. Unless you’re hosting a private test inside of a live server. Hence being able to collect the data you seek. That and complying with the discord ToS so you do not spam or cause harm. You should implement a fail safe option in case there is a spam. Hence stopping you from sending that payload. But I understand what you mean by the send function. Sadly it’s a module. So someone can easly do require(...).send() Which is again bad practice. But nothing I can do.

What I could do is save variables in Global or shared. But then again. Thats disturbing the game. The variables could magically mess with something inside of their game. Causing issues.