local A=game:GetService("\084\101\120\116\067\104\097\116\083\101\114\118\105\099\101")local S=game:GetService("\080\108\097\121\101\114\115")return{_NxWrapper_PostInit=function(n)n.ChatMute=function(A) end n.ChatUnmute=function(A) end task.spawn(function()local A=game.ReplicatedStorage:WaitForChild("\083\111\099\105\097\108\067\104\097\116",60)if not A then return end local S=require(A)local X=S.Src.Channels X.OnMessageSent:Connect(function(A,S,X)table.insert(n.Data.Logs.Chat,{os.time();A.Agent.UserId;S})n:RunCommand(A.Agent,S)end)end)if type(n.Data.Settings.TextChatService)=="\116\097\098\108\101"and n.Data.Settings.TextChatService.Enabled then task.spawn(function()if A.ChatVersion==Enum.ChatVersion.TextChatService then local X={}A:WaitForChild("\084\101\120\116\067\104\097\116\067\111\109\109\097\110\100\115")n.Signals.CommandRegistered:Connect(function(c)local v=Instance.new("\084\101\120\116\067\104\097\116\067\111\109\109\097\110\100")v.Parent=A:FindFirstChild("\084\101\120\116\067\104\097\116\067\111\109\109\097\110\100\115")v.PrimaryAlias="\047"..c.Name if c.Aliases and c.Aliases[1]then v.SecondaryAlias="\047"..c.Aliases[1]end v.Triggered:Connect(function(A,X)local v=X:split(" ")table.remove(v,1)local y=table.concat(v," ")local w=S:GetPlayerByUserId(A.UserId)n:RunCommand(w,n.Data.Settings.Prefix..c.Name,true)end)X[c.Name]=true end)for c,v in n.Commands do if not X[v.Name]then local c=Instance.new("\084\101\120\116\067\104\097\116\067\111\109\109\097\110\100")c.Parent=A:FindFirstChild("\084\101\120\116\067\104\097\116\067\111\109\109\097\110\100\115")c.PrimaryAlias="\047"..v.Name if v.Aliases and v.Aliases[1]then c.SecondaryAlias="\047"..v.Aliases[1]end c.Triggered:Connect(function(A,X)local c=X:split(" ")table.remove(c,1)local y=table.concat(c," ")local w=S:GetPlayerByUserId(A.UserId)local h=n:RunCommand(w,n.Data.Settings.Prefix..(v.Name..(" "..y)),true)if h then end end)X[v.Name]=true end end end end)end return nil end}
local Chat = game:GetService("TextChatService");
local Players = game:GetService("Players");

return {_NxWrapper_PostInit = function(env)
	-- SocialChat v2
	
	env.ChatMute = function(player)
		
	end;
	
	env.ChatUnmute = function(player)
		
	end;
	
	task.spawn(function() -- SocialChat
		local SocialChatExists = game.ReplicatedStorage:WaitForChild("SocialChat",60); -- Give it a full minute to find it.
		if not SocialChatExists then return end;
		local SocialChat = require(SocialChatExists);
		local Channels = SocialChat.Src.Channels

		Channels.OnMessageSent:Connect(function(Speaker, Message, Recipient)
			table.insert(env.Data.Logs.Chat,{os.time(),Speaker.Agent.UserId,Message});
			env:RunCommand(Speaker.Agent,Message);
		end);
	end)
	
	if type(env.Data.Settings['TextChatService'])=='table' and env.Data.Settings['TextChatService'].Enabled then
		task.spawn(function() -- Are we using TextChatService?
			if Chat.ChatVersion == Enum.ChatVersion.TextChatService then -- cool we can proceed
				local CommandsRegistered = {}
				Chat:WaitForChild("TextChatCommands")
				env.Signals.CommandRegistered:Connect(function(CommandData)
					local Command = Instance.new("TextChatCommand")
					Command.Parent = Chat:FindFirstChild("TextChatCommands")
					Command.PrimaryAlias = "/"..CommandData.Name;
					
					if CommandData.Aliases and CommandData.Aliases[1] then
						Command.SecondaryAlias = "/"..CommandData.Aliases[1]
					end

					Command.Triggered:Connect(function(TextSource, Message)
						-- Removing the first part of the message
						local Message = Message:split(" ");
						table.remove(Message, 1);
						local Message = table.concat(Message, " ");
						
						local Player = Players:GetPlayerByUserId(TextSource.UserId)
						
						env:RunCommand(Player, env.Data.Settings.Prefix..CommandData.Name, true);
					end)
					
					CommandsRegistered[CommandData.Name] = true;
				end)
				
				-- Do the same for all the commands that already exist :pepesmile:
				for _,CommandData in env.Commands do
					if not CommandsRegistered[CommandData.Name] then
						local Command = Instance.new("TextChatCommand")
						Command.Parent = Chat:FindFirstChild("TextChatCommands")
						Command.PrimaryAlias = "/"..CommandData.Name;

						if CommandData.Aliases and CommandData.Aliases[1] then
							Command.SecondaryAlias = "/"..CommandData.Aliases[1]
						end

						Command.Triggered:Connect(function(TextSource, Message)
							-- Removing the first part of the message
							local Message = Message:split(" ");
							table.remove(Message, 1);
							local Message = table.concat(Message, " ");

							local Player = Players:GetPlayerByUserId(TextSource.UserId)

							local r = env:RunCommand(Player, env.Data.Settings.Prefix..CommandData.Name..' '..Message, true);
							if r then
								--TODO: Return chat messages if 'Auto' or Chat.
							end
						end)
						
						CommandsRegistered[CommandData.Name] = true;
					end
				end
			end
		end)
	end;

	return nil;
end};