Split Texts

Using textSplit, splitText, getTextSplitLength, and getMessageContent Example

  1. Importing Functions:

    const { newCommand, textSplit, splitText, getTextSplitLength, getMessageContent } = require('mvk-project');

    First, we import the necessary functions from the mvk-project package:

    • newCommand: Used to define a new command.

    • textSplit: Splits a string of text based on a given separator.

    • splitText: Retrieves a specific part of the split text by index.

    • getTextSplitLength: Returns the total number of parts after splitting the text.

    • getMessageContent: Extracts the content of a message.

  2. Defining a New Command:

    newCommand({
      name: 'test-command',
      code: async function (message) {
      }
    });

    We define a new command named test-command. This command will be executed when invoked, and it will split the message content based on a specific character or string.

  3. Splitting the Text:

    textSplit({
      text: getMessageContent(message), // Extract the content of the message
      separator: '-' 
    });

    The textSplit function is used to split the message content. We extract the message content using getMessageContent(message) and specify the separator as a dash (-). This splits the text wherever a dash appears, breaking it into multiple segments.

  4. Using splitText and getTextSplitLength:

    message.reply(`split 1: ${splitText(1)}, length: ${getTextSplitLength()}`)
    • splitText(1) retrieves the first segment from the split text.

    • getTextSplitLength() returns the total number of segments produced by the textSplit.

    In this example, the bot replies with the first segment of the split message and the total number of parts that the message has been split into.

  5. Final Code:

    const { newCommand, textSplit, splitText, getTextSplitLength, getMessageContent } = require('mvk-project');
    
    newCommand({
      name: 'test-command',
      code: async function (message) {
        textSplit({
          text: getMessageContent(message), // Extract the message content
          separator: '-' // Split wherever a dash ('-') appears
        }); 
    
        message.reply(`split 1(0): ${splitText(0)}, length: ${getTextSplitLength()}`) 
        // Reply with the first segment and the total number of segments
      }
    });

Function Descriptions:

These functions work together to split the content of a message and access specific parts of it for further processing. You can adjust the separator to split by any character or string you need.

Last updated