local D={}D.__index=D function D.AddChannel(n,D)if n.ChatChannels[D:lower()]then error(string.format("\067\104\097\110\110\101\108 \037\113 \097\108\114\097\100\121 \101\120\105\115\116\115\046",D))end local function X(X,b)if b:lower()=="\047\108\101\097\118\101"then local b=n:GetChannel(D)local J=n:GetSpeaker(X)if b and J then if b.Leavable then J:LeaveChannel(D)J:SendSystemMessage(string.format("\089\111\117 \104\097\118\101 \108\101\102\116 \099\104\097\110\110\101\108 \'\037\115\'",D),"\083\121\115\116\101\109")else J:SendSystemMessage("\089\111\117 \099\097\110\110\111\116 \108\101\097\118\101 \116\104\105\115 \099\104\097\110\110\101\108\046",D)end end return true end return false end local b=ChatChannel.new(n,D)n.ChatChannels[D:lower()]=b b:RegisterProcessCommandsFunction("\100\101\102\097\117\108\116\095\099\111\109\109\097\110\100\115",X,ChatConstants.HighPriority)local J,r=pcall(function()n.eChannelAdded:Fire(D)end)if not J and r then print("\069\114\114\111\114 \097\100\100\100\105\110\103 \099\104\097\110\110\101\108\058 "..r)end return b end function D.RemoveChannel(n,D)if n.ChatChannels[D:lower()]then local X=n.ChatChannels[D:lower()].Name n.ChatChannels[D:lower()]:InternalDestroy()n.ChatChannels[D:lower()]=nil local b,J=pcall(function()n.eChannelRemoved:Fire(X)end)if not b and J then print("\069\114\114\111\114 \114\101\109\111\118\105\110\103 \099\104\097\110\110\101\108\058 "..J)end else warn(string.format("\067\104\097\110\110\101\108 \037\113 \100\111\101\115 \110\111\116 \101\120\105\115\116\046",D))end end function D.GetChannel(n,D)return n.ChatChannels[D:lower()]end function D.AddSpeaker(n,D)if n.Speakers[D:lower()]then error("\083\112\101\097\107\101\114 \""..(D.."\" \097\108\114\101\097\100\121 \101\120\105\115\116\115\033"))end local X=Speaker.new(n,D)n.Speakers[D:lower()]=X local b,J=pcall(function()n.eSpeakerAdded:Fire(D)end)if not b and J then print("\069\114\114\111\114 \097\100\100\105\110\103 \115\112\101\097\107\101\114\058 "..J)end return X end function D.InternalUnmuteSpeaker(n,D)for n,X in pairs(n.ChatChannels)do if X:IsSpeakerMuted(D)then X:UnmuteSpeaker(D)end end end function D.RemoveSpeaker(n,D)if n.Speakers[D:lower()]then local X=n.Speakers[D:lower()].Name n:InternalUnmuteSpeaker(X)n.Speakers[D:lower()]:InternalDestroy()n.Speakers[D:lower()]=nil local b,J=pcall(function()n.eSpeakerRemoved:Fire(X)end)if not b and J then print("\069\114\114\111\114 \114\101\109\111\118\105\110\103 \115\112\101\097\107\101\114\058 "..J)end else warn("\083\112\101\097\107\101\114 \""..(D.."\" \100\111\101\115 \110\111\116 \101\120\105\115\116\033"))end end function D.GetSpeaker(n,D)return n.Speakers[D:lower()]end function D.GetChannelList(D)local n={}for D,X in pairs(D.ChatChannels)do if not X.Private then table.insert(n,X.Name)end end return n end function D.GetAutoJoinChannelList(D)local n={}for D,X in pairs(D.ChatChannels)do if X.AutoJoin then table.insert(n,X)end end return n end function D.GetSpeakerList(D)local n={}for D,X in pairs(D.Speakers)do table.insert(n,X.Name)end return n end function D.SendGlobalSystemMessage(n,D)for n,X in pairs(n.Speakers)do X:SendSystemMessage(D,nil)end end function D.RegisterFilterMessageFunction(b,D,n,X)b.FilterMessageFunctions:AddFunction(D,n,X)end function D.UnregisterFilterMessageFunction(n,D)n.FilterMessageFunctions:RemoveFunction(D)end function D.RegisterProcessCommandsFunction(b,D,n,X)b.ProcessCommandsFunctions:AddFunction(D,n,X)end function D.UnregisterProcessCommandsFunction(n,D)n.ProcessCommandsFunctions:RemoveFunction(D)end local n=0 local X=0 local b=0 function D.InternalNotifyFilterIssue(D)if tick()-X>FILTER_THRESHOLD_TIME then b=0 end b=b+1 X=tick()if b>=FILTER_NOTIFCATION_THRESHOLD then if tick()-n>FILTER_NOTIFCATION_INTERVAL then n=tick()local X=D:GetChannel("\083\121\115\116\101\109")if X then X:SendSystemMessage("\084\104\101 \099\104\097\116 \102\105\108\116\101\114 \105\115 \099\117\114\114\101\110\116\108\121 \101\120\112\101\114\105\101\110\099\105\110\103 \105\115\115\117\101\115 \097\110\100 \109\101\115\115\097\103\101\115 \109\097\121 \098\101 \115\108\111\119 \116\111 \097\112\112\101\097\114\046",errorExtraData)end end end end local J={}
--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

