function chatBarFocusLost(A,O)DoBackgroundFadeIn()chatBarFocusChanged:Fire(false)if A then local A=(ChatBar:GetTextBox()).Text if ChatBar:IsInCustomState()then local O=ChatBar:GetCustomMessage()if O then A=O end local V=ChatBar:CustomStateProcessCompletedMessage(A)ChatBar:ResetCustomState()if V then return end end A=string.sub(A,1,ChatSettings.MaximumMessageLength);(ChatBar:GetTextBox()).Text=""if A~=""then moduleApiTable.MessagePosted:fire(A)if not CommandProcessor:ProcessCompletedChatMessage(A,ChatWindow)then if ChatSettings.DisallowedWhiteSpace then for O=1,#ChatSettings.DisallowedWhiteSpace,1 do if ChatSettings.DisallowedWhiteSpace[O]=="\t"then A=string.gsub(A,ChatSettings.DisallowedWhiteSpace[O]," ")else A=string.gsub(A,ChatSettings.DisallowedWhiteSpace[O],"")end end end A=string.gsub(A,"\n","")A=string.gsub(A,"\091 \093\043"," ")local O=ChatWindow:GetTargetMessageChannel()if O then MessageSender:SendMessage(A,O)else MessageSender:SendMessage(A,nil)end end end end end local A={}function setupChatBarConnections()for O=1,#A,1 do A[O]:Disconnect()end A={}local O=(ChatBar:GetTextBox()).FocusLost:connect(chatBarFocusLost)table.insert(A,O)local V=(ChatBar:GetTextBox()).Focused:connect(chatBarFocused)table.insert(A,V)end setupChatBarConnections()ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(A)if not ChatSettings.ShowUserOwnFilteredMessage then if A.FromSpeaker==LocalPlayer.Name then return end end local O=A.OriginalChannel local V=ChatWindow:GetChannel(O)if V then V:UpdateMessageFiltered(A)end if ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName then local O=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if O then O:UpdateMessageFiltered(A)end end end)EventFolder.OnNewMessage.OnClientEvent:connect(function(A,O)local V=ChatWindow:GetChannel(O)if V then V:AddMessageToChannel(A)if A.FromSpeaker~=LocalPlayer.Name then ChannelsBar:UpdateMessagePostedInChannel(O)end local p=nil if ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName then p=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if p then p:AddMessageToChannel(A)end end moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()end end)EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(A,O)O=O or"\083\121\115\116\101\109"local V=ChatWindow:GetChannel(O)if V then V:AddMessageToChannel(A)ChannelsBar:UpdateMessagePostedInChannel(O)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()if ChatSettings.GeneralChannelName and O~=ChatSettings.GeneralChannelName then local O=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if O then O:AddMessageToChannel(A)end end else warn(string.format("\074\117\115\116 \114\101\099\101\105\118\101\100 \115\121\115\116\101\109 \109\101\115\115\097\103\101 \102\111\114 \099\104\097\110\110\101\108 \073\'\109 \110\111\116 \105\110 \091\037\115\093",O))end end)function HandleChannelJoined(A,O,V,p,c,Q)if ChatWindow:GetChannel(A)then ChatWindow:RemoveChannel(A)end if A==ChatSettings.GeneralChannelName then DidFirstChannelsLoads=true end if p then ChatBar:SetChannelNameColor(A,p)end local Z=ChatWindow:AddChannel(A)if Z then if A=="\065\108\108"then DoSwitchCurrentChannel(A)end if V then local O=1 if#V>ChatSettings.MessageHistoryLengthPerChannel then O=#V-ChatSettings.MessageHistoryLengthPerChannel end for A=O,#V,1 do Z:AddMessageToChannel(V[A])end if c then if ChatSettings.GeneralChannelName and A~=ChatSettings.GeneralChannelName then local A=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if A then A:AddMessagesToChannelByTimeStamp(V,O)end end end end if O~=""then local V={ID=-1;FromSpeaker=nil;SpeakerUserId=0;OriginalChannel=A;IsFiltered=true;MessageLength=string.len(O);Message=trimTrailingSpaces(O);MessageType=ChatConstants.MessageTypeWelcome,Time=os.time(),ExtraData=nil}Z:AddMessageToChannel(V)if Q and not ChatSettings.ShowChannelsBar then if A~=ChatSettings.GeneralChannelName then local A=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if A then A:AddMessageToChannel(V)end end end end DoFadeInFromNewInformation()end end EventFolder.OnChannelJoined.OnClientEvent:connect(function(A,O,V,p)HandleChannelJoined(A,O,V,p,false,true)end)EventFolder.OnChannelLeft.OnClientEvent:connect(function(A)ChatWindow:RemoveChannel(A)DoFadeInFromNewInformation()end)EventFolder.OnMuted.OnClientEvent:connect(function(A) end)EventFolder.OnUnmuted.OnClientEvent:connect(function(A) end)EventFolder.OnMainChannelSet.OnClientEvent:connect(function(A)DoSwitchCurrentChannel(A)end);(coroutine.wrap(function()local A=DefaultChatSystemChatEvents:WaitForChild("\067\104\097\110\110\101\108\078\097\109\101\067\111\108\111\114\085\112\100\097\116\101\100",5)if A then A.OnClientEvent:connect(function(A,O)ChatBar:SetChannelNameColor(A,O)end)end end))()
--// Event for making player say chat message.
function chatBarFocusLost(enterPressed, inputObject)
	DoBackgroundFadeIn()
	chatBarFocusChanged:Fire(false)

	if enterPressed then
		local message = ChatBar:GetTextBox().Text

		if ChatBar:IsInCustomState() then
			local customMessage = ChatBar:GetCustomMessage()
			if customMessage then
				message = customMessage
			end
			local messageSunk = ChatBar:CustomStateProcessCompletedMessage(message)
			ChatBar:ResetCustomState()
			if messageSunk then
				return
			end
		end

		message = string.sub(message, 1, ChatSettings.MaximumMessageLength)

		ChatBar:GetTextBox().Text = ""

		if message ~= "" then
			--// Sends signal to eventually call Player:Chat() to handle C++ side legacy stuff.
			moduleApiTable.MessagePosted:fire(message)

			if not CommandProcessor:ProcessCompletedChatMessage(message, ChatWindow) then
				if ChatSettings.DisallowedWhiteSpace then
					for i = 1, #ChatSettings.DisallowedWhiteSpace do
						if ChatSettings.DisallowedWhiteSpace[i] == "\t" then
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], " ")
						else
							message = string.gsub(message, ChatSettings.DisallowedWhiteSpace[i], "")
						end
					end
				end
				message = string.gsub(message, "\n", "")
				message = string.gsub(message, "[ ]+", " ")

				local targetChannel = ChatWindow:GetTargetMessageChannel()
				if targetChannel then
					MessageSender:SendMessage(message, targetChannel)
				else
					MessageSender:SendMessage(message, nil)
				end
			end
		end
	end
