· 4 years ago · Apr 03, 2021, 12:36 AM
1--!nolint UnknownGlobal
2--[[ 
3RoManager In-Game Commands
4ADONIS ONLY
5]]--
6
7-- Your API key (generate one with the .apikey generate command)
8local API_KEY = ''
9
10-- Your Group ID (make sure to change this - incorrectly setting this may result in unexpected behavior)
11local GROUP_ID = 0
12
13-- Whether or not to check if the user running the command has permission to change ranks
14local CHECK_PERMISSIONS = true
15
16--------------------------------
17local baseUrl = 'https://api.romanager.jaydensar.net/v1'
18
19local http = game:GetService('HttpService')
20
21local group = game:GetService('GroupService'):GetGroupInfoAsync(GROUP_ID)
22print('[RoManager Plugin] Using group '.. group.Name ..' ('.. group.Id.. ')')
23
24local function getMatchingRole(roleInput, plr)
25	local group = game:GetService('GroupService'):GetGroupInfoAsync(GROUP_ID)
26	local groupRank = plr:GetRankInGroup(GROUP_ID)
27
28	if (string.len(tonumber(roleInput) or '') == string.len(roleInput)) then
29		for _,role in ipairs(group.Roles) do
30			if role.Rank == tonumber(roleInput) then
31				if CHECK_PERMISSIONS then
32					if not (role.Rank >= groupRank) then
33						return role
34					end
35				else
36					return role
37				end
38			end
39		end
40	end
41
42	for _, role in ipairs(group.Roles) do
43		if string.match(string.lower(role.Name), string.lower(roleInput)) then
44			if CHECK_PERMISSIONS then
45				if not (role.Rank >= groupRank) then
46					return role
47				end
48			else
49				return role
50			end
51		end
52	end
53end
54
55return function()
56	server.Commands.RoleCommand = {
57		Prefix = server.Settings.Prefix;
58		Commands = { 'role', 'setrole', 'setrank' };
59		Args = { 'player', 'role' };
60		Description = '[RoManager] Assigns a role to a member';
61		AdminLevel = 'Players';
62		Function = function(plr, args)
63			assert(args[1], 'You must provide a player to role.')
64			assert(args[2], 'You must provide a role to assign.')
65		
66			local userGroupPermissions = http:JSONDecode(http:RequestAsync({
67				Url = baseUrl..'/permissions/'..plr.UserId,
68				Method = 'GET',
69				Headers = {
70					Authorization = API_KEY
71				},
72			}).Body)
73			
74			if CHECK_PERMISSIONS then
75				assert(userGroupPermissions.groupMembershipPermissions.changeRank, 'You do not have the Manage lower-ranked member ranks permission.')
76			end
77			
78			local role = getMatchingRole(args[2], plr)
79			
80			assert(role, 'Could not find a role matching '.. args[2].. '.')
81			
82			for _,splr in pairs(service.GetPlayers(plr, args[1])) do
83				local splrGroupMemberships = http:JSONDecode(http:GetAsync('https://groups.rprxy.xyz/v1/users/' ..splr.UserId.. '/groups/roles')).data
84				local splrRank
85				for _,groupMembership in ipairs(splrGroupMemberships) do
86					if groupMembership.group.id == GROUP_ID then
87						splrRank = groupMembership.role.rank
88					end
89				end
90
91				if CHECK_PERMISSIONS then
92					assert(splrRank, 'Unable to get the group role of '.. splr.Name.. '.')
93					if (splrRank >= plr:GetRankInGroup(GROUP_ID)) then
94						continue
95					end
96				end
97				local ans = server.Remote.GetGui(plr, 'YesNoPrompt', {
98					Question = 'Are you sure you want to role '.. splr.Name.. ' (' .. splr.UserId.. ') to '.. role.Name .. '?';
99				})
100				
101				if ans == 'Yes' then
102					http:RequestAsync({
103						Url = baseUrl.. '/role/'.. splr.UserId,
104						Method = 'PATCH',
105						Headers = {
106							['Content-Type'] = 'application/json',
107							['Authorization'] = API_KEY
108						},
109						Body = http:JSONEncode({
110							roleName = role.Name
111						})
112					})
113				end
114			end
115		end
116	}
117end
118