如何在Discord机器人中修复这些TypeError?

时间:2020-06-20 05:27:45

标签: javascript discord discord.js

我遇到一些错误:

  • 第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);

1 个答案:

答案 0 :(得分:0)

尝试

client.user.setPresence({ 

     activity: {name: 'Youtube'}, 
     status: 'Watching' 

})