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:
- 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”
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.