Using the Discord REST API through Roblox

As some of you may know, Discord has a REST API which can be used for bots to perform different functions within Discord. This API can be used anywhere where you can send an HTTP request including Roblox’s HttpService.

Using this API can be a lot easier than needing to host and setup an external server.

Reminder: Respect all ratelimits set by Discord for their API. Ratelimits can be found here

Note: You are unable to receive events using this. To do that, you would need to use an external server. That is not what this guide is for.

What is a REST API?

A REST API (REpresentational State Transfer) is a type of web service which handles requests from a client to a server

There are multiple types of methods available in a REST API which include but are not limited to:

  • GET - Used to retrieve information
  • POST - Used to create information
  • PATCH - Used to update information
  • DELETE - Used to delete information
What is the Discord API?

Discord has an API that is used to perform actions on bot user accounts. The documentation can be found here.

The base URL for the Discord REST API is https://discord.com/api/v{version}/

We will be using the latest version of the Discord API in this tutorial (v9)

Please note this is talking about Discord’s REST API used to perform actions and not the Discord Gateway used to receive events. The gateway is not accessible using Roblox due to a lack of WebSocket support.

Discord API Endpoints are documented like this:
[METHOD] [endpoint]

For example, the get channel endpoint is documented as:
GET /channels/{channel.id}

This means that the request requires a GET request to https://discord.com/api/v9/channels/{channel.id} with “{channel.id}” being replaced with a valid channel id.

Most endpoints need an Authorization header of "Bot " + the bot token

Making Discord Bot Application and getting the token

The first part of interacting with the Discord API is having a bot to interact to it with.

To make the bot. You should go to this page and follow these instructions:

  1. Creating the Application

When you first go onto the page, you should see a “New Application” button on the corner of your screen. Click it to start the process of creating a Bot Application.

You should now see a UI to Create the Bot. Enter the bot name and press “Create”

Once you create the application, go to the “Bot” menu in settings and press “Add Bot”

botsetting

Once you do that, press the “Yes, do it!” button and your bot should be created.

To finally get your token, find the token section of the Bot menu and press “Copy”.

You should now have the bot’s token copied and can use it accordingly.

Examples
Adding a Role

The Documentation for the add guild member role endpoint is

PUT /guilds/{guild.id}/members/{user.id}/roles/{role.id}

This means we need a guild (server) id, a user id, and a role id

local HttpService = game:GetService("HttpService")

-- Setting ID Variables
local guildId = "591720572250226730" -- Example Guild ID
local userId = "640224786366201856" -- Example User ID (You can use some sort of verification API to get UserIds)
local roleId = "591721253933416451" -- Example Role ID

-- Setting token
local token = "YOUR_BOT_TOKEN" -- Put your Bot Token Here

-- Making the request
HttpService:RequestAsync({
Url = ("https://discord.com/api/v9/guilds/%s/members/%s/roles/%s"):format(guildId, userId, roleId),
Method = "PUT",
Headers = {
["Authorization"] = "Bot "..token,
["Content-Type"] = "application/json"
},
Body = "{}" -- Empty body because PUT requests still require you to have a body
})

Sending a message

Note: Sending a message requires your bot to have previously connected to the gateway using another method. Webhooks are a good alternative to this endpoint.

You are also unable to use images/other media/files in your messages as it is not supported by Roblox.

The Documentation for the create message endpoint is

POST /channels/{channel.id}/messages

This means we need a channel id

This endpoint also requires a body as shown below:

Most of these are optional except for “content”

Here is the example code:

local HttpService = game:GetService("HttpService")

-- Setting ID Variables
local channelid = "694926422820782081"
-- Setting token
local token = "YOUR_BOT_TOKEN" -- Put your Bot Token Here
-- Message Content Variable
local messageContent = "Testing Message"

-- Making the request
HttpService:RequestAsync({
	Url = ("https://discord.com/api/v9/channels/%s/messages"):format(channelid),
	Method = "POST",
	Headers = {
		["Authorization"] = "Bot "..token,
		["Content-Type"] = "application/json"
	},
	Body = HttpService:JSONEncode({["content"] = messageContent})
})

Your bot should now send the message in the specified channel

If you would like to see anything added/changed in this guide, or have any other questions, feel free to ask in the comments.

18 Likes

So, I fully made this API as an OOP module on Roblox. I just haven’t wanted to release it yet because some of the code is still funky.

But I went through and made all the API from Guild, Channel, and Webhook on there,

(For the ones you can use through Roblox at least. Some of them you can’t use because you need to send images or files which you can’t through Roblox. And some of them you need to connect with a websocket before you can do anything. i.e., sending messages to channels through the bot, you can still send them with a webhook though, unless you want to find a way to connect to the socket, which only needs to be done once then you can send messages.)

Great tutorial!

1 Like

Yeah, I was originally making a module for this but decided to stop it and just let developers decide how they want to implement the API.

I did note the part about having to connect to the gateway for sending messages in my send message example and recommended webhooks as an alternative. I didn’t really think about messages with files though, will note that in the post.

Thanks for the support!

1 Like

Is it possible to send an embed? If so how?