【问题标题】:Multiple Files running in Discord.js? [closed]Discord.js 中运行多个文件? [关闭]
【发布时间】:2018-07-13 10:42:29
【问题描述】:

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

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

【问题讨论】:

  • 你能举一个具体的例子说明问题是什么以及你想要达到的目标吗?
  • 在 Node.js 中,您只需要使用某种类型的 require('<package>') 语句 - 这就是您所说的吗?你对 Node.js 了解多少?
  • 大家好!我对node.js了解不多,
  • 我刚刚开始使用它举个例子,如果我有一个脚本“索引”,其中包含 ping 和东西等所有命令来处理这些东西,但是我想要另一个脚本处理所有管理命令和东西,但是当我尝试这样做时,只有索引脚本有效,而不是 AdminCommands

标签: node.js discord.js


【解决方案1】:

别担心,这是发布此问题的最佳地点。基本上你需要做的是这样的:

在您要存储管理命令的单独文件中(我将此文件命名为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 教程 - 这会很有帮助。但如果您在此期间遇到任何麻烦,我很乐意提供帮助。

【讨论】:

  • 你有什么教程推荐吗? :D谢谢,这工作:D
  • @Jeffeeze - 是的!我强烈推荐 The Net Ninja 的这个:Node Js for Beginners
  • 我会调查的,非常感谢 :D
猜你喜欢
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2018-09-05
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多