Jump to content

(!!Help!!) Send Player ChatBox To Admins


MrLoKi

Recommended Posts

First, get all online administrators using getOnlineAdmins then use event handler onPlayerChat

function getOnlineAdmins()
	local temp = {} --Temp table to insert all online admins in
	for index, players in ipairs (getElementsByType("player")) do --Loop over all players to check who's admin
		local account = getPlayerAccount(players) --Get player account
		if (account and not isGuestAccount(account)) then --Checking for any errors and whether the account is an guest account
			local accountName = getAccountName(account) --Get account name using the account
			local isAdmin = isObjectInACLGroup("user."..accountName, aclGetGroup("Admin")) --checking if user.accountName is added in Admin ACL group.
			if isAdmin then -- If he is..
				table.insert(temp, players) --add him in table
			end
		end
	end
	return temp -- return it to get the table
end

function sendChatsToAdmins(message, messageType)
	local onlineAdmins = getOnlineAdmins()
	local playerName = getPlayerName(source) --source is the one who sent a message.
	for index, admins in ipairs (onlineAdmins) do
		outputChatBox(playerName..": "..message, 255, 255, 255) --PlayerName: message
	end
end
addEventHandler("onPlayerChat", root, sendChatsToAdmins)

Haven't tested it but it should work

Edited by WiBox
Link to comment

No, of course not. When you use addEventHandler with onChatMessage event, each time someone use /say /teamsay or /me it will automatically trigger onChatMessage event handler, means you have to use it only once. Just like the code I've send above.

function getOnlineAdmins()
    local temp = {} --Temp table to insert all online admins in
    for index, players in ipairs (getElementsByType("player")) do --Loop over all players to check who's admin
        local account = getPlayerAccount(players) --Get player account
        if (account and not isGuestAccount(account)) then --Checking for any errors and whether the account is an guest account
            local accountName = getAccountName(account) --Get account name using the account
            local isAdmin = isObjectInACLGroup("user."..accountName, aclGetGroup("Admin")) --checking if user.accountName is added in Admin ACL group.
            if isAdmin then -- If he is..
                table.insert(temp, players) --add him in table
            end
        end
    end
    return temp -- return it to get the table
end

function sendChatsToAdmins(message, by)
    local name
    local onlineAdmins = getOnlineAdmins()
    if (by and isElement(by) and getElementType(by) == "player") then
        name = getPlayerName(by).."(player)"
    elseif (by) then
        for index, resources in ipairs (getResources()) do
            if (by == resources) then
                name = getResourceName(by).."(resource)"
            end
        end
    else
        name = "Unknown: "
    end
    for index, admins in ipairs (onlineAdmins()) do
        outputChatBox(name..": "..message, admins, 255, 255, 255) --PlayerName(player): message or ResourceName(resource): message
    end
end
addEventHandler("onChatMessage", root, sendChatsToAdmins)

Should be something like this? It isn't written perfectly but it should do the job

Link to comment

No No , You did not understand what I meant I want a code that looks like this ( Spec Camera Player ) => ( Spec Chats Player )

To show all the chats sent to the player => And Sent To Admins 

It is as if we are playing on that system ?

Edited by MrLoKi
Link to comment

I've no time left to explain to code, hope you got the logic behind it

--Server side!

local chats = {}
local chatRange = 20 --Range to check who's near by the player
local saveTimer, saveInterval = false, 50*1000 --Each 50 seconds, save chat to .txt
local adminSpectating = {}

function getPlayerAccountName(plr)
    return getAccountName(getPlayerAccount(plr))
end

function getOnlineAdmins()
    local temp = {} --Temp table to insert all online admins in
    for index, players in ipairs (getElementsByType("player")) do --Loop over all players to check who's admin
        local account = getPlayerAccount(players) --Get player account
        if (account and not isGuestAccount(account)) then --Checking for any errors and whether the account is an guest account
            local accountName = getAccountName(account) --Get account name using the account
            local isAdmin = isObjectInACLGroup("user."..accountName, aclGetGroup("Admin")) --checking if user.accountName is added in Admin ACL group.
            if isAdmin then -- If he is..
                table.insert(temp, players) --add him in table
            end
        end
    end
    return temp -- return it to get the table
end

function getPlayersInRange (plr, range)
    local x, y, z = getElementPosition(plr)
    local players = {}
    for index, value in ipairs(getElementsByType("player")) do 
        if(plr and value)then --Usually we would check if plr ~= value so we don't add him to the list, although we want to save the 'plr' chat as well!
            local Sx, Sy, Sz = getElementPosition(value)
            local distance = getDistanceBetweenPoints3D(x, y, z, Sx, Sy, Sz)
            if(distance <= range)then
                table.insert(players, value)
            end
        end
    end 
    return players
end

