local L=script.Parent.Parent.Parent local G=game:GetService("\080\108\097\121\101\114\115")local Y=game:GetService("\067\104\097\116")local U=L:WaitForChild("\067\108\105\101\110\116\067\104\097\116\077\111\100\117\108\101\115")local Z=require(U:WaitForChild("\067\104\097\116\083\101\116\116\105\110\103\115"))local i={ChattingToSelf=1,NoMatches=2;MultipleMatches=3}local function B(L,Y,U)if Y==L then return Y,i.ChattingToSelf end local B=G:GetPlayers()for G,Y in pairs(B)do if string.lower(Y.Name)==string.lower(L)then return Y.Name,nil end end local p=0 local j=nil if Z.PlayerDisplayNamesEnabled then for G,U in pairs(B)do if U.Name~=Y and string.lower(U.DisplayName)==string.lower(L)then p=p+1 j=U.Name end end end if p==1 then return j,nil elseif p==0 then if U==L then return U,i.ChattingToSelf else return L,i.NoMatches end elseif p>=2 then return L,i.MultipleMatches end end local function p(L,Y)local U={}local Z=""local i=G:GetPlayers()for G,Z in pairs(i)do if Z.Name~=Y and string.lower(Z.DisplayName)==string.lower(L)then table.insert(U,Z.Name)end end for L,G in pairs(U)do if L~=#U then Z=Z..("\064"..(G.."\044 "))else Z=Z..("\064"..G)end end return Z end return{CommandErrorCodes=i,getUserNameFromChattedName=B,getUsersWithDisplayNameString=p}
--!nonstrict
local chatFolder = script.Parent.Parent.Parent

local Players = game:GetService("Players")
local Chat = game:GetService("Chat")
local ReplicatedModules = chatFolder:WaitForChild("ClientChatModules")
local ChatSettings = require(ReplicatedModules:WaitForChild("ChatSettings"))

local CommandErrorCodes = {
	ChattingToSelf = 1,
	NoMatches = 2,
	MultipleMatches = 3,
}

--returns (string) UserName as the first value and an error code
local function getUserNameFromChattedName(targetDisplayName, speakerUserName, speakerDisplayName)
	--we're looking for non-speaker matches, so if the displayName matches the userName we need to short circuit
	if speakerUserName == targetDisplayName then
		return speakerUserName, CommandErrorCodes.ChattingToSelf
	end

	--first, scan for userName matches
	local players = Players:GetPlayers()

	for _, player in pairs(players) do
		if string.lower(player.Name) == string.lower(targetDisplayName) then
			return player.Name, nil
		end
	end

	--then, if there we no userName matches, scan for DisplayName matches. return true if there is one non-local-player match
	local numDisplayNameMatches = 0
	local lastDisplayNameMatch = nil

	if ChatSettings.PlayerDisplayNamesEnabled then
		for _, player in pairs(players) do
			if
				player.Name ~= speakerUserName
				and string.lower(player.DisplayName) == string.lower(targetDisplayName)
			then
				numDisplayNameMatches = numDisplayNameMatches + 1
				lastDisplayNameMatch = player.Name
			end
		end
	end

	if numDisplayNameMatches == 1 then
		return lastDisplayNameMatch, nil
	elseif numDisplayNameMatches == 0 then
		if speakerDisplayName == targetDisplayName then
			return speakerDisplayName, CommandErrorCodes.ChattingToSelf
		else
			return targetDisplayName, CommandErrorCodes.NoMatches
		end
	elseif numDisplayNameMatches >= 2 then
		return targetDisplayName, CommandErrorCodes.MultipleMatches
	end
end

local function getUsersWithDisplayNameString(targetDisplayName, speakerUserName)
	local displayNameMatches = {}
	local matchingUsersText = ""

	--first, scan for userName matches
	local players = Players:GetPlayers()

	for _, player in pairs(players) do
		if player.Name ~= speakerUserName and string.lower(player.DisplayName) == string.lower(targetDisplayName) then
			table.insert(displayNameMatches, player.Name)
		end
	end

	for i, matchingUserName in pairs(displayNameMatches) do
		if i ~= #displayNameMatches then
			matchingUsersText = matchingUsersText .. "@" .. matchingUserName .. ", "
		else
			matchingUsersText = matchingUsersText .. "@" .. matchingUserName
		end
	end

	return matchingUsersText
end

return {
	CommandErrorCodes = CommandErrorCodes,
	getUserNameFromChattedName = getUserNameFromChattedName,
	getUsersWithDisplayNameString = getUsersWithDisplayNameString,
}
