How do I make code that connects to discord and from discord back?

Hello, I’m trying to make a approve and decline system where it logs to the discord and says “This person wants to do this” and then it has a reaction where you could either press the approve or decline and it’d connect with whatever response. How would I achieve this?

1 Like

I tried this a while ago but it appears that roblox and discord do not want to be able to connect. I do not know if this still stands but it is quite sad. I had cool ideas for it.
Enjoy your day :saluting_face:

Oh well, that is sad that they wont allow that but I guess it makes sense since discord is 13+, thank you for your response though!

No problem! Though do consider that this was several months ago, that may well have changed.

Script an actual bot, you can host it yourself or use any hosting. Autocode lets you create discord bots with ease with just a little knowledge of javascript, however free plans are rate limited, I think about 1000 requests a month. I’ve managed to create a discord verification bot using autocode. https://autocode.com/ (not advertising!!!)

Contrary to what other people have said it is possible. It’s not hard but it requires a bit of backend setup.

What you’d need to do is:
Set up a webserver. Discord webhooks won’t work because you’re going to transfer data to a bot which will process and send back the information.

I don’t know your setup but I’d recommend using Cloudflare Workers (https://workers.cloudflare.com) which execute Node.js server-side Javascript from the edge. Very cheap (free up to 100k requests/day) and scales around the world. You’ll need a domain but you could also get a free subdomain from freedns.afraid.org. Cloudflare will also handle DoS attacks and hides any endpoints that might be vulnerable. You could also just set up something on your computer.

Then, you’ll need to use httpService inside your game to send a POST request to your server that’s carrying your data. I don’t know exactly what you’re doing but here’s an example based on what you said:

local HttpService = game:GetService("HttpService")

local function onReceivedRequest(request)
    local data = request.Body
    print("recieved:", data)
end

game:GetService("HttpService"):RegisterAsyncHttpListener("MyPostEndpoint", HttpService.HttpMethod.POST, onReceivedRequest)

local HttpService = game:GetService("HttpService")
local serverUrl = "https://yourserver.com/post" 

local dataToSend = {
    id = "5789",
    username = "example123",
    request = "info"
}

local jsonData = HttpService:JSONEncode(dataToSend)

local headers = {
    ["Content-Type"] = "application/json",
}

local success, response = pcall(function()
    return HttpService:RequestAsync({
        Url = serverUrl,
        Method = "POST",
        Headers = headers,
        Body = jsonData,
    })
end)

if success then
    print("request sent successfully.")
    print("response:", response.Body)
else
    print("error:", response)
end

Then, on your server you’ll need to process the data sent in the request and then get it to a Discord bot. There’s a few ways of doing this:

  • If you’re using CF workers then you can also host the Discord bot there. You can follow this tutorial but you’ll essentially have to configure a tunnel.
  • Or, if you’re hosting the server on your computer & the Discord bot is also on your computer, you can get the data from the request and send it directly through your Discord bot to the server.

Once the message has been sent, you have to configure your Discord bot to send the approval back to the webserver. Since you can’t send POST requests directly to Roblox servers, you have to put the data on an external server and get Roblox to read it.

You could use unique ticket IDs if you’re going to get a lot of requests, but you could also just organize them by name assuming they’ll be deleted later. To simplify it, this is the step-by step process:

  • You’ve just submitted a request on Roblox and a ticket is created with a unique ID of 5789.
  • The bot posts a message on the Discord server requesting approval
  • Someone on the Discord server accepts the request with the ID of 5789.
  • The Discord bot will now add request 5789 to a JSON file on the webserver and mark it as approved, like this, for Roblox to read:
{
  "5789": "approved",
  "1234": "approved",
  "2637": "declined"
}

Then, on Roblox, you’ll need to be periodically sending requests to the JSON file (let’s say it’s located at yourserver.com/requests.json) to check if it has been accepted or declined. For example, in Roblox:

local HttpService = game:GetService("HttpService")

local jsonFileUrl = "https://yourserver.com/requests.json"

local function checkIds()
    local success, response = pcall(function()
        return HttpService:GetAsync(jsonFileUrl)
    end)

    if not success then
        warn("failed to fetch:", response)
        return
    end

    local jsonData = HttpService:JSONDecode(response)

    if jsonData then
        for id, status in pairs(jsonData) do
            if status == "approved" then
                print("ID", id, "APPROVED")
            elseif status == "declined" then
                print("ID", id, "DECLINED")
            else
                print("ID", id, "invalid:", status)
            end
        end
    else
        warn("failed to read")
    end
end

checkIds()

while true do
   -- check every 30 seconds
    wait(30)
    checkIds()
end

Let me know if you need help with anything or something doesn’t make sense.

1 Like

Wow, this actually worked, thank you so much. I really do appreciate it.

1 Like