Advertisement
Squanou

Base

Jun 23rd, 2024 (edited)
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local DiscordHook = require("DiscordHook")
  2. local speaker = peripheral.find("speaker")
  3. local detector = peripheral.find("playerDetector")
  4. local playersInBase = {}
  5.  
  6. if speaker == nil then
  7.     error("Aucun 'speaker' détecté")
  8.     return
  9. end
  10.  
  11. if detector == nil then
  12.     error("Aucun 'playerDetector' détecté")
  13.     return
  14. end
  15.  
  16. -- Fonction pour échapper les caractères spéciaux dans le pseudo
  17. local function escapeUsername(playerName)
  18.     -- Liste des caractères spéciaux à échapper dans Discord
  19.     local specialChars = {"*", "_", "~", "`", "|"}
  20.     local escapedName = playerName
  21.  
  22.     -- Remplacer chaque caractère spécial par son équivalent échappé
  23.     for _, char in ipairs(specialChars) do
  24.         escapedName = escapedName:gsub(char, "\\" .. char)
  25.     end
  26.  
  27.     return escapedName
  28. end
  29.  
  30. local function isPlayerInBase(playerName)
  31.     for _, name in ipairs(playersInBase) do
  32.         if name == playerName then
  33.             return true
  34.         end
  35.     end
  36.     return false
  37. end
  38.  
  39. local function isPlayerInList(player, list)
  40.     for _, name in ipairs(list) do
  41.         if name == player then
  42.             return true
  43.         end
  44.     end
  45.     return false
  46. end
  47.  
  48. local function removeFromList(list, player)
  49.     for i, name in ipairs(list) do
  50.         if name == player then
  51.             table.remove(list, i)
  52.             return
  53.         end
  54.     end
  55. end
  56.  
  57. local function detectNewPlayers()
  58.     local players = detector.getPlayersInRange(200)
  59.  
  60.     local playersToRemove = {}
  61.  
  62.     for _, player in ipairs(playersInBase) do
  63.         if not isPlayerInList(player, players) then
  64.             table.insert(playersToRemove, player)
  65.         end
  66.     end
  67.  
  68.     for _, player in ipairs(playersToRemove) do
  69.         removeFromList(playersInBase, player)
  70.         print(string.format("%s a quitté la base", player))
  71.         local success, hook = DiscordHook.createWebhook("https://discord.com/api/webhooks/1254412774172393482/yp196njdT93B7z8FO2KbykMj6bmad-kHpXE5OgH7Ta_xHqfU_UnimQOM91TlF1dgeFtn")
  72.  
  73.         if not success then
  74.             error("Webhook connection failed! Reason: " .. hook)
  75.         end
  76.  
  77.         if success then
  78.             local message = string.format("TsykiBase> %s a quitté la base", escapeUsername(player))
  79.             hook.send(message, "Server")
  80.         end
  81.     end
  82.  
  83.     if players and #players > 0 then
  84.         for _, player in ipairs(players) do
  85.             if not isPlayerInBase(player) then
  86.                 table.insert(playersInBase, player)
  87.                 print(string.format("%s a rejoint la base", player))
  88.                 speaker.playNote("harp",  1, 1)
  89.                 local success, hook = DiscordHook.createWebhook("https://discord.com/api/webhooks/1254412774172393482/yp196njdT93B7z8FO2KbykMj6bmad-kHpXE5OgH7Ta_xHqfU_UnimQOM91TlF1dgeFtn")
  90.  
  91.                 if not success then
  92.                     error("Webhook connection failed! Reason: " .. hook)
  93.                 end
  94.  
  95.                 if success then
  96.                     local message = string.format("TsykiBase> %s a rejoint la base", escapeUsername(player))
  97.                     hook.send(message, "Server")
  98.                 end
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. while true do
  105.     detectNewPlayers()
  106.     os.sleep(1)
  107. end
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement