Discord.js 中有多个文件 运行?

Multiple Files running in Discord.js?

如果这看起来很愚蠢或有任何问题,请原谅我,我已经尝试寻找地方,但我不知道这是不是放置它的正确位置或任何东西

所以我正在尝试制作一个不和谐的机器人,我有一个脚本和所有东西,但问题是,我只有一个脚本。我不知道如何使用其他脚本,但我想我不是唯一遇到这个问题的人,因为如果有人使用两种语言,那么它需要两个文件,但我不知道如何制作第二个文件 运行.

别担心,对于 post 这个问题,这是一个很好的选择。基本上你需要做的是这样的:

在您要存储管理命令的单独文件中(我将此文件命名为 adminCmds.js),设置一个 module.exports 变量,该变量指向包含您的管理命令的对象。在我的示例中,我的 adminCmds.js 文件与 index.js.

位于同一目录中

尝试这样的事情:

// inside adminCmds.js
function admin1() {
    console.log('in admin 1 command');
    // your command code here
}

function admin2() {
    console.log('in admin 2 command');
    // your command code here
}

module.exports = {
    checkAdminCmd: function(message) {
        let command = message.content, found = false;

        switch(command) {
            // your first admin command (can be whatever you want)
            case '?admin1':
                // set found equal to true so your index.js file knows
                //   to not try executing 'other' commands
                found = true;
                // execute function associated with this command
                admin1();
                break;

            // your second admin command (similar setup as above)
            case '?admin2':
                found = true;
                admin2();
                break;

            // ... more admin commands
        }

        // value of 'found' will be returned in index.js
        return found;
    }
};

在您的 index.js 主文件中,像这样设置您的主消息侦听器:

// get admin commands from other file
const adminCmds = require('./adminCmds');

// set message listener 
client.on('message', message => {
    let command = message.content;

    // execute admin commands
    // -> if function checkAdminCmd returns false, move on to checking 'other' commands
    if ( adminCmds.checkAdminCmd(message) )
        return;

    // execute other commands
    else {
        switch(command) {
            case '?PING':
                message.reply('pong');
                break;

            // ... other commands here
        }
    }
});

我强烈建议您在使用 Discord.js 之前先阅读一些 Node.js 教程 - 这会大有帮助。但是,如果您在此期间 运行 遇到任何麻烦,我很乐意提供帮助。