Group ranking system

Hello! I’ve come across a big rock in my head. I am trying to make a group ranking system, so whenever you click rank a player to a rank in the group, they will become the rank in the group. Would I be-able to use apis on there own using HTTPService, or would I need a cookie. (So if the player ranking another player has permissions to rank in the group, would the api work or would it need a cookie?)

4 Likes

Greetings! The answer to your question is that you would require a cookie as the API’s will require a user to rank the target to the rank you want to rank them to.

Can you do all of this in stuido?

1 Like

So I just wrote and tested this & it works.

local players = game:GetService("Players")
local groups = game:GetService("GroupService")
local http = game:GetService("HttpService")

local getNameFromUserIdAsync = players.GetNameFromUserIdAsync
local getGroupInfoAsync = groups.GetGroupInfoAsync
local getGroupsAsync = groups.GetGroupsAsync
local getAsync = http.GetAsync
local jsonDecode = http.JSONDecode
local jsonEncode = http.JSONEncode
local requestAsync = http.RequestAsync

local groupRolesUrl = "https://groups.roproxy.com/v1/groups/%s/roles"
local changeGroupRoleUrl = "https://groups.roproxy.com/v1/groups/%s/users/%s"

local cookie = "" --Copy and paste your cookie here.
cookie = cookie:gsub("_|WARNING:%-DO%-NOT%-SHARE%-THIS\.%-%-Sharing%-this%-will%-allow%-someone%-to%-log%-in%-as%-you%-and%-to%-steal%-your%-ROBUX%-and%-items\.|_", "")

local whitelistedIds = {1, 2, 3} --These players can use the command.

local function changeGroupRole(groupId, userId, groupRoleId, xCsrfToken, maxTries)
	maxTries = maxTries or 5
	if maxTries == 0 then return end
	maxTries -= 1
	
	local requestBody = {["roleId"] = groupRoleId}
	local success, result = pcall(jsonEncode, http, requestBody)
	if success then
		if result then
			local requestData = {}
			requestData.Url = changeGroupRoleUrl:format(groupId, userId)
			requestData.Method = "PATCH"
			requestData.Headers = {["cookie"] = cookie, ["x-csrf-token"] = xCsrfToken, ["Content-Type"] = "application/json"}
			requestData.Body = result
			
			local success2, result2 = pcall(requestAsync, http, requestData)
			if success2 then
				if result2 then
					if not result2.Success then
						xCsrfToken = result2.Headers["x-csrf-token"]
						changeGroupRole(groupId, userId, groupRoleId, xCsrfToken, maxTries)
					end
				end
			else
				task.wait()
				warn(result2)
				changeGroupRole(groupId, userId, groupRoleId, xCsrfToken, maxTries)
			end
		end
	else
		task.wait()
		warn(result)
		changeGroupRole(groupId, userId, groupRoleId, xCsrfToken, maxTries)
	end
end

local function getGroupRoles(groupId, maxTries)
	maxTries = maxTries or 3
	if maxTries == 0 then return end
	maxTries -= 1
	
	local groupRoles = {}
	
	local requestUrl = groupRolesUrl:format(groupId)
	local success, result = pcall(getAsync, http, requestUrl)
	if success then
		if result then
			local success2, result2 = pcall(jsonDecode, http, result)
			if success2 then
				if result2 then
					for _, groupRole in ipairs(result2.roles) do
						groupRoles[groupRole.rank] = groupRole.id
					end
					
					return groupRoles
				end
			else
				task.wait()
				warn(result2)
				getGroupRoles(groupId, maxTries)
			end
		end
	else
		task.wait()
		warn(result)
		getGroupRoles(groupId, maxTries)
	end
end

local function getPrimaryGroup(userId, maxTries)
	maxTries = maxTries or 3
	if maxTries == 0 then return end
	maxTries -= 1
	
	local success, result = pcall(getGroupsAsync, groups, userId)
	if success then
		if result then
			for _, groupInfo in ipairs(result) do
				if groupInfo.IsPrimary then
					return groupInfo.Id
				end
			end
		end
	else
		task.wait()
		warn(result)
		getPrimaryGroup(userId, maxTries)
	end
end

local function isIdValid(userId, maxTries)
	maxTries = maxTries or 3
	if maxTries == 0 then return end
	maxTries -= 1
	
	local success, result = pcall(getNameFromUserIdAsync, players, userId)
	if success then
		if result then
			return true
		else
			return false
		end
	else
		task.wait()
		warn(result)
		isIdValid(userId, maxTries)
	end
end

local function onPlayerAdded(player)
	local function onPlayerChatted(message)
		if message:lower():match("^:rank") then
			if table.find(whitelistedIds, player.UserId) then
				local splitMessage = message:split(" ")
				if splitMessage[2]:len() > 0 and splitMessage[3]:len() > 0 then
					local chattedRank = splitMessage[3]:match("^%d+$")
					local chattedPlayers, chattedPlayerId = {}, nil
					for _, player in ipairs(players:GetPlayers()) do
						if player.Name:lower():match("^"..splitMessage[2]:lower()) then
							table.insert(chattedPlayers, player)
						end
					end
					
					if chattedRank then
						chattedRank = tonumber(chattedRank)
						if chattedRank then
							if #chattedPlayers == 1 then
								chattedPlayerId = chattedPlayers[1].UserId
							else
								chattedPlayerId = tonumber(splitMessage[2])
								if chattedPlayerId then
									if isIdValid(splitMessage[2]) then
										chattedPlayerId = splitMessage[2]
									else
										chattedPlayerId = nil
									end
								end
							end
							
							if chattedPlayerId then
								local primaryGroupId = getPrimaryGroup(player.UserId)
								if primaryGroupId then
									local groupRoles = getGroupRoles(primaryGroupId)
									if groupRoles then
										local groupRoleId = groupRoles[chattedRank]
										if groupRoleId then
											changeGroupRole(primaryGroupId, chattedPlayerId, groupRoleId)
										end
									end
								end
							end
						end
					end
				end
			end	
		end
	end
	
	player.Chatted:Connect(onPlayerChatted)
end
	
players.PlayerAdded:Connect(onPlayerAdded)

It’s currently set up to change roles in the game owner’s primary group (this can be changed), there are two ways in which it works. Firstly, you can type :change {PlayerNameHere} {GroupRankHere} if the player is in the server (you can abbreviate the player’s name too). Secondly, if the player is not in the server you can type :change {PlayerIdHere} {GroupRankHere}, the player’s ID must be exact.

8 Likes

Jeez. Didn’t know what I was going up against.
Thank you. :slight_smile:

Does that still work?What permissions this bot would have in group?

this request not longer possible as i found out.There no ways anymore to do smth like so unless you host it in your own pc\server

1 Like