TypeError:client.on不是函数(discord.js)



我一直收到这个错误,我不确定自己做错了什么。

TypeError: client.on is not a function

这是我的代码:

const client = require("../index");
const { promisify } = require("util");
const wait = promisify(setTimeout);
const { MessageEmbed } = require("discord.js")

let invites;
module.exports = {
name: "setchannel",
description: "Sets the channel.",
async execute(message, args) {
const channelids = new MessageEmbed()
.setDescription("Please enter a channel or channel ID.")
.setColor("#E74C3C");
client.on("ready", () => {
console.log("Ready!");
client.user.setActivity("$help", { type: "PLAYING" }).catch(console.error);

client.guilds.cache
.get(message.guild.id, true)
.fetchInvites()
.then((inv) => {
invites = inv;
});

});

这是在一个单独的文件中,而不是我的index.js文件。我已声明const client = new Discord.Client();在我的index.js文件中,但我不确定问题是什么。

client.on('ready')在此文件中是无用的。首先,你应该在你的index.js中使用它。其次,客户端从未在index.js定义为module.exports,因此它是未定义的。你不能指望JavaScript只是浏览你的文件并从那里获得客户端!此外,如果您尝试message.client.on('ready'),它不会抛出错误,但也不会运行任何东西,因为它几乎是一次运行的,并且您获得消息的唯一方法是,如果客户端已经准备好,那么ready就不会运行两次。我的解决方案是删除该文件中的client.on

问题是const client = require("../index");实际上并没有引用index.js中实例化的discord客户端。您需要导出客户端才能从另一个文件访问它。

在你的index.js:

module.exports = {
client
}

在您的第二个文件中:

const { client } = require("../index");

最新更新