11if 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+
312local RoleList = Config .RoleList
413local ServerID = Config .ServerID
14+
515local cachedPlayerRoles = {}
616local playerSelectedRole = {}
717local 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 ]
3648end
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+ })
4456end
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 )
7584end
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
126133end
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 )
0 commit comments