local R={}PlayersService.PlayerAdded:connect(function(U)for R,m in pairs(R)do local A=ChatService:GetSpeaker(R.Name)if A then for R=1,#m,1 do local H=m[R]if H==U.UserId then A:AddMutedSpeaker(U.Name)end end end end end)PlayersService.PlayerRemoving:connect(function(U)R[U]=nil end)EventFolder.SetBlockedUserIdsRequest.OnServerEvent:connect(function(U,m)if type(m)~="\116\097\098\108\101"then return end R[U]=m local A=ChatService:GetSpeaker(U.Name)if A then for R=1,#m,1 do if type(m[R])=="\110\117\109\098\101\114"then local U=PlayersService:GetPlayerByUserId(m[R])if U then A:AddMutedSpeaker(U.Name)end end end end end)EventFolder.GetInitDataRequest.OnServerInvoke=function(R)local U=ChatService:GetSpeaker(R.Name)if not(U and U:GetPlayer())then CreatePlayerSpeakerObject(R)U=ChatService:GetSpeaker(R.Name)end local m={}m.Channels={}m.SpeakerExtraData={}for R,A in pairs(U:GetChannelList())do local H=ChatService:GetChannel(A)if H then local R={A;H:GetWelcomeMessageForSpeaker(U),H:GetHistoryLogForSpeaker(U);H.ChannelNameColor}table.insert(m.Channels,R)end end for R,U in pairs(ChatService:GetSpeakerList())do local A=ChatService:GetSpeaker(U)m.SpeakerExtraData[U]=A.ExtraData end return m end local function U(R,U,m)local A=ChatService:GetSpeaker(R)local H=ChatService:GetChannel(U)if A then if H then if H.Joinable then if not A:IsInChannel(H.Name)then A:JoinChannel(H.Name)else A:SetMainChannel(H.Name)A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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\'",H.Name)),"\123\082\066\088\095\078\065\077\069\125",H.Name),H.Name)end else A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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 \'"..(U.."\'\046")),"\123\082\066\088\095\078\065\077\069\125",U),m)end else A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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 \'"..(U.."\' \100\111\101\115 \110\111\116 \101\120\105\115\116\046")),"\123\082\066\088\095\078\065\077\069\125",U),m)end end end local function m(R,U,m)local A=ChatService:GetSpeaker(R)local H=ChatService:GetChannel(U)if A then if A:IsInChannel(U)then if H.Leavable then A:LeaveChannel(H.Name)A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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\'",U)),"\123\082\066\088\095\078\065\077\069\125",H.Name),"\083\121\115\116\101\109")else A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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 \'"..(U.."\'\046")),"\123\082\066\088\095\078\065\077\069\125",U),m)end else A:SendSystemMessage(string.gsub(ChatLocalization:Get("\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 \'"..(U.."\'\046")),"\123\082\066\088\095\078\065\077\069\125",U),m)end end end ChatService:RegisterProcessCommandsFunction("\100\101\102\097\117\108\116\095\099\111\109\109\097\110\100\115",function(R,A,H)if(string.sub(A,1,6)):lower()=="\047\106\111\105\110 "then U(R,string.sub(A,7),H)return true elseif(string.sub(A,1,3)):lower()=="\047\106 "then U(R,string.sub(A,4),H)return true elseif(string.sub(A,1,7)):lower()=="\047\108\101\097\118\101 "then m(R,string.sub(A,8),H)return true elseif(string.sub(A,1,3)):lower()=="\047\108 "then m(R,string.sub(A,4),H)return true elseif string.sub(A,1,3)=="\047\101 "or string.sub(A,1,7)=="\047\101\109\111\116\101 "then return true end return false end)if ChatSettings.GeneralChannelName and ChatSettings.GeneralChannelName~=""then local R=ChatService:AddChannel(ChatSettings.GeneralChannelName)R.Leavable=false R.AutoJoin=true R:RegisterGetWelcomeMessageFunction(function(R)if RunService:IsStudio()then return nil end local U=R:GetPlayer()if U then local R,m=pcall(function()return Chat:CanUserChatAsync(U.UserId)end)if R and not m then return""end end end)end local A=ChatService:AddChannel("\083\121\115\116\101\109")A.Leavable=false A.AutoJoin=true A.WelcomeMessage=ChatLocalization:Get("\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")A.SpeakerJoined:connect(function(R)A:MuteSpeaker(R)end)local function H(R)if R:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then local U=require(R)if type(U)=="\102\117\110\099\116\105\111\110"then U(ChatService)end end end local P=Chat:WaitForChild("\067\104\097\116\077\111\100\117\108\101\115")P.ChildAdded:connect(function(R)local U,m=pcall(H,R)if not U and m then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(R.Name..("\058 "..m)))end end)for R,U in pairs(P:GetChildren())do local m,A=pcall(H,U)if not m and A then print("\069\114\114\111\114 \114\117\110\110\105\110\103 \109\111\100\117\108\101 "..(U.Name..("\058 "..A)))end end PlayersService.PlayerRemoving:connect(function(R)if ChatService:GetSpeaker(R.Name)then ChatService:RemoveSpeaker(R.Name)end end)

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

	BlockedUserIdsMap[player] = blockedUserIdsList
	local speaker = ChatService:GetSpeaker(player.Name)
	if speaker then
		for i = 1, #blockedUserIdsList do
			if type(blockedUserIdsList[i]) == "number" then
				local blockedPlayer = PlayersService:GetPlayerByUserId(blockedUserIdsList[i])
				if blockedPlayer then
					speaker:AddMutedSpeaker(blockedPlayer.Name)
				end
			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 _, 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)
					speaker:SendSystemMessage(
						string.gsub(
							ChatLocalization:Get(
								"GameChat_SwitchChannel_NowInChannel",
								string.format("You are now chatting in channel: '%s'", channel.Name)
							),
						"{RBX_NAME}",channel.Name),
						channel.Name
					)
				end
			else
				speaker:SendSystemMessage(
					string.gsub(
						ChatLocalization:Get(
							"GameChat_ChatServiceRunner_YouCannotJoinChannel",
							("You cannot join channel '" .. channelName .. "'.")
						),
					"{RBX_NAME}",channelName),
					fromChannelName
				)
			end
		else
			speaker:SendSystemMessage(
				string.gsub(
					ChatLocalization:Get(
						"GameChat_ChatServiceRunner_ChannelDoesNotExist",
						("Channel '" .. channelName .. "' does not exist.")
					),
				"{RBX_NAME}",channelName),
				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.gsub(
						ChatLocalization:Get(
							"GameChat_ChatService_YouHaveLeftChannel",
							string.format("You have left channel '%s'", channelName)
						),
					"{RBX_NAME}",channel.Name),
					"System"
				)
			else
				speaker:SendSystemMessage(
					string.gsub(
						ChatLocalization:Get(
							"GameChat_ChatServiceRunner_YouCannotLeaveChannel",
							("You cannot leave channel '" .. channelName .. "'.")
						),
					"{RBX_NAME}",channelName),
					fromChannelName
				)
			end
		else
			speaker:SendSystemMessage(
				string.gsub(
					ChatLocalization:Get(
						"GameChat_ChatServiceRunner_YouAreNotInChannel",
						("You are not in channel '" .. channelName .. "'.")
					),
				"{RBX_NAME}",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)

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:Get(
	"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)
