【发布时间】:2020-09-13 10:32:19
【问题描述】:
我遇到了一些错误:
- 第 12 行:
TypeError: Cannot read property 'user' of undefined - 第 14 行:
TypeError: Cannot read property 'guilds' of undefined
我可能还有一些我还没有看到的其他错误。
如何解决这些错误?
这是我的 Discord 机器人的代码:
console.log("hi");
const { RichEmbed } = require("discord.js");
const randomPuppy = require("random-puppy");
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'token';
client.on('ready', async client => {
console.log('This bot is online');
client.user.setActivity("Youtube", {type: "Watching"})
client.guilds.cache.forEach((guild) => {
console.log(guild.name)
guild.channels.cache.forEach((channel) => {
console.log(` - ${channel.name} ${channel.type} ${channel.id}`)
})
//general text id: 721950719657115750
})
const jpg = "https://cdn.discordapp.com/attachments/709199176562638849/722646217930178620/OIPK7JS66QT.jpg"
const img = await randomPuppy(jpg);
const embed = new RichEmbed()
.setColor("RANDOM")
.setImage(img)
.setTitle(`From /r/${random}`)
.setURL(`https://reddit.com/r/${random}`);
message.channel.send(embed);
})
client.on('message', msg=>{
if(msg.content === "hello"){
msg.reply('hello')
}
})
client.login(token);
【问题讨论】:
-
看起来客户端未定义..这就是为什么它显示无法读取未定义的用户,因为您正在访问未定义的客户端对象上的用户属性...我建议您查看已经存在的答案所以。谢谢!
-
请重新生成您的 Discord 机器人令牌,因为该令牌已公开。永远不要共享您的令牌并将环境变量用于机密。
标签: javascript discord discord.js