当我运行具有冷却功能的机器人时出现错误。 这是我的代码的样子:
const Discord = require('discord.js');
const talkedRecently = new Set();
const client = new Discord.Client();
client.login('Redacted', () => {
console.log('dahelper connected via nodejs servers');
});
client.on('message', message => {
if (message.content === '!hello') {
message.channel.send('Hello to you too!');
}
});
if (talkedRecently.has(message.author.id)) {
message.channel.send("You can only use this command once every 3 minutes, use it wisely! - " + msg.author);
} else {
client.on('message', message => {
if (message.content === '!claim') {
message.channel.send('You have claimed 250 guild moneys.');
}
});
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(msg.author.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(msg.author.id);
}, 180000);
}
我收到一条错误消息,基本上是“如果 (talkedRecently.has(message.author.id)) msg 未定义”。我知道为什么。因为我从来没有在代码中的任何地方声明过“msg”,它不知道我在找什么,在这种情况下,我应该把所有写着“msg”的东西都改成“message”我试过这个,我得到了确切的同样的错误,在完全相同的地方,除了它说“消息未定义”。我是 discord.js 的新手,我知道的不多,所以请帮助我。
答案 0 :(得分:0)
您的代码在消息事件的处理程序之外:
client.on('message', message => {
if (message.content === '!hello') {
message.channel.send('Hello to you too!');
}
} /* Function ends here */);
// The rest of your code is outside of it
if (talkedRecently.has(message.author.id)) { // ...
在确保使用正确的变量名后,您应该将其放入函数中。