对Bot命令使用其他文件 [英] Use different file for bot commands

查看:63
本文介绍了对Bot命令使用其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过将所有较长的内容放入另一个文件中,以保持代码的外观整洁和易于理解。我有我的主文件(index.js):

I want to keep my code clean looking and easier to understand, by putting all the longer stuff in another file. I have my main file (index.js):

 const discord = require('discord.js');
 require('dotenv').config()

 const token = process.env.botToken;
 const prefix = "s!";

 const cmds = require("./commands.js");

 var client = new discord.Client();

 client.on('ready', function(message) {});

 client.on('message', function(message) {
    if(message.author.equals(client.user) || !message.content.startsWith(prefix)) return;

    var args = message.content.substring(prefix.length).split(" ");

    switch (args[0].toLowerCase()) {
        case "help":
            cmds.help;
            break;
    }
 });

 client.login(token)

和我的其他文件夹(命令。 js):

and my other folder (commands.js):

const discord = require('discord.js');
var client = new discord.Client();

    module.exports = {
        help: function(message) {
            var embed = new discord.RichEmbed()
                .addField("spyBot Commands", "If you get issues, dont be afraid to join us: http://discord.gg/3k6zGNF");
            message.channel.send(embed);
        }
    }

我希望它发送嵌入,但是当我把命令放进去了,什么也没发生,也没有任何错误输出。

I would like it to send the embed, but when I put the command in, nothing happens and no errors are printed.

推荐答案

我看到有两点需要修复:

1 commands.js中的客户端

2 :客户端中的命令功能主文件

I see two major things that need to be fixed:
1: the client in commands.js
2: the command function in the main file

1 -在 commands.js 中,您创建了一个新客户端。如果只有此命令,它将不会导致任何问题,因为在代码中未使用 client ,但是当您需要它时,它将不起作用必须与主文件中的相同。您有两种可能的解决方案:将客户端设置为全局客户端或需要主模块。如果您的漫游器不必发布在公共软件包中,则可以保存 global.client = client; ,然后以 client的身份访问。另一种方法是从主模块( module.exports = {client}; )导出客户端,然后在 commands.js ( var {client} = require( ./ index.js); )。

1 - In commands.js, you created a new client. If you have only this command, it won't cause any problem since client is not used in your code, but when you'll need it that won't work since it has to be the same as in the main file. You have two possible solutions: setting your client as global or requiring the main module. If your bot doesn't have to be published in a public package then you can save global.client = client;, and then access it as client in every other file. The alternative is to export the client from the main module (module.exports = {client};) and then require the main file in commands.js (var {client} = require("./index.js");).

2 -在 commands.js 中,您导出的是帮助 功能,因此当您调用它 index.js 您必须使用括号并将消息作为参数传递。尝试这样的事情:

2 - In commands.js you're exporting a help function, so when you call it index.js you have to use parenthesis and pass the message as an argument. Try something like this:

//in the switch statement
case "help":
  cmds.help(message);
  break;

我希望这可以为您提供帮助,如果您还有其他疑问,请告诉我。

I hope this can help you, let me know if you have any further question.

这篇关于对Bot命令使用其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