Help with Discord.js to Roblox

I have a Discord bot using Discord.js. I’m currently trying to make it so you can type ;ban (Roblox User) (Reason) in a text channel, and if that user is in the game, that user will be banned.

I can add a command to do so, etc, but I just need SOMETHING to be sent to Roblox. I’m not asking you guys to make a ban command for me.

How do I send things to a Roblox server using Discord.js?

2 Likes

You must make this via your own VPS or trello using http service HttpService | Documentation - Roblox Creator Hub

1 Like

You have to constantly poll some sort of server (as to not spam Discord with requests) and have the server have a stable connection to Discord itself. That way only your server gets all those requests because unfortunately websockets dont exist in Roblox.

Discard my reply above, I read your question wrong.

You can use the

http.createServer(async function (req, res) {
var args = req.url.split("/")
if (args[1] == "checkban") {
if (banned.includes(args[2])) {
res.end(JSON.stringify({"result":true, "reason":"lol banned bruh"}))
} else {
res.end(JSON.stringify({"result":false}))
}
}
}).listen(8085)

Then, you can go in roblox and make a script:

game.Players.PlayerAdded:connect(function(p)
local x = game:GetService("HttpService"):GetAsync("http://XX.XX.XXX.XX:8085/checkban/".. p.UserId)
-- now basically just decode it 
if (decoded.result == true) then
p:Kick("You are banned for: ".. decoded.reason)
end

coroutine.resume(coroutine.create(function()
while true do wait(30)
local x = game:GetService("HttpService"):GetAsync("http://XX.XX.XXX.XX:8085/checkban/".. p.UserId)
-- now basically just decode it 
if (decoded.result == true) then
p:Kick("You are banned for: ".. decoded.reason)
end
end
end))
end)

This is polling like mentioned above. It refreshes every 30 seconds, so don’t expect the ban to be instant. If you’re using this for only one thing, you can probably use something shorter, like 10 seconds. Make sure your network can handle it depending on where you host your bot.

I have this for my Roblox script:

game.Players.PlayerAdded:connect(function(p)
	local x = game:GetService("HttpService"):GetAsync("http://XX.XX.XXX.XX:8085/checkban/".. p.UserId)
	if (decoded.result == true) then
		p:Kick("You are banned for: "..decoded.reason)
	end
end)

coroutine.resume(coroutine.create(function()
	while true do wait(5)
		local x = game:GetService("HttpService"):GetAsync("http://XX.XX.XXX.XX:8085/checkban/".. p.UserId)
		if (decoded.result == true) then
			p:Kick("You are banned for: "..decoded.reason)
		end
	end
end)

and this for my Discord.js bot (permissions aren’t done I just want to make sure it works)

    if (command == "sban") {
      let User = args[0]
      let Reason = args.slice(1).join(" ");
      
      if(!User) return
      if(!Reason) return
      
      JSON.stringify({"result":true, "reason":Reason})
    }

Is this correct?

You would need to store JSON.stringify({"result":true, "reason":"lol banned bruh"}) in a memory or database;

var data = []; // heres a memory example
    if (command == "sban") {
      let User = args[0]
      let Reason = args.slice(1).join(" ");
      
      if(!User) return
      if(!Reason) return
      data[User.toLowerCase()] = Reason
    }
http.createServer(async function (req, res) {
var args = req.url.split("/")
if (args[1] == "checkban") {
if (data[args[2].toLowerCase()]) {
res.end(JSON.stringify({result:true, reason:data[args[2].toLowerCase()]}))
} else {
res.end(JSON.stringify({"result":false}))
}
}
}).listen(8085)

Keep in mind that my syntax may be wrong because I’m writing this on devforum.

Roblox script looks fine. Make sure your server can handle 5 second polling as it is not recommended. Also you will need to setup port forwarding to make this work.

I created a bot/API to set something like this up. You should be using a short-poll system as @Wunder_Wulfe mentioned to accomplish this. You can’t directly access a game server without doing this.

If you would like a reference, @Cyanitem and his team has already made one. You can learn a lot from here:

2 Likes