Skip to content

Commit 34c6939

Browse files
authored
Merge pull request #3 from RickyBhatti/v2
DiscordAPI v2
2 parents 5487dbc + 828dcd7 commit 34c6939

8 files changed

Lines changed: 217 additions & 184 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Tired of having multiple Discord resources, just to be able to grant Ace permiss
1212
- Lightweight resource, no unrequired code.
1313

1414
## Planned Changes
15-
- Expand the API features.
15+
- Expand the API features. (Check for Discord role, check for Discord permission, etc.)
1616

1717
## In-Game Example
1818
### Ace Permissions (Server console)
@@ -73,6 +73,7 @@ Check out the [releases](https://github.com/RickyBhatti/DiscordAPI/releases) pag
7373
![](https://i.imgur.com/zk757un.png)
7474

7575
DiscordAPI is a very lightweight resource and will not cause any performance issues for you while ensuring permissions and chat tags are correctly and accurately applied.
76+
NOTE: This image shows the performance of the initial release. It doesn't include the performance improvements made in the latest release (v2).
7677

7778
## Credits
7879
Credit to the following resources and their authors for giving me the idea to create a centralized resource.
@@ -85,5 +86,5 @@ If you've found a bug, you can go ahead and create an [issue](https://github.com
8586
If you've improved the resource, feel free to make a [pull request](https://github.com/RickyBhatti/DiscordAPI/pulls)!
8687

8788
## License
88-
Copyright © 2023 [Ricky Bhatti](https://github.com/RickyBhatti).
89+
Copyright © 2024 [Ricky Bhatti](https://github.com/RickyBhatti).
8990
This project is [GNU GPL v3.0](https://github.com/RickyBhatti/DiscordAPI/blob/main/LICENSE) licensed.

cl_chat_tags.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TriggerServerEvent( "DiscordAPI:UpdateChatPermissions" )
1+
TriggerServerEvent("DiscordAPI:UpdateChatPermissions")
22

3-
TriggerEvent( "chat:addSuggestion", "/chattag", "Select the tag that appears beside your name.", {
4-
{ name = "ID", help = "Chat tag ID." }
5-
} )
3+
TriggerEvent("chat:addSuggestion", "/chattag", "Select the tag that appears beside your name.", {
4+
{name = "ID", help = "Chat tag ID."}
5+
})

fxmanifest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ game "gta5"
44
name "DiscordAPI"
55
description "An all one in solution for Discord API, that controls chat tags, permissions."
66
author "ricky"
7-
version "v1.0"
7+
version "v2.0-dev"
88

99
lua54 "yes"
1010

sv_ace_perms.lua

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
if not Config.AcePermsEnabled then return end
22

3-
local groups = Config.Groups
3+
local pairs = pairs
4+
local tostring = tostring
5+
6+
local _ExecuteCommand = ExecuteCommand
7+
local _GetPlayerName = GetPlayerName
48

9+
local groups = Config.Groups
510
local permissions = Config.Permissions
611

712
local groupsToRemove = {}
@@ -12,54 +17,56 @@ local groupRemove = "remove_principal identifier.%s %s"
1217
local permissionAdd = "add_ace identifier.%s \"%s\" allow"
1318
local permissionRemove = "remove_ace identifier.%s \"%s\" allow"
1419

15-
local function applyPermissions( source )
16-
local identifiers = GetIdentifiersTable( source )
20+
local function applyPermissions(source)
21+
local identifiers = GetIdentifiersTable(source)
22+
local license, discord = identifiers.license, identifiers.discord
1723

18-
for _, v in pairs( groupsToRemove ) do
19-
ExecuteCommand( groupRemove:format( identifiers.license, v ) )
24+
for _, v in pairs(groupsToRemove) do
25+
_ExecuteCommand(groupRemove:format(license, v))
2026
end
2127

22-
for k, _ in pairs( permissionsToRemove ) do
23-
ExecuteCommand( permissionRemove:format( identifiers.license, k ) )
28+
for k, _ in pairs(permissionsToRemove) do
29+
_ExecuteCommand(permissionRemove:format(license, k))
2430
end
2531

26-
if identifiers.discord then
27-
local roles = getRoles( source )
32+
if not discord then return end
33+
34+
local roles = getRoles(source)
35+
if roles == nil then return end
2836

29-
if roles == nil then return end
37+
local name = _GetPlayerName(source)
3038

31-
for _, v in pairs( roles ) do
32-
local groupInformation = groups[ tostring( v ) ]
33-
local permissionInformation = permissions[ tostring( v ) ]
39+
for _, v in pairs(roles) do
40+
local groupInformation = groups[tostring(v)]
41+
local permissionInformation = permissions[tostring(v)]
3442

35-
if groupInformation then
36-
ExecuteCommand( groupAdd:format( identifiers.license, groupInformation ) )
37-
Log( "Granted \"" .. groupInformation.. "\" to " .. GetPlayerName( source ) .. " (" .. identifiers.license .. ")." )
38-
end
43+
if not groupInformation then goto skipGroupInformation end
44+
ExecuteCommand(groupAdd:format(license, groupInformation))
45+
Log("Granted \"" .. groupInformation.. "\" to " .. name .. " (" .. license .. ").")
46+
::skipGroupInformation::
3947

40-
if permissionInformation then
41-
Log( "Granting permission set for role ID: " .. v .. "." )
42-
for _, v2 in pairs( permissionInformation ) do
43-
ExecuteCommand( permissionAdd:format( identifiers.license, v2 ) )
44-
Log( "Granted \"" .. v2.. "\" to " .. GetPlayerName( source ) .. " (" .. identifiers.license .. ") due to them having the role ID: " .. v .. "." )
45-
end
46-
end
48+
if not permissionInformation then goto skipPermissionInformation end
49+
Log("Granting permission set for role ID: " .. v .. ".")
50+
for _, v2 in pairs(permissionInformation) do
51+
ExecuteCommand(permissionAdd:format(license, v2))
52+
Log("Granted \"" .. v2.. "\" to " .. name .. " (" .. license .. ") due to them having the role ID: " .. v .. ".")
4753
end
54+
::skipPermissionInformation::
4855
end
4956
end
5057

51-
AddEventHandler( "playerConnecting", function()
52-
applyPermissions( source )
53-
end )
58+
AddEventHandler("playerConnecting", function()
59+
applyPermissions(source)
60+
end)
5461

55-
for _, v in pairs( groups ) do
56-
table.insert( groupsToRemove, v )
62+
for _, v in pairs(groups) do
63+
table.insert(groupsToRemove, v)
5764
end
5865

59-
for _, v in pairs( permissions ) do
60-
for _, v2 in pairs( v ) do
61-
if tostring( v2 ) ~= "" and not permissionsToRemove[ tostring( v2 ) ] then
62-
permissionsToRemove[ v2 ] = true
66+
for _, v in pairs(permissions) do
67+
for _, v2 in pairs(v) do
68+
if tostring(v2) ~= "" and not permissionsToRemove[tostring(v2)] then
69+
permissionsToRemove[v2] = true
6370
end
6471
end
6572
end

sv_chat_tags.lua

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,141 @@
11
if not Config.ChatRolesEnabled then return end
22

3+
local pairs = pairs
4+
local tostring = tostring
5+
local tonumber = tonumber
6+
local find = string.find
7+
local insert = table.insert
8+
9+
local _IsPlayerAceAllowed = IsPlayerAceAllowed
10+
local _TriggerClientEvent = TriggerClientEvent
11+
312
local RoleList = Config.RoleList
413
local ServerID = Config.ServerID
14+
515
local cachedPlayerRoles = {}
616
local playerSelectedRole = {}
717
local playerStaffChatStatus = {}
818

9-
local function syncTags( source )
10-
local src = source
11-
local identifiers = GetIdentifiersTable( src )
19+
local function syncTags(source)
20+
local identifiers = GetIdentifiersTable(source)
1221

13-
if identifiers.discord then
14-
local roles = getRoles( src )
15-
local rolesAllowed = {}
16-
local highestRole, highestRoleIndex = nil, nil
22+
if not identifiers.discord then return end
1723

18-
for i = 1, #RoleList do
19-
if tostring( RoleList[ i ][ 1 ] ) == "0" then
20-
table.insert( rolesAllowed, i )
21-
highestRoleIndex = i
22-
end
23-
if roles ~= nil then
24-
for _, v in pairs( roles ) do
25-
if tostring( RoleList[ i ][ 1 ] ) == tostring( v ) then
26-
table.insert( rolesAllowed, i )
27-
highestRole, highestRoleIndex = v, i
28-
end
29-
end
24+
local roles = getRoles(source)
25+
local rolesAllowed = {}
26+
local highestRole, highestRoleIndex = nil, nil
27+
28+
for i = 1, #RoleList do
29+
if tostring(RoleList[i][1]) == "0" then
30+
insert(rolesAllowed, i)
31+
highestRoleIndex = i
32+
end
33+
34+
if roles == nil then goto skip end
35+
36+
for _, v in pairs(roles) do
37+
if tostring(RoleList[i][1]) == tostring(v) then
38+
insert(rolesAllowed, i)
39+
highestRole, highestRoleIndex = v, i
3040
end
3141
end
3242

33-
cachedPlayerRoles[ src ] = rolesAllowed
34-
playerSelectedRole[ src ] = RoleList[ highestRoleIndex ][ 2 ]
43+
::skip::
3544
end
45+
46+
cachedPlayerRoles[source] = rolesAllowed
47+
playerSelectedRole[source] = RoleList[highestRoleIndex][2]
3648
end
3749

38-
local function sendMessage( source, message )
39-
TriggerClientEvent( "chat:addMessage", source, {
40-
color = { 255, 0, 0 },
50+
local function sendMessage(source, message)
51+
TriggerClientEvent("chat:addMessage", source, {
52+
color = {255, 0, 0},
4153
multiline = true,
42-
args = { "Server", tostring( message ) }
43-
} )
54+
args = {"Server", tostring(message)}
55+
})
4456
end
4557

46-
AddEventHandler( "chatMessage", function( source, name, message )
58+
AddEventHandler("chatMessage", function(source, name, message)
4759
CancelEvent()
4860

49-
local source, args, role = tonumber( source ), SplitString( message ), playerSelectedRole[ source ]
61+
local source, args, role = tonumber(source), SplitString(message), playerSelectedRole[source]
5062
if role == nil then
51-
syncTags( source )
52-
role = playerSelectedRole[ source ]
63+
syncTags(source)
64+
role = playerSelectedRole[source]
5365
end
5466

55-
if not string.find( args[ 1 ], "/" ) and not playerStaffChatStatus[ source ] then
56-
if ServerID then
57-
TriggerClientEvent( "chatMessage", -1, "^*^7" .. source .. " | " .. role .. name .. "^r^7: " .. message )
58-
else
59-
TriggerClientEvent( "chatMessage", -1, "^*^7" .. role .. name .. "^r^7: " .. message )
60-
end
61-
elseif not string.find( args[ 1 ], "/" ) and playerStaffChatStatus[ source ] then
62-
for k, _ in pairs( playerStaffChatStatus ) do
63-
TriggerClientEvent( "chatMessage", k, "^*^7[^8Staff Chat^7] " .. role .. name .. "^r^7: " .. message )
67+
if not find(args[1], "/") and not playerStaffChatStatus[source] then
68+
local message = ServerID and ("^*^7" .. source .. " | " .. role .. name .. "^r^7: " .. message) or ("^*^7" .. role .. name .. "^r^7: " .. message)
69+
_TriggerClientEvent("chatMessage", -1, message)
70+
elseif not find(args[1], "/") and playerStaffChatStatus[source] then
71+
for k, _ in pairs(playerStaffChatStatus) do
72+
_TriggerClientEvent("chatMessage", k, "^*^7[^8Staff Chat^7] " .. role .. name .. "^r^7: " .. message)
6473
end
6574
end
66-
end )
75+
end)
6776

68-
local function setData( source )
69-
syncTags( source )
77+
local function setData(source)
78+
syncTags(source)
79+
80+
if not _IsPlayerAceAllowed(source, "DiscordAPI:StaffChat") then return end
7081

71-
if IsPlayerAceAllowed( source, "DiscordAPI:StaffChat" ) then
72-
playerStaffChatStatus[ tonumber( source ) ] = false
73-
TriggerClientEvent( "DiscordAPI:staffChatStatus", source, false )
74-
end
82+
playerStaffChatStatus[tonumber(source)] = false
83+
_TriggerClientEvent("DiscordAPI:staffChatStatus", source, false)
7584
end
7685

77-
RegisterNetEvent( "DiscordAPI:UpdateChatPermissions" )
78-
AddEventHandler( "DiscordAPI:UpdateChatPermissions", function()
79-
setData( source )
80-
end )
86+
RegisterNetEvent("DiscordAPI:UpdateChatPermissions")
87+
AddEventHandler("DiscordAPI:UpdateChatPermissions", function()
88+
setData(source)
89+
end)
8190

82-
AddEventHandler( "playerDropped", function()
83-
cachedPlayerRoles[ source ] = nil
84-
playerSelectedRole[ source ] = nil
85-
end )
91+
AddEventHandler("playerDropped", function()
92+
cachedPlayerRoles[source] = nil
93+
playerSelectedRole[source] = nil
94+
end)
8695

87-
RegisterCommand( "chattag", function( source, args, rawCommand )
96+
RegisterCommand("chattag", function(source, args, rawCommand)
8897
local src = source
89-
local roleList = cachedPlayerRoles[ src ]
98+
local roleList = cachedPlayerRoles[src]
9099
if roleList == nil then
91-
syncTags( src )
92-
roleList = cachedPlayerRoles[ src ]
100+
syncTags(src)
101+
roleList = cachedPlayerRoles[src]
93102
end
94103

95104
if #args == 0 then
96-
for k, v in pairs( roleList ) do
97-
TriggerClientEvent( "chatMessage", src, "^*" .. k .. "^r^7: " .. RoleList[ v ][ 2 ] )
105+
for k, v in pairs(roleList) do
106+
_TriggerClientEvent("chatMessage", src, "^*" .. k .. "^r^7: " .. RoleList[v][2])
98107
end
99-
sendMessage( src, "Use /chattag <id> to select the tag you'd like to use.^r^7" )
108+
sendMessage(src, "Use /chattag <id> to select the tag you'd like to use.^r^7")
100109
elseif #args == 1 then
101-
local selectedID = tonumber( args[ 1 ] )
110+
local selectedID = tonumber(args[1])
102111
if selectedID > 0 and selectedID <= #roleList then
103-
playerSelectedRole[ src ] = RoleList[ roleList[ selectedID ] ][ 2 ]
104-
sendMessage( src, "You've selected to use the following as your tag: " .. playerSelectedRole[ src ] .. "^r^7" )
112+
playerSelectedRole[src] = RoleList[roleList[selectedID]][2]
113+
sendMessage(src, "You've selected to use the following as your tag: " .. playerSelectedRole[src] .. "^r^7")
105114
else
106-
sendMessage( src, "You've selected out of range.^r^7" )
115+
sendMessage(src, "You've selected out of range.^r^7")
107116
end
108117
end
109-
end )
118+
end)
110119

111-
local function StaffChat( source )
112-
local src = tonumber( source )
113-
if IsPlayerAceAllowed( src, "DiscordAPI:StaffChat" ) then
114-
playerStaffChatStatus[ src ] = not ( playerStaffChatStatus[ src ] or false )
120+
local function StaffChat(source)
121+
local src = tonumber(source)
122+
if not _IsPlayerAceAllowed(src, "DiscordAPI:StaffChat") then sendMessage(src, "You're not authorized to enter staff chat.") return end
115123

116-
if playerStaffChatStatus[ src ] then
117-
TriggerClientEvent( "DiscordAPI:staffChatStatus", src, true )
118-
sendMessage( src, "You've entered staff chat." )
119-
else
120-
TriggerClientEvent( "DiscordAPI:staffChatStatus", src, false )
121-
sendMessage( src, "You've left staff chat." )
122-
end
124+
playerStaffChatStatus[src] = not (playerStaffChatStatus[src] or false)
125+
126+
if playerStaffChatStatus[src] then
127+
_TriggerClientEvent("DiscordAPI:staffChatStatus", src, true)
128+
sendMessage(src, "You've entered staff chat.")
123129
else
124-
sendMessage( src, "You're not authorized to enter staff chat." )
130+
_TriggerClientEvent("DiscordAPI:staffChatStatus", src, false)
131+
sendMessage(src, "You've left staff chat.")
125132
end
126133
end
127134

128-
RegisterCommand( "staffchat", function( source )
129-
StaffChat( source )
130-
end, false )
135+
RegisterCommand("staffchat", function(source)
136+
StaffChat(source)
137+
end, false)
131138

132-
RegisterCommand( "sc", function( source )
133-
StaffChat( source )
134-
end, false )
139+
RegisterCommand("sc", function(source)
140+
StaffChat(source)
141+
end, false)

sv_config.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ Config = {
3232
ServerID = true, -- If you'd like the resource to disable the players server ID before their name in chat.
3333

3434
DiscordRoles = { -- Roles you'd like to be able to check for using the Discord API. Name is anything you'd like it to be.
35-
[ "Public Cop" ] = "793043744726843412",
35+
["Public Cop"] = "793043744726843412",
3636
},
3737

3838
AcePermsEnabled = true, -- If you'd like the resource to grant ace permissions or not.
3939
Groups = { -- These are the group ace permissions that you'd like to grant based off of Discord roles.
40-
[ "793044205269680128" ] = "group.admin", --// Role: Owner
41-
[ "793043765224931388" ] = "group.member", --// Role: Member
40+
["793044205269680128"] = "group.admin", --// Role: Owner
41+
["793043765224931388"] = "group.member", --// Role: Member
4242
},
4343
Permissions = { -- Special command permissions you'd like to grant users with certain Discord roles.
44-
[ "793044205269680128" ] = { --// Role: Owner
44+
["793044205269680128"] = { --// Role: Owner
4545
"aop.*",
4646
"chattoggle",
4747
}

0 commit comments

Comments
 (0)