简体   繁体   English

你可以在 Discord JS 中声明你自己的函数吗?

[英]Can you declare your own function in Discord JS?

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.我习惯于使用 C++ 来处理更容易处理的函数。 Does anyone know if it's possible to code a bot in C++ or is there no support?有谁知道是否可以用 C++ 编写机器人代码,或者不支持? 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.否则我可以轻松地继续使用 JS。

I'm not sure if a discord framework for C++ exists, probably but I'm not sure.我不确定 C++ 的不和谐框架是否存在,可能但我不确定。

You can of course define a function somewhere and call it in the onMessage event.您当然可以在某处定义一个函数并在onMessage事件中调用它。

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.注意:在本例中,我将文件命名为functions.js并将其放在我需要它的文件所在的目录中。

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.要使用这个函数,我们需要在我们想要使用它的文件中require它。这里我们还有两个选项。

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.这两种选择都有优点和缺点,但最终它们都做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM