Prefix Commands

Setting Up Prefix Commands

1. Import Required Functions

In your index.js file (or wherever you initialize your bot), start by importing the necessary functions from the mvk-project package:

const { startBot, loadCommands } = require('mvk-project');

2. Create and Configure Your Bot Client

Use the startBot function to create and configure your bot client. This function sets up the bot with your token, intents, prefix, and an optional console log message:

const client = startBot({
  token: '...', // Replace with your bot's token
  intents: [
    'Guilds',
    'GuildMessages',
    'MessageContent'
    // Add more intents if needed
  ],
  prefix: '!', // Command prefix for your bot
  consoleLog: 'MVK Project is online' // Optional log message when the bot starts
});

3. Load Prefix Commands

To load and manage prefix commands, use the loadCommands function. This function will load all your prefix-based commands from a specified directory. Make sure to pass the client instance to the function:

loadCommands('./prefix_commands', client); // Loads prefix commands from the specified folder

This step ensures that all your prefix commands are registered and ready to be used. The loadCommands function looks for command files in the provided directory (./prefix_commands in this case) and integrates them into your bot.

4. Start Your Bot

Finally, start your bot by running your index.js file using Node.js:

node index.js

Your bot will initialize, load the prefix commands, and begin operating with the configurations you’ve set.

Test with a Basic Ping Command

To test if everything is working correctly, let's start with a basic ping command. The functions shown below are just for demonstration purposes; you will learn more about them later on.

const { newCommand, sendMessage, getClient } = require('../index');
const client = getClient();

newCommand({
  name: 'ping',
  code: async function (message) {
    const ping = client.ws.ping; 

    await sendMessage({
      channel: message.channel,
      message: `The bot's ping is: ${ping}`
    });
  }
});

In this example:

  • Replace client.ws.ping with the actual function or property you use to get the bot's ping.

  • The sendMessage function is used to send a message to the channel with the ping information.

You can now test this command to see if your bot responds with the ping value.

Last updated