can you declare your own function in discord js?

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

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

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


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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top