local k={}PlayersService.PlayerAdded:connect(function(f)for k,u in pairs(k)do local Z=ChatService:GetSpeaker(k.Name)if Z then for k=1,#u,1 do local W=u[k]if W==f.UserId then Z:AddMutedSpeaker(f.Name)end end end end end)PlayersService.PlayerRemoving:connect(function(f)k[f]=nil end)EventFolder.SetBlockedUserIdsRequest.OnServerEvent:connect(function(f,u)k[f]=u local Z=ChatService:GetSpeaker(f.Name)if Z then for k=1,#u,1 do local f=PlayersService:GetPlayerByUserId(u[k])if f then Z:AddMutedSpeaker(f.Name)end end end end)EventFolder.GetInitDataRequest.OnServerInvoke=function(k)local f=ChatService:GetSpeaker(k.Name)if not(f and f:GetPlayer())then CreatePlayerSpeakerObject(k)f=ChatService:GetSpeaker(k.Name)end local u={}u.Channels={}u.SpeakerExtraData={}for k,Z in pairs(f:GetChannelList())do local W=ChatService:GetChannel(Z)if W then local k={Z;W.WelcomeMessage;W:GetHistoryLogForSpeaker(f),W.ChannelNameColor}table.insert(u.Channels,k)end end for k,f in pairs(ChatService:GetSpeakerList())do local Z=ChatService:GetSpeaker(f)u.SpeakerExtraData[f]=Z.ExtraData end return u end local function f(k,f,u)local Z=ChatService:GetSpeaker(k)local W=ChatService:GetChannel(f)if Z then if W then if W.Joinable then if not Z:IsInChannel(W.Name)then Z:JoinChannel(W.Name)else Z:SetMainChannel(W.Name)Z:SendSystemMessage(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),W.Name)end else Z:SendSystemMessage("\089\111\117 \099\097\110\110\111\116 \106\111\105\110 \099\104\097\110\110\101\108 \'"..(f.."\'\046"),u)end else Z:SendSystemMessage("\067\104\097\110\110\101\108 \'"..(f.."\' \100\111\101\115 \110\111\116 \101\120\105\115\116\046"),u)end end end local function u(k,f,u)local Z=ChatService:GetSpeaker(k)local W=ChatService:GetChannel(f)if Z then if Z:IsInChannel(f)then if W.Leavable then Z:LeaveChannel(W.Name)Z:SendSystemMessage(string.format("\089\111\117 \104\097\118\101 \108\101\102\116 \099\104\097\110\110\101\108 \'\037\115\'",W.Name),"\083\121\115\116\101\109")else Z:SendSystemMessage("\089\111\117 \099\097\110\110\111\116 \108\101\097\118\101 \099\104\097\110\110\101\108 \'"..(f.."\'\046"),u)end else Z:SendSystemMessage("\089\111\117 \097\114\101 \110\111\116 \105\110 \099\104\097\110\110\101\108 \'"..(f.."\'\046"),u)end end end ChatService:RegisterProcessCommandsFunction("\100\101\102\097\117\108\116\095\099\111\109\109\097\110\100\115",function(k,Z,W)if(string.sub(Z,1,6)):lower()=="\047\106\111\105\110 "then f(k,string.sub(Z,7),W)return true elseif(string.sub(Z,1,3)):lower()=="\047\106 "then f(k,string.sub(Z,4),W)return true elseif(string.sub(Z,1,7)):lower()=="\047\108\101\097\118\101 "then u(k,string.sub(Z,8),W)return true elseif(string.sub(Z,1,3)):lower()=="\047\108 "then u(k,string.sub(Z,4),W)return true elseif string.sub(Z,1,3)=="\047\101 "or string.sub(Z,1,7)=="\047\101\109\111\116\101 "then return true end return false end)local Z=ChatService:AddChannel("\065\108\108")local W=ChatService:AddChannel("\083\121\115\116\101\109")Z.Leavable=false Z.AutoJoin=true W.Leavable=false W.AutoJoin=true W.WelcomeMessage="\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"W.SpeakerJoined:connect(function(k)W:MuteSpeaker(k)end)local function w(k)if k:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then local f=require(k)if type(f)=="\102\117\110\099\116\105\111\110"then f(ChatService)end end end local Y=(game:GetService("\067\104\097\116")):WaitForChild("\067\104\097\116\077\111\100\117\108\101\115")Y.ChildAdded:connect(function(k)local f,u=pcall(w,k)if not f and u then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(k.Name..("\058 "..u)))end end)for k,f in pairs(Y:GetChildren())do local u,Z=pcall(w,f)if not u and Z then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(f.Name..("\058 "..Z)))end end local a=game:GetService("\080\108\097\121\101\114\115")a.PlayerRemoving:connect(function(k)if ChatService:GetSpeaker(k.Name)then ChatService:RemoveSpeaker(k.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)
	BlockedUserIdsMap[player] = blockedUserIdsList
	local speaker = ChatService:GetSpeaker(player.Name)
	if speaker then
		for i = 1, #blockedUserIdsList do
			local blockedPlayer = PlayersService:GetPlayerByUserId(blockedUserIdsList[i])
			if blockedPlayer then
				speaker:AddMutedSpeaker(blockedPlayer.Name)
			end
		end
	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 i, channelName in pairs(speaker:GetChannelList()) do
		local channelObj = ChatService:GetChannel(channelName)
		if channelObj then
			local channelData = {
				channelName,
				channelObj.WelcomeMessage,
				channelObj:GetHistoryLogForSpeaker(speaker),
				channelObj.ChannelNameColor,
			}

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

	for i, 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)
					speaker:SendSystemMessage(
						string.format("You are now chatting in channel: '%s'", channel.Name),
						channel.Name
					)
				end
			else
				speaker:SendSystemMessage("You cannot join channel '" .. channelName .. "'.", fromChannelName)
			end
		else
			speaker:SendSystemMessage("Channel '" .. channelName .. "' does not exist.", 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)
				speaker:SendSystemMessage(string.format("You have left channel '%s'", channel.Name), "System")
			else
				speaker:SendSystemMessage("You cannot leave channel '" .. channelName .. "'.", fromChannelName)
			end
		else
			speaker:SendSystemMessage("You are not in channel '" .. channelName .. "'.", 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
	elseif string.sub(message, 1, 3) == "/e " or string.sub(message, 1, 7) == "/emote " then
		-- Just don't show these in the chatlog. The animation script listens on these.
		return true
	end

	return false
end)

local allChannel = ChatService:AddChannel("All")
local systemChannel = ChatService:AddChannel("System")

allChannel.Leavable = false
allChannel.AutoJoin = true

systemChannel.Leavable = false
systemChannel.AutoJoin = true
systemChannel.WelcomeMessage = "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 = game:GetService("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 i, 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

local Players = game:GetService("Players")
Players.PlayerRemoving:connect(function(playerObj)
	if ChatService:GetSpeaker(playerObj.Name) then
		ChatService:RemoveSpeaker(playerObj.Name)
	end
end)