end

local ChatBarConnections = {}
function setupChatBarConnections()
	for i = 1, #ChatBarConnections do
		ChatBarConnections[i]:Disconnect()
	end
	ChatBarConnections = {}

	local focusLostConnection = ChatBar:GetTextBox().FocusLost:connect(chatBarFocusLost)
	table.insert(ChatBarConnections, focusLostConnection)

	local focusGainedConnection = ChatBar:GetTextBox().Focused:connect(chatBarFocused)
	table.insert(ChatBarConnections, focusGainedConnection)
end

setupChatBarConnections()
ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)

EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(messageData)
	if not ChatSettings.ShowUserOwnFilteredMessage then
		if messageData.FromSpeaker == LocalPlayer.Name then
			return
		end
	end

	local channelName = messageData.OriginalChannel
	local channelObj = ChatWindow:GetChannel(channelName)
	if channelObj then
		channelObj:UpdateMessageFiltered(messageData)
	end

	if ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
		local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
		if generalChannel then
			generalChannel:UpdateMessageFiltered(messageData)
		end
	end
end)

EventFolder.OnNewMessage.OnClientEvent:connect(function(messageData, channelName)
	local channelObj = ChatWindow:GetChannel(channelName)
	if channelObj then
		channelObj:AddMessageToChannel(messageData)

		if messageData.FromSpeaker ~= LocalPlayer.Name then
			ChannelsBar:UpdateMessagePostedInChannel(channelName)
		end

		local generalChannel = nil
		if ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
			generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if generalChannel then
				generalChannel:AddMessageToChannel(messageData)
			end
		end

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()
	end
end)

EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(messageData, channelName)
	channelName = channelName or "System"

	local channelObj = ChatWindow:GetChannel(channelName)
	if channelObj then
		channelObj:AddMessageToChannel(messageData)

		ChannelsBar:UpdateMessagePostedInChannel(channelName)

		moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
		moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)

		DoFadeInFromNewInformation()

		if ChatSettings.GeneralChannelName and channelName ~= ChatSettings.GeneralChannelName then
			local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
			if generalChannel then
				generalChannel:AddMessageToChannel(messageData)
			end
		end
	else
		warn(string.format("Just received system message for channel I'm not in [%s]", channelName))
	end
end)

function HandleChannelJoined(
	channel,
	welcomeMessage,
	messageLog,
	channelNameColor,
	addHistoryToGeneralChannel,
	addWelcomeMessageToGeneralChannel
)
	if ChatWindow:GetChannel(channel) then
		--- If the channel has already been added, remove it first.
		ChatWindow:RemoveChannel(channel)
	end

	if channel == ChatSettings.GeneralChannelName then
		DidFirstChannelsLoads = true
	end

	if channelNameColor then
		ChatBar:SetChannelNameColor(channel, channelNameColor)
	end

	local channelObj = ChatWindow:AddChannel(channel)

	if channelObj then
		if channel == "All" then
			DoSwitchCurrentChannel(channel)
		end

		if messageLog then
			local startIndex = 1
			if #messageLog > ChatSettings.MessageHistoryLengthPerChannel then
				startIndex = #messageLog - ChatSettings.MessageHistoryLengthPerChannel
			end

			for i = startIndex, #messageLog do
				channelObj:AddMessageToChannel(messageLog[i])
			end

			if addHistoryToGeneralChannel then
				if ChatSettings.GeneralChannelName and channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessagesToChannelByTimeStamp(messageLog, startIndex)
					end
				end
			end
		end

		if welcomeMessage ~= "" then
			local welcomeMessageObject = {
				ID = -1,
				FromSpeaker = nil,
				SpeakerUserId = 0,
				OriginalChannel = channel,
				IsFiltered = true,
				MessageLength = string.len(welcomeMessage),
				Message = trimTrailingSpaces(welcomeMessage),
				MessageType = ChatConstants.MessageTypeWelcome,
				Time = os.time(),
				ExtraData = nil,
			}
			channelObj:AddMessageToChannel(welcomeMessageObject)

			if addWelcomeMessageToGeneralChannel and not ChatSettings.ShowChannelsBar then
				if channel ~= ChatSettings.GeneralChannelName then
					local generalChannel = ChatWindow:GetChannel(ChatSettings.GeneralChannelName)
					if generalChannel then
						generalChannel:AddMessageToChannel(welcomeMessageObject)
					end
				end
			end
		end

		DoFadeInFromNewInformation()
	end
end

EventFolder.OnChannelJoined.OnClientEvent:connect(function(channel, welcomeMessage, messageLog, channelNameColor)
	HandleChannelJoined(channel, welcomeMessage, messageLog, channelNameColor, false, true)
end)

EventFolder.OnChannelLeft.OnClientEvent:connect(function(channel)
	ChatWindow:RemoveChannel(channel)

	DoFadeInFromNewInformation()
end)

EventFolder.OnMuted.OnClientEvent:connect(function(channel)
	--// Do something eventually maybe?
	--// This used to take away the chat bar in channels the player was muted in.
	--// We found out this behavior was inconvenient for doing chat commands though.
end)

EventFolder.OnUnmuted.OnClientEvent:connect(function(channel)
	--// Same as above.
end)

EventFolder.OnMainChannelSet.OnClientEvent:connect(function(channel)
	DoSwitchCurrentChannel(channel)
end)

coroutine.wrap(function()
	-- ChannelNameColorUpdated may not exist if the client version is older than the server version.
	local ChannelNameColorUpdated = DefaultChatSystemChatEvents:WaitForChild("ChannelNameColorUpdated", 5)
	if ChannelNameColorUpdated then
		ChannelNameColorUpdated.OnClientEvent:connect(function(channelName, channelNameColor)
			ChatBar:SetChannelNameColor(channelName, channelNameColor)
		end)
	end
end)()
