Changing users rank in roblox group

  • Are all functions prefixed with async?
    Is the bottom line correct? It uses a promise, which is basically something in the future which hasn’t happened yet.
updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));

I’m still getting the error hers some of the code

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    let data = await response.json();
    for (let role of data.groupRoles) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");

    let data = await response.json();
    for (let error of data.errors) {
        console.log("Code:", error.code, "Message:", error.message);
    }
};

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!

    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    console.log(response.status!==200?String(response.Status) + response.statusText + "(from second request)":"Request ok. Code 200.");
};

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));

k try this

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    let data = await response.json();
    for (let role of data.groupRoles) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!
    
    let data = await response.json();

    for (let error of response.errors) {
        console.log(error.code, error.message);
    };
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    console.log(response.status!==200?String(response.Status) + response.statusText + "(from second request)":"Request ok. Code 200.");
};

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));
1 Like

I got an error saying Request ok. Code 200.
TypeError: response.errors is not iterable
at getUserMembershipId (C:\Users\jenkins\Videos\fuelsystem\index.js:37:32)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async updateRoleProcess (C:\Users\jenkins\Videos\fuelsystem\index.js:58:24)

So… it is working now? I thought you said it wasn’t before?

Anyway, if there are no request issues, see if the target user got their rank changed in the group.

Run this and tell me if any of the errors output.

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    if (response.status !== 200) {
        for (let error of JSON.parse(response).errors) {
            console.log(error);
        };

        throw new Error("Warning: Failed to retrieve membership ID.")
    };

    let data = await response.json();
    for (let role of data.groupRoles) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");
    
    if (response.status !== 200) {
        for (let error of JSON.parse(response).errors) {
            console.log(error);
        };

        throw new Error("Warning: Failed to retrieve membership ID.")
    };

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!
    
    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    if (response.status !== 200) {
        for (let error of JSON.parse(response).errors) {
            console.log(error);
        };

        throw new Error("Warning: Failed to retrieve membership ID.")
    };
};

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));

If there are no errors, see if the user got ranked up.

I got another error saying Request ok. Code 200.
SyntaxError: Unexpected token ‘o’, “[object Response]” is not valid JSON

I was going for a quick way to parse JSON and forgot how the response body works, sorry

Can you change all occurences of this

To this:

if (response.status !== 200) {
    let data = await response.json();
    for (let error of data.errors) {
        console.log(error.code, error.message);
    }
}

Edit: accidentally replied to myself, not sure if you will be notified of this post so i will tag you @freezermonkey53

hers the code i got the same error again

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    if (response.status !== 200) {
        let data = await response.json();
        for (let error of data.errors) {
            console.log(error.code, error.message);
        }
    }

    let data = await response.json();
    for (let role of data.groupRoles) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");
    
    if (response.status !== 200) {
        for (let error of JSON.parse(response).errors) {
            console.log(error);
        };

        throw new Error("Warning: Failed to retrieve membership ID.")
    };

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!
    
    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    if (response.status !== 200) {
        for (let error of JSON.parse(response).errors) {
            console.log(error);
        };

        throw new Error("Warning: Failed to retrieve membership ID.")
    };
};

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));

Thats because you only changed one of the places the code i gave before was

You need to change all occurences like i said

now I’m getting an error saying Request ok. Code 200.
TypeError: data.errors is not iterable

ok, after some clearing up, I got to this:

const apiKey = "";
const groupId = 0;
const targetRole = "rank";
const targetUserId = 0;

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    let data = await response.json()

    if (response.status !== 200) {
        console.log(data.message);
    }

    for (let role of data.groupRoles) {
        if (role.displayName === targetRole) {
            return Number(role.id);
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");
    let data = await response.json();

    if (response.status !== 200) {
        console.log(data.message);
    };

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId;
        }
    }
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });

    let data = await response.json();

    if (response.status !== 200) {
        console.log(data.message);
    } else {
        console.log("Request ok. Code 200.")
    };
};

async function updateRoleProcess(userId, roleName) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole).catch(error => console.log(error));

Remember to replace with your constants at the top.

The only way I got “Bad Request” from this was if I tried to set the user to the rank they were already, if the user was not in the group, and also if the user is a higher rank than you, the maker of the API key, has permissions to edit.

to anyone else reading this whos gonna tell me i dont need some of the parameters, please don’t

I got another error saying Request ok. Code 200.
Failed to parse resource path and its identifiers - groups/7045894/memberships/undefined.

Is the UserId definitely correct? Is the user in the group?

The only way I was able to reproduce this myself was by making the group ID or user ID incorrect.

yes my user id and my group id right

And the role name, is it definitely right? Remember, it’s case sensitive.

my role name right const targetRole = “Suspended”;

Alright, well it’s an issue with the data getting to the endpoint somehow. In the part of the code where it checks all the role names against the target role name (in getGroupRoleId), can you make it output the found ID? (The code section with role.DisplayName === targetRole)

so I did console.log role then it printed this out path: ‘groups/7045894/roles/44649633’,
createTime: ‘2020-07-14T06:53:08.703Z’,
updateTime: ‘2023-07-26T04:57:01.563Z’,
id: ‘44649633’,
displayName: ‘Suspended’,
description: ‘Suspension from any Role or Job for a set amount of time. This is not considered a Staff Rank’,
rank: 20,
memberCount: 0,
permissions: {
viewWallPosts: true,
createWallPosts: true,
deleteWallPosts: false,
viewGroupShout: true,
createGroupShout: false,
changeRank: false,
acceptRequests: false,
exileMembers: false,
manageRelationships: false,
viewAuditLog: false,
spendGroupFunds: false,
advertiseGroup: false,
createAvatarItems: false,
manageAvatarItems: false,
manageGroupUniverses: false,
viewUniverseAnalytics: false,
createApiKeys: false,
manageApiKeys: false,
banMembers: false,
viewForums: false,
manageCategories: false,
createPosts: false,
lockPosts: false,
pinPosts: false,
removePosts: false,
createComments: false,
removeComments: false
}
}

Ok. How many people are in the group? You might need that nextPageToken I mentioned before.

where do i get the nextPageToken at