【问题标题】:"Cannot read property 'name' of null" when trying to read server name尝试读取服务器名称时“无法读取 null 的属性‘名称’”
【发布时间】:2019-03-14 00:55:11
【问题描述】:

当我执行console.log(message.guild.name) 时,它正常工作,给我公会的名称,其中包含一条消息。但是,由于某种原因,当我在向用户发送 DM 时运行该代码时,它给了我以下错误:

(node:36816) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of null'

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    DM channel 不在公会中,因此不能附加公会

    您需要检查message.channel.type 属性以确定它是什么类型的通道。消息可以来自这些频道类型中的任何一种TextChannel(又名公会文本频道)、DMChannelGroupDMChannel

    查看channel.type 时,它会为您提供以下选项之一:

    • dm - DM 频道
    • group - 群组 DM 频道
    • text - 公会文字频道
    • voice - 公会语音频道
    • category - 公会分类频道

    你可以这样做:

    switch (message.channel.type) {
        case 'text':
            // Do guild stuff
            break;
    
        case 'dm':
        case 'group':
            // Do DM stuff
            break;
    
        default:
            // Do stuff on unexpected channel
            break;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 2020-08-31
      • 2021-10-14
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 2023-03-11
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多