我在运行此命令时遇到一些问题:导出工作正常(控制台日志中没有问题),但是当我执行其中一些命令时,什么都没有发生...
let _exports = require("../exports.js");
module.exports.main = function(guild,command,type,member,message) {
const Discord = require('discord.js');
const prefix = "!"
Client.on("ready", () => {
console.log("online");
Client.user.setPresence({ game: { name: `Hello world`, type: 0} });
});
// welcome message
Client.on("guildMemberAdd", member => {
member.guild.defaultChannel.send("Welcome to: " + member.guild.name + "")
});
Client.on("guildMemberRemove", member => {
member.guild.defaultChannel.send("Goodbye: " + member.user.username + " from " + member.guild.name)
});
Client.on("guildCreate", guild => {
console.log("server owner: " + guild.owner.user.username)
});
Client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
let command = message.content.split(" ")[0];
command = command.slice(prefix.length);
let args = message.content.split(" ").slice(1);
if (command === "ping") {
message.channel.send(`Pong! Time took: ${Date.now() - message.createdTimestamp} ms`);
} else
if (command === "say") {
message.delete()
const embed = new Discord.RichEmbed()
.setColor(0x954D23)
.setDescription(message.author.username + " says: " + args.join(" "));
message.channel.send({embed})
} else
if (command == "helpz") {
const embed = new Discord.RichEmbed()
.setColor(0x954D23)
.setTitle("Command List:")
.addField("!help", "Will give the current command list")
.addField("!ping", "WIll show the ping time for the bot")
.addField("!say [text]", "Will make the bot say something")
message.channel.send({embed})
}
if(command === `slive`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("**LIVE, ALL!**", ("good luck!"));
return message.channel.send('@here', botembed);
}
if(command === `sremake`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("**RMK / ALL!**", ("rmk"));
return message.channel.send('@here', botembed);
}
if(command === `cd15s`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("Next lobby starts in **15** Seconds", ("00:15"));
return message.channel.send('@here', botembed);
}
if(command === `cd30s`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("Next lobby starts in **30** Seconds", ("00:30"));
return message.channel.send('@here', botembed);return message.channel.send('@here', botembed);
}
if(command === `cd1m`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("Next lobby starts in **1** Minute", ("01:00"));
return message.channel.send('@here', botembed);return message.channel.send('@here', botembed);
}
if(command === `cd2m`){
let botembed = new Discord.RichEmbed()
.setColor("#C02909")
.addField("Next lobby starts in **2** Minutes", ("02:00"));
return message.channel.send('@here', botembed);
}
});
};
如果您知道问题出在哪里,只需修改源代码!预先谢谢你
答案 0 :(得分:0)
如果这是您的整个文件,则由于未定义Client
而无法工作:您可以将客户端作为参数传递给module.exports.main
函数,也可以在顶部声明。 br />
话虽如此,我不会导出所有内容:由于您仅声明事件监听器,因此可以将它们直接添加到声明客户端的相同函数中。仅当您希望将每个命令保存在不同的文件中时,导出才有用(也可以使用discord.js-commando
)。在这种情况下,您应该在主文件中声明侦听器,然后需要命令文件。
方案:
// main file
const Discord = require('discord');
const client = new Discord.Client();
client.on('ready') {...}
// other "client.on"s
client.on('message') {
// command parsing & co
if (command == 'say') require('./commands/say.js').main()
}
// command file: '/commands/say.js'
module.exports.main = (message, ...args) => {
...
};