Discord Webhooks - Integrating Roblox with Discord


Hello, Welcome to this tutorial! The tutorial is entirely devoted to Discord Webhooks and some examples of how to use them in your roblox game and take it to the next level. This is my first tutorial on roblox so I am open to any kind of suggestions about the tutorial. Anways Keep Scrolling down to keep learning…:slightly_smiling_face:

NOTE: As Of October 2021, Discord has banned any webhook requests coming from the Roblox UserAgent, because it was getting highly abused and used for situations like error logging etc, which it isn’t meant for.

If that is your use case, use Game Analytics, or Sentry.


About The Tutorial

Part 1 - About Webhooks
Part 2 - How to Create A Webhook
Part 3 - Integrating Them Into Our Games
Part 4 - Some Cool Examples of Webhooks used in Roblox games


Part 1 - About Webhooks

Webhooks typically are used to connect 2 Different Applications to transfer data updates from one Application to another. In the case of Discord and Roblox, we can use Discord’s Built-In Webhook Functions to create an easy way to Post Automated Messages into any channel in your server From Roblox using POST Requests.


Part 2 - Creating Your Own Webhook!

Obviously to use webhooks, you should have a server. Then open the server and follow the steps below:

Step 1 - Open the Settings Tab Of Your Server

Untitled

Step 2 - Navigate to the Webhooks Tab

Step 3 - Click on the Create Webhook Button

Step 4 - Configure you Webhook!

Now that you have come till here, you have some options that you can configure:

  • Choose the Channel - You can select the desired channel you want the posts to be sent in.

  • Name it - You can give an Unique name to the webhook to distinguish between Multiple Webhooks (If you have.)

  • Upload An Icon - Some Icon…

After doing all that, copy your Webhook URL and save it somewhere. As we will need it later on.

And…There you go! You just created your own Discord Webhook! Now lets move on to integrating them into our games in the next section :slightly_smiling_face:


Part 3 - Integrating the Webhook With Roblox Game

You just created your webhook in the last section, now lets move on to integrating the Webhook with your game to send simple messages from Roblox to a Discord Channel!

But first of all, please make sure that your HTTPService Is Enabled, If its not you can enable it by going to Game Settings > Options and Turn on HTTPService

After Ensuring that it is enabled, lets move on to the Actual part!
First of all you have to get the HttpService in your script, which can done with something like this:

local HS = game:GetService("HttpService")

After This you can store your WebhookURL into a variable, just to make the code a bit clean.

local WebhookURL = "https://discordapp.com/api/webhooks/xxxxxxxx/xxxxxx"

--Replace your link with the link in the Quotes.

Then you can store the message you want to send in a table, like the following.

local MessageData = {
	["content"] = "Hoi, Test message arrived!"
}

MessageData = HS:JSONEncode(MessageData)

--We used JSONEncode to convert the Lua Table into a Json String 

And Finally you can use the POST method to Send a HTTP Post Request to the URL with the Message.

HS:PostAsync(WebhookURL,MessageData)

Your Final Script should look something like this:

local HS = game:GetService("HttpService")
local WebhookURL = "https://discordapp.com/api/webhooks/xxxxxx/xxxxxxx"
--Replace your link with the link in the Quotes.

local MessageData = {
	["content"] = "Hoi, Test Message arrived!"
}

MessageData = HS:JSONEncode(MessageData)
--We used JSONEncode to convert the Lua Table into a Json String 

HS:PostAsync(WebhookURL,MessageData)

The Above was a very simple example to demonstrate how you can use the Discord Built-In Webhooks Function to send a message from Roblox to Discord!


Part 4 - A Cool Example!

As I said in the beginning of the tutorial, you can use webhooks to basically take your game to the next level and in some ways save you some time also!

A Feedback UI, which will allow players to send feedback which land directly on your discord Server!

For the UI you can design it as you like, but I will guide you through the script for controlling the max length you can write, the Function of the Send Button etc.

ControllerForUI Script

local maxCharacters = 500 -- Maximum Characters

local player = game.Players.LocalPlayer
local open = false

local feedbackMain = script.Parent.FeedbackMain

feedbackMain.CharactersLeft.Text = maxCharacters - #feedbackMain.InputBox.Input.Text
feedbackMain.InputBox.Input.Changed:Connect(function()
	feedbackMain.CharactersLeft.Text = maxCharacters - #feedbackMain.InputBox.Input.Text
	if maxCharacters - #feedbackMain.InputBox.Input.Text < 0 then
		feedbackMain.CharactersLeft.TextColor3 = Color3.fromRGB(255,50,50)
		feedbackMain.SendButton.Style = Enum.ButtonStyle.RobloxRoundButton
	else
		feedbackMain.CharactersLeft.TextColor3 = Color3.fromRGB(255,255,255)
		feedbackMain.SendButton.Style = Enum.ButtonStyle.RobloxRoundDefaultButton
	end
end)

local db = false
feedbackMain.SendButton.MouseButton1Click:Connect(function()
	if not db and maxCharacters - #feedbackMain.InputBox.Input.Text >= 0 then
		db = true
		local msg = feedbackMain.InputBox.Input.Text
		feedbackMain.InputBox.Input.Text = "Sending Message..."
		local response = game.ReplicatedStorage.FilteringFunction:InvokeServer(msg)
		feedbackMain.InputBox.Input.Text = response
		wait(5)
		if feedbackMain.InputBox.Input.Text == response then
			feedbackMain.InputBox.Input.Text = "Type feedback/bug report here"
		end
		db = false
	end
end)