function methods:AddChannel(channelName)
	if self.ChatChannels[channelName:lower()] then
		error(string.format("Channel %q alrady exists.", channelName))
	end

	local function DefaultChannelCommands(fromSpeaker, message)
		if message:lower() == "/leave" then
			local channel = self:GetChannel(channelName)
			local speaker = self:GetSpeaker(fromSpeaker)
			if channel and speaker then
				if channel.Leavable then
					speaker:LeaveChannel(channelName)
					speaker:SendSystemMessage(string.format("You have left channel '%s'", channelName), "System")
				else
					speaker:SendSystemMessage("You cannot leave this channel.", channelName)
				end
			end

			return true
		end
		return false
	end

	local channel = ChatChannel.new(self, channelName)
	self.ChatChannels[channelName:lower()] = channel

	channel:RegisterProcessCommandsFunction("default_commands", DefaultChannelCommands, ChatConstants.HighPriority)

	local success, err = pcall(function()
		self.eChannelAdded:Fire(channelName)
	end)
	if not success and err then
		print("Error addding channel: " .. err)
	end

	return channel
end

function methods:RemoveChannel(channelName)
	if self.ChatChannels[channelName:lower()] then
		local n = self.ChatChannels[channelName:lower()].Name

		self.ChatChannels[channelName:lower()]:InternalDestroy()
		self.ChatChannels[channelName:lower()] = nil

		local success, err = pcall(function()
			self.eChannelRemoved:Fire(n)
		end)
		if not success and err then
			print("Error removing channel: " .. err)
		end
	else
		warn(string.format("Channel %q does not exist.", channelName))
	end
end

function methods:GetChannel(channelName)
	return self.ChatChannels[channelName:lower()]
end

function methods:AddSpeaker(speakerName)
	if self.Speakers[speakerName:lower()] then
		error('Speaker "' .. speakerName .. '" already exists!')
	end

	local speaker = Speaker.new(self, speakerName)
	self.Speakers[speakerName:lower()] = speaker

	local success, err = pcall(function()
		self.eSpeakerAdded:Fire(speakerName)
	end)
	if not success and err then
		print("Error adding speaker: " .. err)
	end

	return speaker
end

function methods:InternalUnmuteSpeaker(speakerName)
	for channelName, channel in pairs(self.ChatChannels) do
		if channel:IsSpeakerMuted(speakerName) then
			channel:UnmuteSpeaker(speakerName)
		end
	end
end

function methods:RemoveSpeaker(speakerName)
	if self.Speakers[speakerName:lower()] then
		local n = self.Speakers[speakerName:lower()].Name
		self:InternalUnmuteSpeaker(n)

		self.Speakers[speakerName:lower()]:InternalDestroy()
		self.Speakers[speakerName:lower()] = nil

		local success, err = pcall(function()
			self.eSpeakerRemoved:Fire(n)
		end)
		if not success and err then
			print("Error removing speaker: " .. err)
		end
	else
		warn('Speaker "' .. speakerName .. '" does not exist!')
	end
end

function methods:GetSpeaker(speakerName)
	return self.Speakers[speakerName:lower()]
end

function methods:GetChannelList()
	local list = {}
	for i, channel in pairs(self.ChatChannels) do
		if not channel.Private then
			table.insert(list, channel.Name)
		end
	end
	return list
end

function methods:GetAutoJoinChannelList()
	local list = {}
	for i, channel in pairs(self.ChatChannels) do
		if channel.AutoJoin then
			table.insert(list, channel)
		end
	end
	return list
end

function methods:GetSpeakerList()
	local list = {}
	for i, speaker in pairs(self.Speakers) do
		table.insert(list, speaker.Name)
	end
	return list
end

function methods:SendGlobalSystemMessage(message)
	for i, speaker in pairs(self.Speakers) do
		speaker:SendSystemMessage(message, nil)
	end
end

function methods:RegisterFilterMessageFunction(funcId, func, priority)
	self.FilterMessageFunctions:AddFunction(funcId, func, priority)
end

function methods:UnregisterFilterMessageFunction(funcId)
	self.FilterMessageFunctions:RemoveFunction(funcId)
end

function methods:RegisterProcessCommandsFunction(funcId, func, priority)
	self.ProcessCommandsFunctions:AddFunction(funcId, func, priority)
end

function methods:UnregisterProcessCommandsFunction(funcId)
	self.ProcessCommandsFunctions:RemoveFunction(funcId)
end

local LastFilterNoficationTime = 0
local LastFilterIssueTime = 0
local FilterIssueCount = 0
function methods:InternalNotifyFilterIssue()
	if (tick() - LastFilterIssueTime) > FILTER_THRESHOLD_TIME then
		FilterIssueCount = 0
	end
	FilterIssueCount = FilterIssueCount + 1
	LastFilterIssueTime = tick()
	if FilterIssueCount >= FILTER_NOTIFCATION_THRESHOLD then
		if (tick() - LastFilterNoficationTime) > FILTER_NOTIFCATION_INTERVAL then
			LastFilterNoficationTime = tick()
			local systemChannel = self:GetChannel("System")
			if systemChannel then
				systemChannel:SendSystemMessage(
					"The chat filter is currently experiencing issues and messages may be slow to appear.",
					errorExtraData
				)
			end
		end
	end
end

local StudioMessageFilteredCache = {}
