Intercept message from player and broadcast?Solved

Hello all !

I'm new here, and I'm new with C#, I usually code in JS with React and Node for the web.

So I have a question, I would like to do just a little thing. 

I want to replace the message of player onPlayerChat. 

I have make a code like this : 

private object OnPlayerChat(BasePlayer player, string message, Chat.ChatChannel channel) { 
            if(message.Equals("test")) {
                player.ChatMessage($"<color=#e74c3c>[Joueur]</color><color=#aaa69d> ->{player.displayName}</color> {message}");
                return true;
            }
            return null;
}

But just the player who send the message see the message, and I want what this message are see on a globalchat ? 

And, it the same thing for that : 

 player.SendConsoleCommand("chat.add", 2, 76561############, $"<color=#e74c3c>ReaperBot</color><color=#aaa69d> -></color> [INFOS] Le wipe a démarré le Jeudi 19/11 à 20h et était un BP Wipe.");

Just the player who send the message see the message. How can I do to see this message in a global chat ? 

Thank you,

Good day

You are using BasePlayer.ChatMessage() which only sends the message to that one player. If you wanted to broadcast it to the whole server you could use PrintToChat().

Yeah I have already use PrinToChat, but I want to send in the chat in place of the player. If I use PrintToChat it's like a broadcast.

I would like, if the player send "hello", the message are formated to be "<player> hello"

I have see the method rust.sendChatMessage(), it's work for that ? 

Like this :

rust.SendChatMessage(player, $"<color=#e74c3c>[Joueurs]</color> <color=#1bb8db>{player.displayName}</color> ->", message, player.UserIDString);
Maybe? I've never used that, last time I needed to do something like this I used Server.Broadcast().

Ok I have test with a friend and the message is send only to the sender, so not working

Anybody have an idea to do that ?  just add a prefix when player send message on the message., but I don't know how to do that .

Thank you

This would be the recommendated way, which is universal:

private object OnUserChat(IPlayer player, string message)
{
    if (message.Equals("test"))
    {
        server.Broadcast($"[#e74c3c][Joueur][/#][#aaa69d] ->{player.Name}[/#] {message}");
        return true;
    }

    return null;
}

This would be a Rust-specific way:

private object OnPlayerChat(BasePlayer player, string message)
{
    if (message.Equals("test"))
    {
        PrintToChat($"<color=#e74c3c>[Joueur]</color><color=#aaa69d> ->{player.displayName}</color> {message}");
        return true;
    }

    return null;
}
Locked automatically