Stop

Here's the full documentation in English, incorporating your requested changes and additions:


Using the stop Function to Control Command Flow

In 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.

const { newCommand, stop, getMessageContent, checkContains } = require('mvk-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.

newCommand({
  name: 'test-command',
  code: async function (message) {
    let msg = getMessageContent(message); // Fetch the message content

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.

if (!msg) {
  message.reply('Please provide a number between 1 and 10.');
  return stop(); // Halt execution if no message is provided
}
  • Condition: If msg is empty or null, we reply with a message and call stop() to halt execution, preventing further steps from running.

4. Processing the Message Content

If a message is provided, we proceed to process it further.

msg = await getMessageContent(message, 0); // Fetch the message content
const msgNumber = parseInt(msg, 10); // Convert the message to a number

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 (msgNumber >= 0 && msgNumber <= 10) {
  const results = await checkContains({
    text: msg, // Pass the msg as checkContains only accepts strings
    keywords: ['1', '2', '3', '4', '6', '7', '8', '9'] // Avoid 5 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) {
    message.reply('The code continues beyond this point.');
  } else {
    message.reply('The code stops here.');
    return stop(); // Stop execution if no match is found
  }
} else {
  message.reply('The number must be between 1 and 10.');
  return stop(); // Stop execution if the number is out of range
}
  • 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

console.log('This message will only appear if the code was not stopped.');

If stop was not called, this message will be logged to the console, ensuring the code successfully passed all checks.

Final Code Example:

const { newCommand, stop, getMessageContent, checkContains } = require('mvk-project');

newCommand({
  name: 'test-command',
  code: async function (message) {
    let msg = getMessageContent(message); // Fetch the message content
    if (!msg) {
      message.reply('Please provide a number between 1 and 10.');
      return stop(); // Halt execution if no message is provided
    } else {
      msg = await getMessageContent(message, 0); // Fetch the message content
      const msgNumber = parseInt(msg, 10); // Convert the message to a number
      
      if (msgNumber >= 0 && msgNumber <= 10) { // Compare as a number
        const results = await checkContains({
          text: msg, // Pass the msg as checkContains only accepts strings
          keywords: ['1', '2', '3', '4', '6', '7', '8', '9'] // Avoid 5 and 10
        });
        
        if (results) {
          message.reply('The code continues beyond this point.');
        } else {
          message.reply('The code stops here.');
          return stop(); // Stop execution if no match is found
        }
      } else {
        message.reply('The number must be between 1 and 10.');
        return stop(); // Stop execution if the number is out of range
      }
    }
    
    console.log('This message will only appear if the code was not stopped.');
  }
});


Why Use the 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