Discord Integration: A guide on using Discord through Roblox [UPDATED]

httpservice
discord
webhooks

#1

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 :frowning: )

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: https://discordapp.com/developers/docs/resources/webhook#execute-webhook-jsonform-params.

UPDATE

Luckily @FoxbyDev was already working on an API, and has finished it! Here’s a link to his:


Game Design Theory: Planning a Game and Implementing Features
#2

There is a discord webhook API in the models made by Tigerism that works really well. I don’t think it supports embeds or anything of sort, just messages. Good tutorial though, good luck expanding it.

EDIT: Direct requests to Discord from Roblox no longer work, so I am going to assume that this API isn’t functional.


#3

Also good to mention that you should wrap the PostAsync in a pcall function because it will fail from time to time.


#4

This is actually against Discord’s TOS. I’d definitely advise against it.


Error tracking with Sentry on Roblox
#5

That’s strange. Why would they block an error monitor?


#6

Maybe it’s too resource intensive depending on the amount of errors


#8

I actually replied & asked if it was b/c of resources, but he just responded with this:

My best guess would be that they allow webhooks for stuff meant for Discord (like bots), and not people using it to send stuff.


Error tracking with Sentry on Roblox
Detailed Dislike Analytics/Feedback
#9

That makes sense I guess. I’ll remove the error monitoring part.


#10

You can leave it up there since it’d still help people out, but I had one logging errors for Survivor (which got 29k players at once), so I’m assuming the high amount of http requests made them look into what it was for. As long as your game doesn’t have a lot of players at once, they probably won’t ever check it.


#11

Can definitely agree with @repotted that lower traffic is probably fine. My game had 3k players on at once sending reports without any problems.


#12

Yeah, that’s probably why :grimacing:. I’ll put it back up there with a warning so people don’t get their discords terminated.


#13

I talked to a few of the Discord developers about this and it would seem that the main issue was certain developers not being meticulous about how many requests they were sending. For example, if you have an error that triggers multiple times per second because of some unchecked bug, you would basically be DDoSing Discord with POST requests. I’d say they likely mitigate this by checking certain headers that were present in the request that would reveal it was sent from a ROBLOX server.

There are several very high-profile servers that actively use Discord to log important errors, so I’m going to guess they aren’t completely opposed to error logging on the platform but ban it in the case of ROBLOX due to the very high potential of API abuse.

Then again, some people are still sending webhook messages through ROBLOX; perhaps this is because they’re only going after people causing trouble or they have a certain check setup like “if sent from ROBLOX and channel name includes ‘error’…”

TLDR: Use Sentry, it’s awesome and actually made for error logging.


Error tracking with Sentry on Roblox
Http PostAsync only working in studio
#14

Please do not spam discord. If too many people do this they may just blacklist roblox server IPs in general and that would ruin it for everyone who doesn’t abuse it.


#15

Would it be better to just remove the monitor in general? Some people with malicious intent might be viewing this.


#16

It’s kind of unavoidable. Worst case scenario those who use it responsibly can set up a proxy for very cheap (cheap because they aren’t overloading it!!)


#17

Alright. Thanks for the feedback all of you by the way.


#18

I haven’t used webhooks in a while, but I think you can send multiple embeds in one HTTP request. You could group several messages (each as a separate embed) into one request.

Actually lemme test:

{
  const url = 'https://canary.discordapp.com/api/webhooks/343362987622662144/z6r8217KiNVhWxf3SbnOaX8fQ4__TptWNvApVM3VqgybOXdQOE5-sgy00D4nMio7g89-';

  function embed(title, desc, color, author) {
    return {
      timestamp: "2017-07-11T17:27:07.299000+00:00",
      title,
      desc,
      color,
      author: {
        name: author,
        icon_url: 'https://cdn.discordapp.com/emojis/339896242211389450.png'
      },
      fields: [
        {
          name: 'A',
          value: 'B',
          inline: true
      },
        {
          name: 'A',
          value: 'B',
          inline: true
      },
        {
          name: 'A',
          value: 'B',
          inline: true
      },
        {
          name: 'A',
          value: 'B',
          inline: true
      },
    ]
    }
  }

  console.log(embed('a', 'b', 0xFFFFFF, 'einsteinK'));

  $.post(url, JSON.stringify({
    embeds: [
    embed('Title1', 'Description1', 0xFF0000, 'einsteinK'),
    embed('Title2', 'Description2', 0x00FF00, 'einsteinK'),
    embed('Title3', 'Description3', 0x0000FF, 'einsteinK'),
  ]
  }), console.log);
}

I already deleted the webhook to prevent spam. thanks to antonio6643#8700 for pointing that out


#19

We actually attempted to do that with the egg hunt this year and did not get such a message. We hit like all rate limits though so we had to remove it after a few hours.


#20

Honestly depends on what you’re using it for, and if your game is constantly printing things in the console.


#21

:pray: A user some months back linked me to Tigerism’s stuff, tons of coolness. This tutorial will hopefully spread the neat usage of Discord integration in ROBLOX.