Introduction
I’ve been scripting for a long time, and as a result, I’ve come up with various different ways to do things as opposed to more traditional methods. One of these methods I’ve come up with is using Discord Webhooks
to track and/or notify me of whats happening in my game. Without further ado, here’s the tutorial.
What is a Discord Webhook?
a Discord Webhook
is a handy little feature built into Discord that allows developers to send messages without the use of a bot(and in-turn a web server). These Webhooks are usable through POST
requests, and can only be used to send messages. By using these hooks, developers can do anything from error monitoring to even logging when players join and leave their game.
Setting up a Webhook on your Discord Server
Adding a Webhook to your server is incredibly easy.
Step 1. Open the Discord Server on which you want to put the Webhook.
(No screenshot )
Step 2. Click the dropdown arrow and open Server Settings.
Step 3. Open the Webhooks tab and click Create Webhook.
Step 4. Configure the Webhook to your liking and then save the link(We’ll need this later).
Interacting with the Webhook
Now that the webhook is set up, we can move on to using it in Roblox. Before we get into the code, make sure that HTTPService
is enabled. If you don’t know how, type this in your command bar:
game:GetService("HttpService").HttpEnabled = true
Now that HTTPService is enabled, we can get into the actual code of this tutorial. To use the webhook, we will have to use the PostAsync
function of HTTPService
. Here’s a basic example:
local http = game:GetService("HttpService")
local Data = {
["content"] = "Hey! This is a message sent from roblox!"
}
Data = http:JSONEncode(Data)
http:PostAsync("", Data) --Put the link you saved between the two quotes.
When we run this code, it’ll send a message looking something like this:
What else can we do with this?
Just about anything! Discord will automatically detect and implement any markdown you have in your code, so the possibilities are endless! Here’s a couple of examples of what I like do with these:
Error Monitoring
local HTTPService = game:GetService("HttpService")
local url = "[Discord url here]"
game:GetService("ScriptContext").Error:Connect(function(msg, stacktrace, scriptt)
local Data = {
["username"] = "Error Monitor";
["content"] = "```lua\n"..msg.."\n "..stacktrace.."```";
}
Data = HTTPService:JSONEncode(Data)
HTTPService:PostAsync(url, Data)
end)
WARNING: Do NOT use this if your game has high traffic. It could potentially lead to your Discord account being terminated.
Player Join Tracker
local http = game:GetService("HttpService")
game:GetService("Players").PlayerAdded:Connect(function(player)
if not game:GetService("RunService"):IsStudio() then
local date = os.date("!*t")
local Data = {
["content"] = player.Name.." joined your game on "..date.month.."/"..date.day.."/"..date.year
}
Data = http:JSONEncode(Data)
http:PostAsync("[Discord hook here]", Data)
end
end)
Final Notes
If you have any feedback on this tutorial, feel free to contact me either through the DevForums, or by PMing me directly on the roblox website. If you’d like to know more about what webhooks can do, check out this link: Discord Developer Portal.
UPDATE
Luckily @FoxbyDev was already working on an API, and has finished it! Here’s a link to his: