OnChatMessage

From GT-MP
Jump to: navigation, search
OnChatMessage
Shared Function
Utilities-terminal.png Server-Side System-users.png Client-Side
Csharp.png C# Javascript.png JavaScript

This type of event can be used to filter, disable or even use as command handler but that is a very legacy way of doing commands and is not recommended.

Requirements

First of all, it should be defined in your Main constructor for it to be called, otherwise it wouldn't be initiated at all!

   public Main()
   {
      API.onChatMessage += OnChatMessageHandler;
   }

Usage example(s)

  • Disabling main chat
   public void OnChatMessageHandler(Client player, string message, CancelEventArgs e)
   {
      e.Cancel = true; //This cancels the chat event
   }
  • An example for filtering a word in chat
   public void OnChatMessageHandler(Client player, string message, CancelEventArgs e)
   {
      if (message.Contains("Foo"))
      {
         API.sendChatMessageToPlayer(player, "Prohibited word!");
         e.Cancel = true; //This cancels the message event
      }
   }
  • An example for replacing a word in chat
   public void OnChatMessageHandler(Client player, string message, CancelEventArgs e)
   {
      if (message.Contains("Foo"))
      {
         API.sendChatMessageToAll(player.Name, message.Replace("Foo", "!#$"));
         e.Cancel = true; //This cancels the message event
      }
   }

References