Stop
Here's the full documentation in English, incorporating your requested changes and additions:
Using the stop
Function to Control Command Flow
stop
Function to Control Command FlowIn this example, we will break down how the stop
function is used to control the flow of a command based on certain conditions, such as whether the user provided a valid message or number input.
1. Importing the Functions
First, import the necessary functions, including stop
, from your project.
Here, we import newCommand
to define a new command, stop
to control the flow, getMessageContent
to fetch message content, and checkContains
to check if a message contains specific keywords.
2. Defining a New Command
Define a new command where we will use the stop
function to halt execution if certain conditions aren't met.
The command named test-command
starts by getting the message content using getMessageContent
.
3. Checking for Message Content
Check if the user has provided a message. If not, we stop further execution.
Condition: If
msg
is empty or null, we reply with a message and callstop()
to halt execution, preventing further steps from running.
4. Processing the Message Content
If a message is provided, we proceed to process it further.
Here, we retrieve the first word of the message, convert it into a number using parseInt
, and store it in msgNumber
.
5. Validating the Number Range
Next, we check if the provided number falls between 0 and 10.
If the number is between 0 and 10, we check if the message contains certain keywords using the checkContains
function.
6. Stopping Execution Based on Results
If the results from checkContains
are positive, we continue. If not, we stop the execution.
If results: The code proceeds if the
checkContains
function detects a keyword.If no results: The
stop
function is called to halt execution if no keyword is detected, or if the number is outside the specified range.
7. Final Console Log
If stop
was not called, this message will be logged to the console, ensuring the code successfully passed all checks.
Final Code Example:
Why Use the stop
Function?
stop
Function?The stop
function is used to prevent unnecessary code execution when certain conditions, like in the example, are not met. Even though there may be more code present, calling stop
ensures that the rest of the code won't be executed once it's triggered. It can be used in various contexts, not just with conditions—it's all about creativity in how you apply it.
This example demonstrates how stop
effectively halts command execution when certain conditions aren't met, allowing you to avoid unnecessary code execution and use it creatively in various contexts.
Last updated