Events

Event Handler Setup

  1. Import Required Functions

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

    const { startBot, loadEvents } = 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: '!', // Prefix for your bot commands
      consoleLog: 'Bot ${bot.username} is online' // Optional message when the bot starts
    });
  3. Load Events

    To load and manage events, use the loadEvents function. This function will load all your event handlers from a specified directory. Make sure to pass the client instance to the function:

    loadEvents('./events', client); // Loads events from the specified folder

    This step ensures that all your events are registered and ready to be used. The loadEvents function searches for event files in the provided directory (./events 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 events, and start operating with the settings you’ve configured.


Example Code:

const { alwaysReply, checkContains } = require('mvk-project');

module.exports = alwaysReply({
    action: async (message) => {
        const authorId = await message.author.id;
        const result = checkContains({
            text: message.content,
            keywords: ['hi', 'hello']
        });
        if (result) {
            message.reply(`Hi <@${authorId}>!`);
        }
    }
});

Note: The functions seen above will be explained in more detail later.

Last updated