script.Parent.Button.MouseButton1Click:Connect(function()
	open = not open
	if open then
		feedbackMain:TweenPosition(UDim2.new(1,-300,1,-300),"Out","Quint",0.3,true)
	else
		feedbackMain:TweenPosition(UDim2.new(1,100,1,-300),"Out","Quint",0.3,true)
	end
end)

You can make the UI according to yourself and change the Tweens Values or the Max Characters.

After doing all that, you can create a Remote Function in Replicated Storage with the corresponding name, in our case we used FilteringFunction as that is what we used in the first script.

Now lets move on to the Server Side, which will Post the Request so the message is sent to the Discord Server! For this you can write up a script something like the following.


local webhookURL = "https://discordapp.com/api/webhooks/xxxx/xxxxxxx"
local filteringFunction = game.ReplicatedStorage.FilteringFunction

local ChatService = game:GetService("Chat")

local HTTP = game:GetService("HttpService")

function filteringFunction.OnServerInvoke(player, msg)
	local FilteredMessage = ChatService:FilterStringForBroadcast(msg, player)	
        --Filter the message, before sending it to the webhook.

	local payload = HTTP:JSONEncode({
		content = FilteredMessage,
		username = "Submitted By "..player.Name
	})
	
	HTTP:PostAsync(webhook, payload)
	return "Feedback recieved!"
end

Note: You have to Filter the message before sending it to the webhook, for security reasons.

And that would do the job! A quick preview of what this will result in:
https://gyazo.com/d7787ebc001d6e722daf5a9a0d1df348

As you can see, alot can be done with Webhooks! Thats all For Now guys, please suggest me how I can improve my tutorial! Thanks for reading!

If you want to contact me, you can on DevForum itself or on my Discord - Neil#1234

Some Important Points to Note

  • The Discord’s API rate limits requests in order to prevent abuse and overload - rate-limits
  • Webhooks shouldn’t be used for error logging.
  • Be sure to implement a rate limiting for your Roblox to Discord Integration, otherwise players might use it in a bad way and hit the Discord rate limits.

Again Thanks For reading :slightly_smiling_face:

173 Likes

This is a good topic on discord web-hooks, I kinda like the idea of this. Me and some other developers created a App service out of web-hooks. However this looks good as well! :slight_smile: Keep up the good work.

8 Likes

Thank you for your kind words :slightly_smiling_face:

3 Likes

Thank you so much for the much detailed tutorial my friend water! Keep uploading more tutorials like this. :smiley:

6 Likes

Welcome, I will make more tutorials someday.

3 Likes

Is there anyway to trigger an in game event across all server with a discord command that the webhook could respond too? I’m not too sure if a discord webhook can respond to commands though. If anyone knows more info on this please reply or DM me on discord
Afraid#0035 (Also please state that you’re from the devforum thanks) :smile:

6 Likes

Discord webhooks can only send messages. You can’t fire an event from them.

2 Likes

@Afraid4Life As said by Ultrasonic54321, Discord Webhooks can only send messages. But you can make a Discord bot for this. You would need web hosting, and every time a command is run, the backend will check the Command and update the server. But currently HTTP requests cannot be posted to Roblox games. So maybe use a while loop and check if there is anything new, then execute the event.

5 Likes

For Simplicity you can try using this module to connect with the Roblox API - GitHub - affenity/bloxy: Interact with the Roblox website and API

4 Likes

It should also be noted to respect the rate limits from discord and not to abuse the webhooks. It also isn’t recommended to using the webhook to log errors (should be avoided at all costs).

3 Likes

Thanks for reminding me about it, I have added it in the Tutorial :slightly_smiling_face:

That is amazing but I get curious if you can write a message from discord that says as for example:
! ms hi
then on all servers, a message says hi

2 Likes

Unfortunately, you can not use Webhooks to send something from Discord to Roblox. Webhooks are only a one way connection to Discord. You would have to look into making a Discord bot if you want to send something from Discord to Roblox.

3 Likes

I think there’s a small mistake in your last script:

local webhookURL = "https://discordapp.com/api/webhooks/xxxx/xxxxxxx"
local filteringFunction = game.ReplicatedStorage.FilteringFunction

local ChatService = game:GetService("Chat")

local HTTP = game:GetService("HttpService")

function filteringFunction.OnServerInvoke(player, msg)
	local FilteredMessage = ChatService:FilterStringForBroadcast(msg, player)	
        --Filter the message, before sending it to the webhook.

	local payload = HTTP:JSONEncode({
		content = FilteredMessage,
		username = "Submitted By "..player.Name
	})
	
	HTTP:PostAsync(webhook, payload)
	return "Feedback recieved!"
end

In the second to last line its says HTTP:PostAsync(webhook, payload). I think it has to be webhookURL.

Anyways great tutorial! It really helped me to improve myself and my game :happy3:

6 Likes

Hello, how do I script the webhook that when someone buys an gear then it should send to my Discord Server.

1 Like

I would use the PromptGamePassPurchaseFinished event of MarketplaceService then inside the event send a message to the webhook.

2 Likes

I have an bot created from application, how do I do that? Like fully script and making the bot to be online?

1 Like

I do not think a bot would be necessary if you are just sending a message to a Discord channel. If you are looking to use a bot for something else, this thread is probably not the best place to ask since this is mainly about Webhooks, not bots.

2 Likes

Okay, may you send an link about bots please?

1 Like

Well, there are many threads out there about Discord bots. I would recommend searching on the DevForum or just looking things up on Google that is related to what you are trying to do.

1 Like