I wanna make a bot which allows me to ban users via discord bots, I know how to get the api key but can anyone help me with the user restrictions API & how I can send requests to it, and how do I fill out the filters (IE the page token, user restriction ID, etc.)
NOTE: ELEMENTS OF THIS POST ARE INCORRECT, PLEASE SEE MY CORRECTED RESPONSE IN MY NEXT POST.
I don’t know the exact endpoint, but you need to send a dictionary similar to the one in Roblox.
const body = JSON.stringify({
userIds: [0000000],
displayReason: "Reason",
privateReason: "Reason",
duration: 300,
excludeAltAccounts: false,
applyToUniverse: true
});
Then, you can use discord.js
to send a HTTP request to the server.
You can get the discord.js
module using Node.js and npm (node packet manager):
npm init -y
npm install discord
const {Client, GatewayIntentBits, Partials} = require("discord.js");
const token = ""; //your bot's token
const allowedUserIds = []; //example for checking Discord user IDs for user validation
const apiKey = ""; //your api key
//new bot
const bot = new Client({
Intents: [/*Gateway Intents*/],
Partials: [/*Partials*/]
});
//when the bot is ready
bot.on("ready", function() {
console.log("Bot has logged in!");
});
async function banUser(userId, duration, msg) {
/*here, just check the response's code to make sure the request didn't fail. Return null if it did.*/
//cast values
userId = Number(userId);
duration = Number(duration);
//create ban data
const banData = {
duration: duration,
userIds: [userId],
applyToUniverse: true,
excludeAltAccounts: false,
privateReason: msg,
displayReason: msg
};
//create request data
const requestData = {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": apiKey
},
body: JSON.stringify(banData)
};
try {
const response = await fetch("https://apis.roblox.com/cloud/v2/the rest of the URL here"
} catch(error) {
//do what you will with the error
};
};
//when a message is sent
bot.on("messageCreate", message => {
//check user
if (!allowedUserIds.includes(message.author.id)) {
return null;
};
//split message at spaces
const msgData = message.content.split(" ");
//message data
var [_cmd, target, duration, msg] = msgData;
//get UserId using API
const data = {
method: "POST",
headers: {"content-type": "application/json"},
body: JSON.stringify({
usernames: [target],
excludeBannedUsers: true
})
};
fetch("https://users.roblox.com/v1/usernames/users", data).then(response => response.json()).then(raw => banUser(raw[0]["id"], duration, msg)).catch(error => messagechannel.send(error));
});
//log the bot in
bot.login(token);
I think I’ve missed a bit of the data you need to send with the ban request, but you could find it all on Roblox’s OpenCloud documentation.
I hope this helps!
Here’s the docs that allows you to call API to ban. You’d have a Discord bot that intakes a username, duration and reason and then the Discord bot just formats that data and sends it to Roblox with your API key.
I read documentation 3 times over, I am asking for help since I didn’t understand 3 parameters, thanks for your time though.
Hey!
I did a bit more research, and figured out how you can ban through the API.
This is assuming you are using Node.js
First, let’s make a quick function to get a Timestamp in the format Roblox wants it: an ISO string.
function getTimestamp() {
const date = new Date();
return date.toISOString();
};
Perfect. Now, a quick function to get the user’s UserId from username:
async function getUserId(username) {
//send the request to get the UserId
const response = await fetch("https://users.roblox.com/v1/usernames/users", {
method: "POST", //we need to send a POST request
headers: {
"content-type": "application/json" //indicate JavaScript Object Notation (JSON) content
},
body: JSON.stringify({
usernames: [username], //username we need
excludeBannedUsers: true //self-explanatory, don't include banned users
})
});
//get the request response in JSON
const data = await response.json();
if (!response.ok) {
//if it failed. Do what you will with the error.
} else {
return data[0]["id"]; //return the UserId
};
};
Great. Now we have that set up, we can get the ban process going.
async function banUser(channel, username, duration, reason) {
//get the UserId of the target
const userId = await getUserId(username);
//set up the URL
const url = `https://apis.roblox.com/cloud/v2/universes/${universeId}/user-restrictions/${userId}`; //replace universeId with your game's universe ID
//set up the ban data
const banData = JSON.stringify({
gameJoinRestriction: {
active: true,
startTime: getTimestamp(),
duration: String(length) + "s", //the length in seconds
privateReason: "Ban request from Discord bot. Reason: " + reason,
displayReason: reason,
inherited: true,
excludeAltAccounts: false
}
});
//send the ban request
const banResponse = await fetch(url, {
method: "PATCH", //send a PATCH request
headers: {
"content-type": "application/json",
"x-api-key": apiKey //your API key
},
body: banBody
});
//do what you will with the returned results here
};
You can test it like this:
//channel is the Discord channel the message was sent in
banUser(channel, username, 300, "test").catch(error => console.log(error));