String Lua字符串匹配/查找

String Lua字符串匹配/查找,string,lua,String,Lua,我试图用下面的代码在我们的游戏服务器中捕获不需要的用户名。目前,它将允许任何人通过,不管他们的用户名是什么。我已经让它工作了,如果它用单个单词停止与服务器的连接,但到目前为止,我正在努力从字符串中提取禁止的单词 local unauthNames = { "admin", "word1", "etc" } for name in pairs(unauthNames) do if string.find(string.lower

我试图用下面的代码在我们的游戏服务器中捕获不需要的用户名。目前,它将允许任何人通过,不管他们的用户名是什么。我已经让它工作了,如果它用单个单词停止与服务器的连接,但到目前为止,我正在努力从字符串中提取禁止的单词

local unauthNames = { "admin", "word1", "etc" }

for name in pairs(unauthNames) do
    if string.find(string.lower(GetPlayerName(source)), unauthNames[name]) then
        print(playerName .. " has been kicked for having an unauthorised name")
        setKickReason("Your name is not permitted. Please check your name is not offensive or associated with blacklisted organisations.")
        CancelEvent()
        break
    end
end

这是使用discord集成的已部署版本。

请勿使用。由于各种原因,将用户名中的单词列入黑名单是一个非常糟糕的主意。此外,您的if条件看起来会比
if GetPlayerName(source):lower():find(禁止)好得多,那么
unauthNames
看起来是什么样子?这是一个序列吗?这将有助于为您的代码提供更多的上下文。抱歉unauthNames是一个变量列表,本地unauthNames={“admin”、“word1”、“etc”},它
string.gsub
返回用户名中的第一个字母数字字符,您缺少一个
。那个代码不可能工作。
AddEventHandler("playerConnecting", function(playerName, setKickReason)
    playerName = string.gsub(playerName, "%W", ""):lower()
    for k, v in pairs(unauthNames) do
      local g, f = playerName:find(string.lower(v)
      if g or f then
          print(playerName .. " has been kicked for having an unauthorised name")
          setKickReason("Your name is not permitted. Please check your name is not offensive or associated with blacklisted organisations.")
          CancelEvent()
      end
    end
  end)
AddEventHandler("playerConnecting", function(playerName, setKickReason)
    playerName = (string.gsub(string.gsub(string.gsub(string.gsub(playerName,  "-", ""), ",", ""), "%.", ""), " ", ""):lower())
    for k, v in pairs(unauthNames) do
      local g, f = playerName:find(string.lower(v))
      if g or f then
        local id = source;
        local ids = ExtractIdentifiers(id);
        local ip = ids.ip;
        local steam = ids.steam:gsub("steam:", "");
        local steamDec = tostring(tonumber(steam,16));
        steam = "https://steamcommunity.com/profiles/" .. steamDec;
        local gameLicense = ids.license;
        local discord = ids.discord;
        local realName = GetPlayerName(source);
          print(playerName .. " has been kicked for having an unauthorised name")
          sendToDisc("Anti-abuse - Player kicked for having an unauthorised name",
          'Steam: **' .. steam .. '**\n' ..
          'GameLicense: **' .. gameLicense .. '**\n' ..
          'Discord UID: **' .. discord:gsub('discord:', '') .. '**\n'
          .. 'Name: **' .. realName ..'**\n'
          .. 'Blacklisted Item: **' .. v ..'**\n');
          setKickReason("[Anti-abuse] Your name, or part of it, is not permitted. Please check your name is not offensive, does not contain weblinks, odd characters or other restricted content: " ..v)
          CancelEvent()
      end
    end
  end)