问题描述
我想通过将所有较长的内容放在另一个文件中来使我的代码看起来干净且易于理解.我有我的主文件(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)
和我的其他文件夹(commands.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 中,您正在导出 help function,所以当您调用它时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.
这篇关于对机器人命令使用不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)