【问题标题】:Node.js Discord Bot : TypeError: Cannot read property 'send' of undefinedNode.js Discord Bot:TypeError:无法读取未定义的属性“发送”
【发布时间】:2018-02-13 01:44:03
【问题描述】:

我希望我的公共机器人在特定频道和特定服务器中发送消息。但是,我有一个错误......这是我的代码:

client.on('message', msg => {
  if (msg.content.startsWith('+specifictest')) {
    var channellog = msg.client.channels.get('352496750327496725');
    var guiiild = msg.client.guilds.get('343913599686934539').channellog;
    guiiild.send({
      embed: new Discord.RichEmbed()
                        .setColor("#FFFFFF")
                        .setAuthor("Dessin")
                        .setDescription(`Demandé par <@${msg.author.id}>`)
    })
  }
})

还有,我的错误:TypeError: Cannot read property 'send' of undefined

【问题讨论】:

    标签: javascript node.js discord


    【解决方案1】:

    此错误(在您的上下文中)意味着您的变量 guiiild 未正确填充,因此当它尝试使用不存在的属性(在本例中为 send 函数)时出现意外失败。

    尝试/抓住

    您可以将其包装在 try/catch block 中:

    client.on('message', msg => {
      if (msg.content.startsWith('+specifictest')) {
       try{
        var channellog = msg.client.channels.get('352496750327496725');
        var guiiild = msg.client.guilds.get('343913599686934539').channellog;
        guiiild.send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
       }catch(e){console.log("[ERROR]",e)}
      }
    })
    

    但是如果msg.client.guilds.get('343913599686934539').channellog 没有返回任何包含.send 的东西,它仍然会给你错误

    【讨论】:

    • 我已经尝试了你的代码,现在我有这个错误:[ERROR] TypeError: Cannot read property 'send' of undefined :c
    • @SplatingWorld,你明白我的回答吗? (它不仅仅是复制粘贴)
    • 是的,对不起... :c
    • channellog不是函数吗?还是没有类似 guild.GetChannelLogs() 的东西?
    【解决方案2】:

    你可以试试这个:

    client.channels.get("ID").send("Your message")
    

    ID 将是您要将消息发送到的频道的 ID。所以在你的情况下,试试:

    client.on('message', msg => {
      if (msg.content.startsWith('+specifictest')) {
        client.channels.get("352496750327496725").send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
      }
    })
    

    【讨论】:

      【解决方案3】:

      这可能会迟到,但是 尝试使用await

      我认为您使用的是 Disord v11 而不是 12,我建议升级到 12,但这是您的决定

      V11

      client.on('message', async (msg) => {
        if (msg.content.startsWith('+specifictest')) {
          const channel = await client.channels.get("352496750327496725")
          channel.send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
       }
      })
      

      v12

      client.on('message', async(msg) => {
        if (msg.content.startsWith('+specifictest')) {
          const channel = await client.channels.cache.find(x => x.id == "352496750327496725")
          channel.send(new Discord.MessageEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par ${msg.author}`)}) //this is the same as <@ID>
        }
      })
      

      【讨论】:

      • 谢谢兄弟,你的生活更安全了,非常感谢
      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2021-02-01
      • 2022-01-24
      • 2020-12-19
      相关资源
      最近更新 更多