function onPlayerChat(msg, msgType)
    local type = "("..getPlayerName(source)..") "
    if (msgType == 0) then
        type = "/say: "
    elseif (msgType == 1) then
        type = "/me: "
    elseif (msgType == 2) then
        type = "/teamsay: "
    elseif (msgType == 3) then
        type = "private: "
    elseif (msgType == 4) then
        type = "internal: "
    else
        type = "unknown :"
    end
    for index, value in ipairs(getOnlineAdmins()) do
        if (adminSpectating[value] == source) then
            triggerClientEvent(value, "addToMemo", value, type.." "..msg)
        end
    end
    local accountName
    local playersInRange = getPlayersInRange(source, chatRange)
    for index, value in ipairs (playersInRange) do
        accountName = getPlayerAccountName(value)
        if (not chats[accountName]) then --We must make sure that the table chats[accountName] exists!
            chats[accountName] = {}
        end
        table.insert(chats[accountName], type.." "..msg) --Now all messages that the player(source) said are saved for everyone and himself!
    end --In the same sense, if you know want to check what the player said and whatever was said next him, are saved in that table
end --But like that your server RAM will cry xD. So we'll save the chats in a TXT file!
addEventHandler("onPlayerChat", root, onPlayerChat) --Once any player chats

function loadChatsToMemo(admin, player)
    if (not admin or not player) then --If admin or player were not given
        return false --End the function
    end
    local accountName = getPlayerAccountName(player)
    if (not fileExists("chats/"..accountName..".txt")) then
        return triggerClientEvent(admin, "loadChatToMemo", admin, false) --If file doesn't exists, let the admin know!
    end
    local file = fileOpen("chats/"..accountName..".txt")
    local fileSize = fileGetSize(file)
    local fileContent = fileRead(file, fileSize)
    triggerClientEvent(admin, "loadChatsToMemo", admin, fileContent)
end

function saveChats()
    for index, value in ipairs (getElementsByType("player")) do
        local accountName = getPlayerAccountName(value)
        local file
        if (fileExists("chats/"..accountName..".txt")) then
            file = fileOpen("chats/"..accountName..".txt")
        else
            file = fileCreate("chats/"..accountName..".txt")
        end
        if (file) then
            for index2, messages in ipairs (chats[accountName]) do
                fileWrite(file, messages)
            end
            fileClose(file)
            chats[accountName] = {} --re empty the table once you save its data, so it doesn't duplicate.
        end
    end
end
saveTimer = setTimer(saveChats, saveInterval, 0)

function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
    end
    return false
end

function spectatePlr(admin, cmd, playerName)
    if (not isObjectInACLGroup("user."..getPlayerAccountName(admin), aclGetGroup("Admin")) then -- if the one who used the command isn't an Admin
        return false -- End the function
    end
    local player = getPlayerFromPartialName(playerName) --helps to find a player using a part of his name!
    if (not player) then
        return outputChatBox("ERROR: Please re-enter the player's nickname!", admin, 255, 150, 0)
    end
    setCameraTarget(admin, player)
    loadChatsToMemo(admin, player)
    adminSpectating[admin] = player
end
addCommandHandler("spectate", spectatePlr)
  
function stopSpectate(admin)
    if (not isObjectInACLGroup("user."..getPlayerAccountName(admin), aclGetGroup("Admin") and adminSpectating[admin]) then
        return false -- if the one who used the command isn't an Admin, End the function
    end
    setCameraTarget(admin) --Retarget it to the admin
    triggerClientEvent(admin, "closeMemo", admin)
    adminSpectating[admin] = false
end
addCommandHandler("stopspectate", stopSpectate)
--Client Side!

local chatWindow

function loadChatsToMemo(chats)
    if (not chatWindow) then
        local screenX, screenY = guiGetScreenSize()
        chatWindow = guiCreateWindow(screenX-300, 0, 300, 300, "Chats Record", false)
        chatMemo = guiCreateMemo(0, 0, 300, 300, "Loading...", false, chatWindow)
        guiWindowSetSizable(chatWindow, false)
        guiSetAlpha(chatWindow, 1)
    end
    showCursor(true)
    if (not chats) then
        return guiSetText(chatMemo, "This player has no chat record!")
    end
    guiSetText(chatMemo, chats)
end
addEvent("loadChatsToMemo", true)
addEventHandler("loadChatsToMemo", localPlayer, loadChatsToMemo)

function addToMemo(message)
    guiSetText(chatMemo, guiGetText(chatMemo).."\n"..addToMemo)
end
addEvent("addToMemo", true)
addEventHandler("addToMemo", localPlayer, addToMemo)

function closeMemo()
    guiSetVisible(chatWindow, false)
    showCursor(false)
end
addEvent("closeMemo", true)
addEventHandler("closeMemo", localPlayer, closeMemo)

I didn't test it, but that should do the job?

Edited by WiBox
editing description
Link to comment

I understood that you want to send to the admin whatever the player IS saying, and whatever is being said TO the player right? You can't tell if something is chatting to him directly, so I saved whatever is being said next him ? Chat with me private in your own native language and explain precisely what you need~

Edited by WiBox
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...