Discord API & Bot running API through Roblox studio

Hello roblox developers or newbies! If you are trying to run a Discord bot through the Discord APIs Inside of Roblox studio, I hope this information and piece of code helps you figure out how you can run them yourself!

  • Fetch messages from bot
  • Post messages through bot
  • Make replies, markings, reactions from your bot with just the API
    The discord API REFERENCES: Discord Developer Portal

Main stuff

local HttpService = game:GetService("HttpService")
-- ptb.discord.com is used if you want this to be useable in Public game prevents potential reject from request.
local BaseUrl = "https://ptb.discord.com/api/v9" 
-- Bot token
local Token = "MTI4Mjk5NzU2MjkwNjkwMj...... here"

FetchMessages by Channel ID Discord Developer Portal

-- Reference: https://discord.com/developers/docs/resources/message#get-channel-messages
function GetChannelMessages()	
	-- URL
	local Url = string.format(
		"%s/channels/%s/messages", -- Reference: https://discord.com/developers/docs/resources/message#get-channel-messages
		BaseUrl, -- BaseUrl
		"1349091999654871124" -- Channel Id
	)
	-- Headers
	local headers = {
		["Content-Type"] = "application/json", -- Get response in JSON type
		["Authorization"] = "Bot "..Token, -- Remove "Bot " if User token.
	}
	
	-- Pcall
	local Success, Response = pcall(function()
		return HttpService:RequestAsync({
			Headers = headers,
			Url = Url,
			Method = "GET",
		})
	end)
	
	if Success and Response and Response.Body then
		-- Content of the Response
		local Body = HttpService:JSONDecode(Response.Body) -- Decode JSON into Lua table
		if Body then
			
			for _, Messages in pairs(Body) do -- Load messages
				if Messages and Messages["author"] then
					-- Message content reference: 
					-- https://discord.com/developers/docs/resources/message#message-object
					local author = Messages.author
					local authorId = author.id
					local channelId = Messages.channel_id
					local messageId = Messages.id
					local content = Messages.content	-- Message content
					print(content)
				end
			end
			
		end
	end
end
GetChannelMessages() -- Run

CreateMessage to Channel ID Discord Developer Portal

-- Reference: https://discord.com/developers/docs/resources/message#create-message
local function CreateMessage()
	-- URL
	local Url = string.format(
		"%s/channels/%s/messages", -- Reference: https://discord.com/developers/docs/resources/message#create-message
		BaseUrl, -- BaseUrl
		"1349091999654871124" -- Channel Id
	)
	-- Headers
	local headers = {
		["Content-Type"] = "application/json", -- Get response in JSON type
		["Authorization"] = "Bot "..Token, -- Remove "Bot " if User token.
	}
	-- Contents
	local body = {
		["content"] = "Hello world", -- Message content
	}
	
	-- Pcall
	local Success, Response = pcall(function()
		return HttpService:RequestAsync({
			Headers = headers,
			Url = Url,
			Method = "POST",
			Body = HttpService:JSONEncode(body)
		})
	end)

	if Success and Response then
		print("CreateMessage successfully!")
	end
end
CreateMessage() -- Run
8 Likes