【问题标题】:TypeError: Cannot read property 'get' of undefined while trying to editTypeError:尝试编辑时无法读取未定义的属性“get”
【发布时间】:2021-10-05 00:18:03
【问题描述】:

我尝试使用以下代码获取公会:

const guild = Discord.Client.guilds.get("GUILD_ID");

但我收到以下错误消息,但我不明白为什么:

TypeError: 无法读取未定义的属性“get”

【问题讨论】:

    标签: javascript discord.js undefined typeerror


    【解决方案1】:

    Discord.Client 是一个类。您要查找的是client.guilds.get("GUILD_ID"),其中clientnew Discord.Client(),已登录并可以使用。

    以下应该有效:

    const Discord = require('discord.js');
    const client = new Discord.Client();
    
    client.once('ready', async () => {
      console.log('Bot is connected...');
      let guild = client.guilds.cache.get('GUILD_ID'); 
      // OR!
      guild = await client.guilds.fetch('GUILD_ID'); 
      console.log(guild.name)
    });
    
    client.login('TOKEN');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2017-08-30
      • 1970-01-01
      • 2022-01-26
      • 2013-01-19
      • 2018-03-15
      • 2020-12-18
      相关资源
      最近更新 更多