local t={}PlayersService.PlayerAdded:connect(function(s)for t,P in pairs(t)do local G=ChatService:GetSpeaker(t.Name)if G then for t=1,#P,1 do local W=P[t]if W==s.UserId then G:AddMutedSpeaker(s.Name)end end end end end)PlayersService.PlayerRemoving:connect(function(s)t[s]=nil end)EventFolder.SetBlockedUserIdsRequest.OnServerEvent:Connect(function(s,P)if type(P)~="\116\097\098\108\101"then return end local G={}local W=ChatService:GetSpeaker(s.Name)if W then for t=1,math.min(#P,MAX_BLOCKED_SPEAKERS_PER_REQ),1 do if type(P[t])=="\110\117\109\098\101\114"then table.insert(G,P[t])local s=PlayersService:GetPlayerByUserId(P[t])if s then W:AddMutedSpeaker(s.Name)end end end t[s]=G end end)EventFolder.GetInitDataRequest.OnServerInvoke=function(t)local s=ChatService:GetSpeaker(t.Name)if not(s and s:GetPlayer())then CreatePlayerSpeakerObject(t)s=ChatService:GetSpeaker(t.Name)end local P={}P.Channels={}P.SpeakerExtraData={}for t,G in pairs(s:GetChannelList())do local W=ChatService:GetChannel(G)if W then local t={G;W:GetWelcomeMessageForSpeaker(s);W:GetHistoryLogForSpeaker(s);W.ChannelNameColor}table.insert(P.Channels,t)end end for t,s in pairs(ChatService:GetSpeakerList())do local G=ChatService:GetSpeaker(s)P.SpeakerExtraData[s]=G.ExtraData end return P end local function s(t,s,P)local G=ChatService:GetSpeaker(t)local W=ChatService:GetChannel(s)if G then if W then if W.Joinable then if not G:IsInChannel(W.Name)then G:JoinChannel(W.Name)else G:SetMainChannel(W.Name)local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\083\119\105\116\099\104\067\104\097\110\110\101\108\095\078\111\119\073\110\067\104\097\110\110\101\108",string.format("\089\111\117 \097\114\101 \110\111\119 \099\104\097\116\116\105\110\103 \105\110 \099\104\097\110\110\101\108\058 \'\037\115\'",W.Name),"\082\066\088\095\078\065\077\069",W.Name)G:SendSystemMessage(t,W.Name)end else local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\082\117\110\110\101\114\095\089\111\117\067\097\110\110\111\116\074\111\105\110\067\104\097\110\110\101\108","\089\111\117 \099\097\110\110\111\116 \106\111\105\110 \099\104\097\110\110\101\108 \'"..(s.."\'\046"),"\082\066\088\095\078\065\077\069",s)G:SendSystemMessage(t,P)end else local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\082\117\110\110\101\114\095\067\104\097\110\110\101\108\068\111\101\115\078\111\116\069\120\105\115\116","\067\104\097\110\110\101\108 \'"..(s.."\' \100\111\101\115 \110\111\116 \101\120\105\115\116\046"),"\082\066\088\095\078\065\077\069",s)G:SendSystemMessage(t,P)end end end local function P(t,s,P)local G=ChatService:GetSpeaker(t)local W=ChatService:GetChannel(s)if G then if G:IsInChannel(s)then if W.Leavable then G:LeaveChannel(W.Name)local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\095\089\111\117\072\097\118\101\076\101\102\116\067\104\097\110\110\101\108",string.format("\089\111\117 \104\097\118\101 \108\101\102\116 \099\104\097\110\110\101\108 \'\037\115\'",s),"\082\066\088\095\078\065\077\069",W.Name)G:SendSystemMessage(t,"\083\121\115\116\101\109")else local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\082\117\110\110\101\114\095\089\111\117\067\097\110\110\111\116\076\101\097\118\101\067\104\097\110\110\101\108","\089\111\117 \099\097\110\110\111\116 \108\101\097\118\101 \099\104\097\110\110\101\108 \'"..(s.."\'\046"),"\082\066\088\095\078\065\077\069",s)G:SendSystemMessage(t,P)end else local t=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\082\117\110\110\101\114\095\089\111\117\065\114\101\078\111\116\073\110\067\104\097\110\110\101\108","\089\111\117 \097\114\101 \110\111\116 \105\110 \099\104\097\110\110\101\108 \'"..(s.."\'\046"),"\082\066\088\095\078\065\077\069",s)G:SendSystemMessage(t,P)end end end ChatService:RegisterProcessCommandsFunction("\100\101\102\097\117\108\116\095\099\111\109\109\097\110\100\115",function(t,G,W)if(string.sub(G,1,6)):lower()=="\047\106\111\105\110 "then s(t,string.sub(G,7),W)return true elseif(string.sub(G,1,3)):lower()=="\047\106 "then s(t,string.sub(G,4),W)return true elseif(string.sub(G,1,7)):lower()=="\047\108\101\097\118\101 "then P(t,string.sub(G,8),W)return true elseif(string.sub(G,1,3)):lower()=="\047\108 "then P(t,string.sub(G,4),W)return true end return false end)if ChatSettings.GeneralChannelName and ChatSettings.GeneralChannelName~=""then local t=ChatService:AddChannel(ChatSettings.GeneralChannelName)t.Leavable=false t.AutoJoin=true t:RegisterGetWelcomeMessageFunction(function(t)if RunService:IsStudio()then return nil end local s=t:GetPlayer()if s then local t,P=pcall(function()return Chat:CanUserChatAsync(s.UserId)end)if t and not P then return""end end end)end local G=ChatService:AddChannel("\083\121\115\116\101\109")G.Leavable=false G.AutoJoin=true G.WelcomeMessage=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\067\104\097\116\083\101\114\118\105\099\101\082\117\110\110\101\114\095\083\121\115\116\101\109\067\104\097\110\110\101\108\087\101\108\099\111\109\101\077\101\115\115\097\103\101","\084\104\105\115 \099\104\097\110\110\101\108 \105\115 \102\111\114 \115\121\115\116\101\109 \097\110\100 \103\097\109\101 \110\111\116\105\102\105\099\097\116\105\111\110\115\046")G.SpeakerJoined:connect(function(t)G:MuteSpeaker(t)end)local function W(t)if t:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then local s=require(t)if type(s)=="\102\117\110\099\116\105\111\110"then s(ChatService)end end end local f=Chat:WaitForChild("\067\104\097\116\077\111\100\117\108\101\115")f.ChildAdded:connect(function(t)local s,P=pcall(W,t)if not s and P then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(t.Name..("\058 "..P)))end end)for t,s in pairs(f:GetChildren())do local P,G=pcall(W,s)if not P and G then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(s.Name..("\058 "..G)))end end PlayersService.PlayerRemoving:connect(function(t)if ChatService:GetSpeaker(t.Name)then ChatService:RemoveSpeaker(t.Name)end end)
-- Map storing Player -> Blocked user Ids.
local BlockedUserIdsMap = {}

PlayersService.PlayerAdded:connect(function(newPlayer)
	for player, blockedUsers in pairs(BlockedUserIdsMap) do
		local speaker = ChatService:GetSpeaker(player.Name)
		if speaker then
			for i = 1, #blockedUsers do
				local blockedUserId = blockedUsers[i]
				if blockedUserId == newPlayer.UserId then
					speaker:AddMutedSpeaker(newPlayer.Name)
				end
			end
		end
	end
end)

PlayersService.PlayerRemoving:connect(function(removingPlayer)
	BlockedUserIdsMap[removingPlayer] = nil
end)

EventFolder.SetBlockedUserIdsRequest.OnServerEvent:Connect(function(player, blockedUserIdsList)
	if type(blockedUserIdsList) ~= "table" then
		return
	end

	local prunedBlockedUserIdsList = {}
	local speaker = ChatService:GetSpeaker(player.Name)
	if speaker then
		for i = 1, math.min(#blockedUserIdsList, MAX_BLOCKED_SPEAKERS_PER_REQ) do
			if type(blockedUserIdsList[i]) == "number" then
				table.insert(prunedBlockedUserIdsList, blockedUserIdsList[i])

				local blockedPlayer = PlayersService:GetPlayerByUserId(blockedUserIdsList[i])
				if blockedPlayer then
					speaker:AddMutedSpeaker(blockedPlayer.Name)
				end
			end
		end

		-- We only want to store the first
		-- MAX_BLOCKED_SPEAKERS_PER_REQ number of ids as needed
		BlockedUserIdsMap[player] = prunedBlockedUserIdsList
	end
end)

EventFolder.GetInitDataRequest.OnServerInvoke = function(playerObj)
	local speaker = ChatService:GetSpeaker(playerObj.Name)
	if not (speaker and speaker:GetPlayer()) then
		CreatePlayerSpeakerObject(playerObj)
		speaker = ChatService:GetSpeaker(playerObj.Name)
	end

	local data = {}
	data.Channels = {}
	data.SpeakerExtraData = {}

	for _, channelName in pairs(speaker:GetChannelList()) do
		local channelObj = ChatService:GetChannel(channelName)
		if channelObj then
			local channelData = {
				channelName,
				channelObj:GetWelcomeMessageForSpeaker(speaker),
				channelObj:GetHistoryLogForSpeaker(speaker),
				channelObj.ChannelNameColor,
			}

			table.insert(data.Channels, channelData)
		end
	end

	for _, oSpeakerName in pairs(ChatService:GetSpeakerList()) do
		local oSpeaker = ChatService:GetSpeaker(oSpeakerName)
		data.SpeakerExtraData[oSpeakerName] = oSpeaker.ExtraData
	end

	return data
end

local function DoJoinCommand(speakerName, channelName, fromChannelName)
	local speaker = ChatService:GetSpeaker(speakerName)
	local channel = ChatService:GetChannel(channelName)

	if speaker then
		if channel then
			if channel.Joinable then
				if not speaker:IsInChannel(channel.Name) then
					speaker:JoinChannel(channel.Name)
				else
					speaker:SetMainChannel(channel.Name)
					local msg = ChatLocalization:FormatMessageToSend(
						"GameChat_SwitchChannel_NowInChannel",
						string.format("You are now chatting in channel: '%s'", channel.Name),
						"RBX_NAME",
						channel.Name
					)
					speaker:SendSystemMessage(msg, channel.Name)
				end
			else
				local msg = ChatLocalization:FormatMessageToSend(
					"GameChat_ChatServiceRunner_YouCannotJoinChannel",
					"You cannot join channel '" .. channelName .. "'.",
					"RBX_NAME",
					channelName
				)
				speaker:SendSystemMessage(msg, fromChannelName)
			end
		else
			local msg = ChatLocalization:FormatMessageToSend(
				"GameChat_ChatServiceRunner_ChannelDoesNotExist",
				"Channel '" .. channelName .. "' does not exist.",
				"RBX_NAME",
				channelName
			)
			speaker:SendSystemMessage(msg, fromChannelName)
		end
	end
end

local function DoLeaveCommand(speakerName, channelName, fromChannelName)
	local speaker = ChatService:GetSpeaker(speakerName)
	local channel = ChatService:GetChannel(channelName)

	if speaker then
		if speaker:IsInChannel(channelName) then
			if channel.Leavable then
				speaker:LeaveChannel(channel.Name)
				local msg = ChatLocalization:FormatMessageToSend(
					"GameChat_ChatService_YouHaveLeftChannel",
					string.format("You have left channel '%s'", channelName),
					"RBX_NAME",
					channel.Name
				)
				speaker:SendSystemMessage(msg, "System")
			else
				local msg = ChatLocalization:FormatMessageToSend(
					"GameChat_ChatServiceRunner_YouCannotLeaveChannel",
					("You cannot leave channel '" .. channelName .. "'."),
					"RBX_NAME",
					channelName
				)
				speaker:SendSystemMessage(msg, fromChannelName)
			end
		else
			local msg = ChatLocalization:FormatMessageToSend(
				"GameChat_ChatServiceRunner_YouAreNotInChannel",
				("You are not in channel '" .. channelName .. "'."),
				"RBX_NAME",
				channelName
			)
			speaker:SendSystemMessage(msg, fromChannelName)
		end
	end
end

ChatService:RegisterProcessCommandsFunction("default_commands", function(fromSpeaker, message, channel)
	if string.sub(message, 1, 6):lower() == "/join " then
		DoJoinCommand(fromSpeaker, string.sub(message, 7), channel)
		return true
	elseif string.sub(message, 1, 3):lower() == "/j " then
		DoJoinCommand(fromSpeaker, string.sub(message, 4), channel)
		return true
	elseif string.sub(message, 1, 7):lower() == "/leave " then
		DoLeaveCommand(fromSpeaker, string.sub(message, 8), channel)
		return true
	elseif string.sub(message, 1, 3):lower() == "/l " then
		DoLeaveCommand(fromSpeaker, string.sub(message, 4), channel)
		return true
	end

	return false
end)

if ChatSettings.GeneralChannelName and ChatSettings.GeneralChannelName ~= "" then
	local allChannel = ChatService:AddChannel(ChatSettings.GeneralChannelName)

	allChannel.Leavable = false
	allChannel.AutoJoin = true

	allChannel:RegisterGetWelcomeMessageFunction(function(speaker)
		if RunService:IsStudio() then
			return nil
		end
		local player = speaker:GetPlayer()
		if player then
			local success, canChat = pcall(function()
				return Chat:CanUserChatAsync(player.UserId)
			end)
			if success and not canChat then
				return ""
			end
		end
	end)
end

local systemChannel = ChatService:AddChannel("System")
systemChannel.Leavable = false
systemChannel.AutoJoin = true
systemChannel.WelcomeMessage = ChatLocalization:FormatMessageToSend(
	"GameChat_ChatServiceRunner_SystemChannelWelcomeMessage",
	"This channel is for system and game notifications."
)

systemChannel.SpeakerJoined:connect(function(speakerName)
	systemChannel:MuteSpeaker(speakerName)
end)

local function TryRunModule(module)
	if module:IsA("ModuleScript") then
		local ret = require(module)
		if type(ret) == "function" then
			ret(ChatService)
		end
	end
end

local modules = Chat:WaitForChild("ChatModules")
modules.ChildAdded:connect(function(child)
	local success, returnval = pcall(TryRunModule, child)
	if not success and returnval then
		print("Error running module " .. child.Name .. ": " .. returnval)
	end
end)

for _, module in pairs(modules:GetChildren()) do
	local success, returnval = pcall(TryRunModule, module)
	if not success and returnval then
		print("Error running module " .. module.Name .. ": " .. returnval)
	end
end

PlayersService.PlayerRemoving:connect(function(playerObj)
	if ChatService:GetSpeaker(playerObj.Name) then
		ChatService:RemoveSpeaker(playerObj.Name)
	end
end)
