Can you declare your own function in Discord JS?

Tags: , , , ,



Normally I just add to the command files or the index file easily but it’s starting to look messy. Recently I got this leveling system working

if (!levels[message.author.id]) {
        levels[message.author.id] = {
            level: 1,
            exp: 0
        }   
    }

    // Gives random EXP
    let randomExp = Math.floor(Math.random() * 5 + 5);

    // Adds the random EXP to their current EXP
    levels[message.author.id].exp += randomExp;

    // Checks their EXP and changes their level
    for (x = 0; x < expLevels.length; x++) {
        if (levels[message.author.id].exp > expLevels[x]) {
            levels[message.author.id].level = x + 1;
            message.channel.reply(`congratulations! You reached level ${levels[message.author.id].level + 1}!`);
        }
    }
    
    fs.writeFile('./levels.json', JSON.stringify(levels), err => {
        if (err) console.error(err);
    });
    
    if (levels[authorMessage.author.id].level >= 10) {
        message.member.roles.remove('720109209479413761');
        message.member.roles.add('719058151940292659');
    }

I would like to be able to put this into its own function and then call it in the “message” section for every time someone sends a message. Is that possible to do? Or no since I need to have access to the “message” variable?

I’m used to C++ with functions where it’s much easier to deal with. Does anyone know if it’s possible to code a bot in C++ or is there no support? If there is a way if someone can point me in the right direction to start please let me know. Otherwise I can easily stay with JS.

Answer

I’m not sure if a discord framework for C++ exists, probably but I’m not sure.

You can of course define a function somewhere and call it in the onMessage event.

There are two ways that you could do that.

  • In the same file
  • In another file

Functions in the same file.

You can declare a function and then pass arguments to that function. You don’t need to declare the type of argument that is being passed here. Source

function leveling(message) { // here you can include all parameters that you might need
// the rest of your code
}

Once you have a function you can call it like this.

leveling(message); // here we pass the values we need to the function

Functions in a different file.

The concept is the same, however we need to export the function in order to use it somewhere else. There are two ways to do this, either export only one function or export all functions, in the case of a dedicated functions file this is the easier option.

Note: In this example I name the file functions.js and place it in the same directory as the file I require it from.

module.exports = {
    // we need to declare the name first, then add the function
    leveling: function (message) { 
        // the rest of your code
    }
    // here we can add more functions, divided by a comma
}

// if you want to export only one function
// declare it normally and then export it
module.exports = leveling;

Calling functions.

To use this function we need to require it in the file we want to use it in. Here we also have two options.

Either require the whole file and get the function from there

const myfunctions = require('./functions.js'); // this is the relative path to the file
// get the function via the new constant
myfunctions.leveling(message);

Or use Object destructuring to get only what you need from the exported functions.

const { leveling } = require('./functions.js');

leveling(message);

Both of these options provide advantages and disadvantages but in the end they both do the same.



Source: stackoverflow