function chatBarFocusLost(l,w)DoBackgroundFadeIn()chatBarFocusChanged:Fire(false)if l then local l=(ChatBar:GetTextBox()).Text if ChatBar:IsInCustomState()then local w=ChatBar:GetCustomMessage()if w then l=w end local U=ChatBar:CustomStateProcessCompletedMessage(l)ChatBar:ResetCustomState()if U then return end end;(ChatBar:GetTextBox()).Text=""if l~=""then moduleApiTable.MessagePosted:fire(l)if not CommandProcessor:ProcessCompletedChatMessage(l,ChatWindow)then if ChatSettings.DisallowedWhiteSpace then for w=1,#ChatSettings.DisallowedWhiteSpace,1 do if ChatSettings.DisallowedWhiteSpace[w]=="\t"then l=string.gsub(l,ChatSettings.DisallowedWhiteSpace[w]," ")else l=string.gsub(l,ChatSettings.DisallowedWhiteSpace[w],"")end end end l=string.gsub(l,"\n","")l=string.gsub(l,"\091 \093\043"," ")local w=ChatWindow:GetTargetMessageChannel()if w then MessageSender:SendMessage(l,w)else MessageSender:SendMessage(l,nil)end end end end end local l={}function setupChatBarConnections()for w=1,#l,1 do l[w]:Disconnect()end l={}local w=(ChatBar:GetTextBox()).FocusLost:connect(chatBarFocusLost)table.insert(l,w)local U=(ChatBar:GetTextBox()).Focused:connect(chatBarFocused)table.insert(l,U)end setupChatBarConnections()ChatBar.GuiObjectsChanged:connect(setupChatBarConnections)function getEchoMessagesInGeneral()if ChatSettings.EchoMessagesInGeneralChannel==nil then return true end return ChatSettings.EchoMessagesInGeneralChannel end EventFolder.OnMessageDoneFiltering.OnClientEvent:connect(function(l)if not ChatSettings.ShowUserOwnFilteredMessage then if l.FromSpeaker==LocalPlayer.Name then return end end local w=l.OriginalChannel local U=ChatWindow:GetChannel(w)if U then U:UpdateMessageFiltered(l)end if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName)then local w=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if w then w:UpdateMessageFiltered(l)end end end)EventFolder.OnNewMessage.OnClientEvent:connect(function(l,w)local U=ChatWindow:GetChannel(w)if U then U:AddMessageToChannel(l)if l.FromSpeaker~=LocalPlayer.Name then ChannelsBar:UpdateMessagePostedInChannel(w)end if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName)then local w=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if w then w:AddMessageToChannel(l)end end moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()end end)EventFolder.OnNewSystemMessage.OnClientEvent:connect(function(l,w)w=w or"\083\121\115\116\101\109"local U=ChatWindow:GetChannel(w)if U then U:AddMessageToChannel(l)ChannelsBar:UpdateMessagePostedInChannel(w)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)DoFadeInFromNewInformation()if getEchoMessagesInGeneral()and(ChatSettings.GeneralChannelName and w~=ChatSettings.GeneralChannelName)then local w=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if w then w:AddMessageToChannel(l)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",w))end end)function HandleChannelJoined(l,w,U,Z,A,N)if ChatWindow:GetChannel(l)then ChatWindow:RemoveChannel(l)end if l==ChatSettings.GeneralChannelName then DidFirstChannelsLoads=true end if Z then ChatBar:SetChannelNameColor(l,Z)end local o=ChatWindow:AddChannel(l)if o then if l==ChatSettings.GeneralChannelName then DoSwitchCurrentChannel(l)end if U then local w=1 if#U>ChatSettings.MessageHistoryLengthPerChannel then w=#U-ChatSettings.MessageHistoryLengthPerChannel end for l=w,#U,1 do o:AddMessageToChannel(U[l])end if getEchoMessagesInGeneral()and A then if ChatSettings.GeneralChannelName and l~=ChatSettings.GeneralChannelName then local l=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if l then l:AddMessagesToChannelByTimeStamp(U,w)end end end end if w~=""then local U={ID=-1;FromSpeaker=nil;SpeakerUserId=0,OriginalChannel=l,IsFiltered=true;MessageLength=string.len(w);MessageLengthUtf8=utf8.len(utf8.nfcnormalize(w));Message=trimTrailingSpaces(w),MessageType=ChatConstants.MessageTypeWelcome,Time=os.time(),ExtraData=nil}o:AddMessageToChannel(U)if getEchoMessagesInGeneral()and(N and not ChatSettings.ShowChannelsBar)then if l~=ChatSettings.GeneralChannelName then local l=ChatWindow:GetChannel(ChatSettings.GeneralChannelName)if l then l:AddMessageToChannel(U)end end end end DoFadeInFromNewInformation()end end EventFolder.OnChannelJoined.OnClientEvent:connect(function(l,w,U,Z)HandleChannelJoined(l,w,U,Z,false,true)end)EventFolder.OnChannelLeft.OnClientEvent:connect(function(l)ChatWindow:RemoveChannel(l)DoFadeInFromNewInformation()end)EventFolder.OnMuted.OnClientEvent:connect(function(l) end)EventFolder.OnUnmuted.OnClientEvent:connect(function(l) end)EventFolder.OnMainChannelSet.OnClientEvent:connect(function(l)DoSwitchCurrentChannel(l)end);(coroutine.wrap(function()local l=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 l then l.OnClientEvent:connect(function(l,w)ChatBar:SetChannelNameColor(l,w)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

		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)

function getEchoMessagesInGeneral()
	if ChatSettings.EchoMessagesInGeneralChannel == nil then
		return true
	end
	return ChatSettings.EchoMessagesInGeneralChannel
end

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
		getEchoMessagesInGeneral()
		and 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

		if
			getEchoMessagesInGeneral()
			and ChatSettings.GeneralChannelName
			and channelName ~= ChatSettings.GeneralChannelName
		then
			local 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
			getEchoMessagesInGeneral()
			and 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 == ChatSettings.GeneralChannelName 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 getEchoMessagesInGeneral() and 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),
				MessageLengthUtf8 = utf8.len(utf8.nfcnormalize(welcomeMessage)),
				Message = trimTrailingSpaces(welcomeMessage),
				MessageType = ChatConstants.MessageTypeWelcome,
				Time = os.time(),
				ExtraData = nil,
			}
			channelObj:AddMessageToChannel(welcomeMessageObject)

			if
				getEchoMessagesInGeneral()
				and 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)()
