msg.content not being defined



好的,所以我试图为我的服务器发出一个公告命令,但当我尝试运行它时,它会说:ReferenceError:没有定义msg

我不确定我做错了什么。

这是有问题的代码:

'use strict';
//require discord.js package
const Discord = require("discord.js");
const config = require("./config.json")
//Create an instance of a Discord client
const client = new Discord.Client();
//The ready event is vital, only after this will your bot start reacting and responding
client.on('ready', () => {
console.log('I am ready!');
});
//create an event listener for messages
client.on('message', message => {
if (message.content === 'ping') {
message.channel.send('pong');
}
});
if (msg.content === "^ann"){
let channel = client.channels.cache.get('channel id');
msg.channel.send("What is your announcement? (^cancel to cancel)");
const collector = msg.channel.createMessageCollector(m => m.author.id === msg.author.id, { time: 100000000 });
collector.once('collect', m => {
if (m.content == "^cancel") {
m.author.send("Announcement cancelled.");
return;
} else {
var announcement = m.content;
channel.send(announcement);
msg.channel.send("Announcement made!");
return;
}
});
//Do stuff
client.on("message", function (msg) {
if (msg.content.indexOf("^mirror") === 0) {
let item = new Discord.MessageEmbed()

.setImage(msg.author.displayAvatarURL())
.setColor("#E6E6FA")
.setFooter("OMG! WHEW~");

msg.channel.send(item);
}
})


//Bot Token
client.login('token')}

这就是我正在上工作的代码

出现错误是因为您的代码在消息事件之外。因此,所有关于消息等的代码都必须在消息事件中。

第二件事,我建议创建一个消息事件。我看到你已经创建了2个消息事件。这不是什么大问题,但它会更有效地清理代码。现在,如果有人发送消息,他会检查此消息2次。我希望我说得清楚一点!

最新更新