Custom Command
FunctionsMessage Functions

$message

The $message function retrieves the user's message or command arguments, providing a powerful way to interact with user input. It's particularly useful for commands where you need to process the text a user has entered.

When used in a Forward Message trigger, $message instead returns the content of the original forwarded message, allowing you to inspect or respond to what was forwarded rather than the forwarding action itself.

Key Use Cases:

  • Direct Message Content: Get the entire message a user sends after a command prefix (e.g., after !cmd).
  • Command Arguments: Access individual words or phrases provided as arguments to a command.
  • Forwarded Messages: Retrieve the content of the original forwarded message when using a Forward Message trigger.
  • Slash Command Data: When used within a slash command, $message retrieves either the value of a specific option or all the option values entered by the user.

Usage

$message
$message[index]
$message[startIndex+]
  • $message: Returns the entire message following the command prefix. When used in a Forward Message trigger, it returns the content of the forwarded message instead.
  • $message[index]: Returns the argument at the specified index (starting from 1).
  • $message[startIndex+]: Returns all arguments starting from the specified startIndex (including the argument at that index).

Examples

Let's say a user types the following command:

!cmd Hello World, How are you?

Here's how $message would behave:

  • $message would be replaced with: Hello World, How are you?
  • $message[1] would be replaced with: Hello
  • $message[2] would be replaced with: World
  • $message[2+] would be replaced with: World, How are you?

Explanation:

  • $message captures the entire input string after the !cmd command.
  • $message[1] gets the first word ("Hello"). Remember that indexing starts at 1, not 0.
  • $message[2] gets the second word ("World").
  • $message[2+] gets all words starting from the second word ("World"), resulting in "World, How are you?".

Forward Message Example

If a user forwards a message containing:

Server maintenance starts in 10 minutes.

and your custom command is triggered by the Forward Message trigger:

  • $messageServer maintenance starts in 10 minutes.
  • $message[1]Server
  • $message[2+]maintenance starts in 10 minutes.

This allows you to process the contents of the original forwarded message just like a normal user message.

Slash Command Example

Imagine you have a slash command:

/greet user:John Doe message:Hello!

If your code uses $message[1], and the slash command defines the options in the order user then message, it may return "John Doe". For slash commands, it is generally more reliable to use $getOption to retrieve values by option name.

Function Difficulty: Easy

Tags: Message Arguments Forward Message Argument Handling

On this page