Advertisement
Guest User

Untitled

a guest
Aug 20th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.79 KB | None | 0 0
  1. ------------------------------------
  2. --- Discord Chat Roles by Badger ---
  3. ------------------------------------
  4.  
  5. --[[
  6.     List in order of least priority to highest with
  7.     highest priority overtaking role before it if
  8.     they have that discord role.
  9. ]]--
  10. -- CONFIG --
  11. roleList = {
  12. {0, "👦🏻 ^4Civilian: | "}, -- 1
  13. {743454425288278108, "👦🏻 ^4Verified Civilian: | "}, -- 2
  14. {743454425640599648, "⛔ ^1T-Mod: | "}, -- 3
  15. {743454425640599649, "⛔ ️^1Moderator: | "}, -- 4
  16. {743454425640599651, "💎 ^6Management: | "}, -- 5
  17. {743454425686736965, "👑 ^1Owner: | "}, -- 6
  18. {745341866601349172, "💸 ^0VIP: | "}, -- 7
  19. {743454424789418010, "💸 ^0MVP: | "}, -- 8
  20. {745041522453643456, "💸 ^0MVP+: | "}, -- 9
  21. }
  22.  
  23. sendBlockMessages = true;
  24.  
  25.  
  26. -- CODE --
  27. function sendMsg(firstline, msg, to)
  28.     TriggerClientEvent('chat:addMessage', to, {
  29.         template = '<div style="padding: 0.5vw; margin: 0.5vw; background-color: rgba(93, 93, 93, 0.25); border-radius: 3px;">{0} <br> {1}</div>',
  30.         args = { firstline, msg }
  31.     });
  32. end
  33.  
  34. -- For allowing colored chat
  35. allowedColors = {3, 4, 10, 11, 12, 13, 14}
  36. allowedRed = {10, 11, 12, 13, 14}
  37.  
  38.  
  39. --- Code ---
  40.  
  41. function sleep (a)
  42.     local sec = tonumber(os.clock() + a);
  43.     while (os.clock() < sec) do
  44.     end
  45. end
  46. local function has_value (tab, val)
  47.     for index, value in ipairs(tab) do
  48.         if value == val then
  49.             return true
  50.         end
  51.     end
  52.  
  53.     return false
  54. end
  55. function stringsplit(inputstr, sep)
  56.     if sep == nil then
  57.         sep = "%s"
  58.     end
  59.     local t={} ; i=1
  60.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  61.         t[i] = str
  62.         i = i + 1
  63.     end
  64.     return t
  65. end
  66. function get_index (tab, val)
  67.     local counter = 1
  68.     for index, value in ipairs(tab) do
  69.         if value == val then
  70.             return counter
  71.         end
  72.         counter = counter + 1
  73.     end
  74.  
  75.     return nil
  76. end
  77.  
  78. roleTracker = {}
  79. roleAccess = {}
  80. chatcolorTracker = {}
  81. local availColors =
  82. {
  83.     ['DiscordChatRoles.Access.Donator'] = {
  84.         ['White'] = {'^0'},
  85.         ['Green'] = {'^2'},
  86.         ['Yellow'] = {'^3'},
  87.         ['Blue'] = {'^4'},
  88.         ['Light Blue'] = {'^5'},
  89.         ['Purple'] = {'^6'},
  90.         ['White'] = {'^7'},
  91.         ['Pink'] = {'^9'},
  92.         ['Police'] = {'^1', '^4'},
  93.         ['Police2'] = {'^4', '^1'},
  94.         ['Christmas'] = {'^2', '^1'},
  95.         ['Christmas2'] = {'^1', '^2'},
  96.     },
  97.     ['DiscordChatRoles.Access.Elite'] = {
  98.         ['RainbowYGB'] = {'^3', '^2', '^4'},
  99.         ['RainbowFull'] = {'^3', '^4', '^1', '^5', '^6', '^7', '^9'},
  100.     },
  101.     ['DiscordChatRoles.Access.Staff'] = {
  102.         ['Red'] = {'^1'},
  103.     }
  104. }
  105. RegisterCommand('chatcolor', function(source, args, rawCommand)
  106.     local theirList = {}
  107.     local colorList = {}
  108.     for k, v in pairs(availColors) do
  109.         -- k = Permission
  110.         -- v = Array of color-selections
  111.         if IsPlayerAceAllowed(source, k) then
  112.             -- They have permission to use these colors:
  113.             for colorName, colorArr in pairs(availColors[k]) do
  114.                 table.insert(theirList, colorName);
  115.                 table.insert(colorList, colorArr);
  116.             end
  117.         end
  118.     end
  119.     if #args == 0 then
  120.         -- List out which ones they have access to
  121.         if #theirList > 0 then
  122.             TriggerClientEvent('chatMessage', source, prefix .. 'You have access to the following Chat-Colors:');
  123.             for i = 1, #theirList do
  124.                 local arr = colorList[i];
  125.                 local example = 'Example'
  126.                 local ex = '';
  127.                 local indCount = 1
  128.                 for j = 1, #example do
  129.                     if indCount > #arr then
  130.                         indCount = 1;
  131.                     end
  132.                     local char = example:sub(j, j);
  133.                     ex = ex .. arr[indCount] .. char;
  134.                     indCount = indCount + 1;
  135.                 end
  136.                 TriggerClientEvent('chatMessage', source, '^9[^4' .. i .. '^9] ^0' .. theirList[i] .. " ---> " .. ex);
  137.             end
  138.         else
  139.             TriggerClientEvent('chatMessage', source, prefix .. '^1ERROR: You have no Chat-Colors :( - Consider donating for some :)');
  140.         end
  141.     else
  142.         local selection = args[1]
  143.         if tonumber(selection) ~= nil then
  144.             local sel = tonumber(selection);
  145.             if sel <= #theirList then
  146.                 chatcolorTracker[source] = colorList[sel];
  147.                 TriggerClientEvent('chatMessage', source, prefix .. 'You have set your Chat-Color to ^4' .. theirList[sel]);
  148.             else
  149.                 -- Invalid selection
  150.                 TriggerClientEvent('chatMessage', source, '^1ERROR: That is not a valid selection...')
  151.             end
  152.         else
  153.             -- Print it's not a number
  154.             TriggerClientEvent('chatMessage', source, '^1ERROR: You did not put in a number...')
  155.         end
  156.     end
  157. end)
  158. RegisterCommand('cc', function(source, args, rawCommand)
  159.     local theirList = {}
  160.     local colorList = {}
  161.     for k, v in pairs(availColors) do
  162.         -- k = Permission
  163.         -- v = Array of color-selections
  164.         if IsPlayerAceAllowed(source, k) then
  165.             -- They have permission to use these colors:
  166.             for colorName, colorArr in pairs(availColors[k]) do
  167.                 table.insert(theirList, colorName);
  168.                 table.insert(colorList, colorArr);
  169.             end
  170.         end
  171.     end
  172.     if #args == 0 then
  173.         -- List out which ones they have access to
  174.         if #theirList > 0 then
  175.             TriggerClientEvent('chatMessage', source, prefix .. 'You have access to the following Chat-Colors:');
  176.             for i = 1, #theirList do
  177.                 local arr = colorList[i];
  178.                 local example = 'Example'
  179.                 local ex = '';
  180.                 local indCount = 1
  181.                 for j = 1, #example do
  182.                     if indCount > #arr then
  183.                         indCount = 1;
  184.                     end
  185.                     local char = example:sub(j, j);
  186.                     ex = ex .. arr[indCount] .. char;
  187.                     indCount = indCount + 1;
  188.                 end
  189.                 TriggerClientEvent('chatMessage', source, '^9[^4' .. i .. '^9] ^0' .. theirList[i] .. " ---> " .. ex);
  190.             end
  191.         else
  192.             TriggerClientEvent('chatMessage', source, prefix .. '^1ERROR: You have no Chat-Colors :( - Consider donating for some :)');
  193.         end
  194.     else
  195.         local selection = args[1]
  196.         if tonumber(selection) ~= nil then
  197.             local sel = tonumber(selection);
  198.             if sel <= #theirList then
  199.                 chatcolorTracker[source] = colorList[sel];
  200.                 TriggerClientEvent('chatMessage', source, prefix .. 'You have set your Chat-Color to ^4' .. theirList[sel]);
  201.             else
  202.                 -- Invalid selection
  203.                 TriggerClientEvent('chatMessage', source, '^1ERROR: That is not a valid selection...')
  204.             end
  205.         else
  206.             -- Print it's not a number
  207.             TriggerClientEvent('chatMessage', source, '^1ERROR: You did not put in a number...')
  208.         end
  209.     end
  210. end)
  211. function setContains(set, key)
  212.     return set[key] ~= nil
  213. end
  214. function msg(src, mesg)
  215.     TriggerClientEvent('chatMessage', src, prefix .. mesg)
  216. end
  217. function msgRaw(src, mesg)
  218.     TriggerClientEvent('chatMessage', src, mesg)
  219. end
  220. prefix = '^9[^5DiscordChatRoles^9] ^3'
  221. RegisterCommand('chattag', function(source, args, rawCommand)
  222.     local steamID = GetPlayerIdentifiers(source)[1];
  223.     local accessChat = roleAccess[steamID]
  224.     if accessChat == nil then
  225.         -- Need them to say something in chat first 1 time
  226.         msg(source, 'You need to say something in chat before you run this command...')
  227.         return;
  228.     end
  229.     if #args == 0 then
  230.         -- Just list their chat tags
  231.         msg(source, "You have access to the following Chat-Tags:")
  232.         for i = 1, #accessChat do
  233.             msgRaw(source, '^9[^4' .. i .. '^9] ^r' .. roleList[accessChat[i]][2])
  234.         end
  235.         msg(source, "Use /chattag <id> to change your Chat-Tag")
  236.     elseif #args == 1 then
  237.         -- Change their chat tag
  238.         if tonumber(args[1]) ~= nil then
  239.             if accessChat[tonumber(args[1])] ~= nil then
  240.                 -- Set their chatTag
  241.                 roleTracker[steamID] = accessChat[tonumber(args[1])]
  242.                 msg(source, 'Your Chat-Tag has now been set to:^r ' .. roleList[accessChat[tonumber(args[1])]][2])
  243.             else
  244.                 -- Not a valid chat tag ID
  245.                 msg(source, '^1ERROR: This is not a valid Chat-Tag id')
  246.             end
  247.         else
  248.             -- It's not a valid number
  249.             msg(source, '^1ERROR: This is not a number...')
  250.         end
  251.     else
  252.         -- Not correct syntax
  253.         msg(source, '^1ERROR: Not proper usage. /chattag <id>')
  254.     end
  255. end)
  256. chatNotEnabled = {}
  257. RegisterNetEvent('DiscordChatRoles:DisableChat')
  258. AddEventHandler('DiscordChatRoles:DisableChat', function(src)
  259.     chatNotEnabled[src] = true;
  260. end)
  261. RegisterNetEvent('DiscordChatRoles:EnableChat')
  262. AddEventHandler('DiscordChatRoles:EnableChat', function(src)
  263.     chatNotEnabled[src] = nil;
  264. end)
  265.  
  266. AddEventHandler('chatMessage', function(source, name, msg)
  267.     local args = stringsplit(msg)
  268.     CancelEvent()
  269.     local src = source
  270.     if not string.find(args[1], "/") and setContains(roleTracker, GetPlayerIdentifiers(source)[1]) and
  271.         not has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) and not (chatNotEnabled[src] ~= nil) then
  272.         local roleStr = roleList[roleTracker[GetPlayerIdentifiers(source)[1]]][2]
  273.         local colors = {'^0', '^2', '^3', '^4', '^5', '^6', '^7', '^8', '^9'}
  274.         local staffColors = {'^1', '^8'}
  275.         local hasColors = false
  276.         local hasRed = false
  277.         local roleNum = roleTracker[GetPlayerIdentifiers(source)[1]]
  278.         for i = 1, #colors do
  279.             local checkFor = "%" .. tostring(colors[i])
  280.             if string.match(msg, checkFor) ~= nil then
  281.                 hasColors = true
  282.             end
  283.         end
  284.         for i = 1, #staffColors do
  285.             if string.find(msg, "%" .. staffColors[i]) ~= nil then
  286.                 hasRed = true
  287.             end
  288.         end
  289.         local dontSend = false
  290.         if hasColors then
  291.             -- Check if they have required role
  292.             if not has_value(allowedColors, tonumber(roleNum)) then
  293.                 dontSend = true
  294.                 TriggerClientEvent('chatMessage', source, "^7[^1DiscordChatRoles^7] ^1You cannot use colored chat since you are not a donator...")
  295.             end
  296.         end
  297.         if hasRed then
  298.             -- Check if they have required role
  299.             if not has_value(allowedRed, tonumber(roleNum)) then
  300.                 dontSend = true
  301.                 TriggerClientEvent('chatMessage', source, "^7[^1DiscordChatRoles^7] ^1You cannot use the color RED in chat since you are not staff...")
  302.             end
  303.         end
  304.         local theirColor = chatcolorTracker[source];
  305.         local finalMessage = msg;
  306.         if theirColor ~= nil then
  307.             finalMessage = ''
  308.             local indCount = 1;
  309.             for j = 1, #msg do
  310.                 if indCount > #theirColor then
  311.                     indCount = 1;
  312.                 end
  313.                 local char = msg:sub(j, j);
  314.                 finalMessage = finalMessage .. theirColor[indCount] .. char;
  315.                 indCount = indCount + 1;
  316.             end
  317.         end
  318.         if not dontSend then
  319.             --TriggerClientEvent('chatMessage', -1, roleStr .. name .. "^7: " .. finalMessage)
  320.             if sendBlockMessages then
  321.                 sendMsg(roleStr .. name .. "^7: ", finalMessage, -1);
  322.             else
  323.                 TriggerClientEvent('chatMessage', -1, roleStr .. name .. "^7: " .. finalMessage);
  324.             end
  325.         end
  326.     end
  327.     if not string.find(args[1], "/") and not has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) and
  328.         not setContains(roleTracker, GetPlayerIdentifiers(source)[1]) and not (chatNotEnabled[src] ~= nil) then
  329.         CancelEvent()
  330.         roleTracker[GetPlayerIdentifiers(source)[1]] = 1
  331.         for k, v in ipairs(GetPlayerIdentifiers(src)) do
  332.             if string.sub(v, 1, string.len("discord:")) == "discord:" then
  333.                 identifierDiscord = v
  334.             end
  335.         end
  336.         local roleStr = roleList[1][2]
  337.         local roleNum = 1
  338.         local hasAccess = {}
  339.         table.insert(hasAccess, roleNum)
  340.         if identifierDiscord then
  341.             local roleIDs = exports.discord_perms:GetRoles(src)
  342.             -- Loop through roleList and set their role up:
  343.             if not (roleIDs == false) then
  344.                 for i = 1, #roleList do
  345.                     for j = 1, #roleIDs do
  346.                         local roleID = roleIDs[j]
  347.                         if (tostring(roleList[i][1]) == tostring(roleID)) then
  348.                             roleStr = roleList[i][2]
  349.                             table.insert(hasAccess, i)
  350.                             roleNum = i
  351.                         end
  352.                     end
  353.                 end
  354.                 roleAccess[GetPlayerIdentifiers(source)[1]] = hasAccess;
  355.             else
  356.                 print(GetPlayerName(src) .. " has not gotten their permissions cause roleIDs == false")
  357.             end
  358.         end
  359.         roleTracker[GetPlayerIdentifiers(source)[1]] = roleNum
  360.         local colors = {'^0', '^2', '^3', '^4', '^5', '^6', '^7', '^8', '^9'}
  361.         local staffColors = {'^1', '^8'}
  362.         local hasColors = false
  363.         local hasRed = false
  364.         for i = 1, #colors do
  365.             local checkFor = "%" .. tostring(colors[i])
  366.             if string.match(msg, checkFor) ~= nil then
  367.                 hasColors = true
  368.             end
  369.         end
  370.         for i = 1, #staffColors do
  371.             if string.find(msg, "%" .. staffColors[i]) ~= nil then
  372.                 hasRed = true
  373.             end
  374.         end
  375.         local dontSend = false
  376.         if hasColors then
  377.             -- Check if they have required role
  378.             if not has_value(allowedColors, tonumber(roleNum)) then
  379.                 dontSend = true
  380.                 TriggerClientEvent('chatMessage', source, "^7[^1DiscordChatRoles^7] ^1You cannot use colored chat since you are not a donator...")
  381.             end
  382.         end
  383.         if hasRed then
  384.             -- Check if they have required role
  385.             if not has_value(allowedRed, tonumber(roleNum)) then
  386.                 dontSend = true
  387.                 TriggerClientEvent('chatMessage', source, "^7[^1DiscordChatRoles^7] ^1You cannot use the color RED in chat since you are not staff...")
  388.             end
  389.         end
  390.         local theirColor = chatcolorTracker[source];
  391.         local finalMessage = msg;
  392.         if theirColor ~= nil then
  393.             finalMessage = ''
  394.             local indCount = 1;
  395.             for j = 1, #msg do
  396.                 if indCount > #theirColor then
  397.                     indCount = 1;
  398.                 end
  399.                 local char = msg:sub(j, j);
  400.                 finalMessage = finalMessage .. theirColor[indCount] .. char;
  401.                 indCount = indCount + 1;
  402.             end
  403.         end
  404.         if not dontSend then
  405.             --TriggerClientEvent('chatMessage', -1, roleStr .. name .. "^7: " .. finalMessage)
  406.             if (sendBlockMessages) then
  407.                 sendMsg(roleStr .. name .. "^7: ", finalMessage, -1);
  408.             else
  409.                 TriggerClientEvent('chatMessage', -1, roleStr .. name .. "^7: " .. finalMessage);
  410.             end
  411.         end
  412.     elseif has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) and not string.find(args[1], "/") and not (chatNotEnabled[src] ~= nil) then
  413.         -- Run client event for all and check perms
  414.         CancelEvent()
  415.         msg = "^7[^1StaffChat^7] ^5(^1" .. name .. "^5) ^9" .. msg
  416.         TriggerClientEvent('Permissions:CheckPermsClient', -1, msg)
  417.         --print("It gets here 1")
  418.     end
  419. end)
  420. RegisterNetEvent('Print:PrintDebug')
  421. AddEventHandler('Print:PrintDebug', function(msg)
  422.     print(msg)
  423.     TriggerClientEvent('chatMessage', source, "^7[^1Badger's Scripts^7] ^1DEBUG ^7" .. msg)
  424. end)
  425. inStaffChat = {}
  426. RegisterNetEvent("DiscordChatRoles:CheckPerms")
  427. AddEventHandler("DiscordChatRoles:CheckPerms", function(msg)
  428.     -- Check if they have permissions
  429.     --print("It gets to start")
  430.     local src = source
  431.     if IsPlayerAceAllowed(src, "StaffChat.Toggle") then
  432.         TriggerClientEvent('chatMessage', src, msg)
  433.         --print("It gets to end")
  434.     else
  435.         -- Doesn't have perms
  436.     end
  437. end)
  438. RegisterCommand("staffchat", function(source, args, rawCommand)
  439.     -- Check if they can run the command
  440.     if IsPlayerAceAllowed(source, "StaffChat.Toggle") then
  441.         if #args == 1 then
  442.             if args[1] == "toggle" then
  443.                 -- Turn off their staffchat and return
  444.                 TriggerClientEvent('DiscordChatRoles:StaffChat:Toggle', source)
  445.                 return
  446.             end
  447.         end
  448.         if not has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) then
  449.             table.insert(inStaffChat, GetPlayerIdentifiers(source)[1])
  450.             TriggerClientEvent('chatMessage', source, "^7[^1StaffChat^7] ^5StaffChat has been toggled ^2ON")
  451.         else
  452.             table.remove(inStaffChat, get_index(inStaffChat, GetPlayerIdentifiers(source)[1]))
  453.             TriggerClientEvent('chatMessage', source, "^7[^1StaffChat^7] ^5StaffChat has been toggled ^1OFF")
  454.         end
  455.     end
  456. end)
  457.  
  458. RegisterCommand("sc", function(source, args, rawCommand)
  459.     -- Check if they can run the command
  460.     if IsPlayerAceAllowed(source, "StaffChat.Toggle") then
  461.         if #args == 1 then
  462.             if args[1] == "toggle" then
  463.                 -- Turn off their staffchat and return
  464.                 TriggerClientEvent('DiscordChatRoles:StaffChat:Toggle', source)
  465.                 return
  466.             end
  467.         end
  468.         if not has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) then
  469.             table.insert(inStaffChat, GetPlayerIdentifiers(source)[1])
  470.             TriggerClientEvent('chatMessage', source, "^7[^1StaffChat^7] ^5StaffChat has been toggled ^2ON")
  471.         else
  472.             table.remove(inStaffChat, get_index(inStaffChat, GetPlayerIdentifiers(source)[1]))
  473.             TriggerClientEvent('chatMessage', source, "^7[^1StaffChat^7] ^5StaffChat has been toggled ^1OFF")
  474.         end
  475.     end
  476. end)
  477.  
  478. AddEventHandler("playerDropped", function()
  479.     if has_value(inStaffChat, GetPlayerIdentifiers(source)[1]) then
  480.         table.remove(inStaffChat, get_index(inStaffChat, GetPlayerIdentifiers(source)[1]))
  481.     end
  482. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement