【问题标题】:Discord node js "TypeError: channel.send is not a function"Discord 节点 js “TypeError:channel.send 不是函数”
【发布时间】:2019-12-05 13:59:15
【问题描述】:

目前正在创建一个不和谐的机器人并遇到一些错误,声称“channel.send”不是一个函数。

这是一个允许机器人向公会中的每个频道发送消息的命令。

case "msgall":
    if (!args[1]) return message.reply("enter a msg")
    if (message.author.bot) return undefined;
    announce = args.slice(1).join(" ")
    message.delete();
    message.guild.channels.forEach(channel=>{
        channel.send(announce)
        channel.send(announce)
        channel.send(announce)
    }).catch(console.log);
    break;

它应该能够向服务器上的每个通道发送消息,但它一直抛出错误。

【问题讨论】:

    标签: node.js discord


    【解决方案1】:

    原因是 message.guild.channels 不是 Channel 对象的数组。根据documentation,您首先需要找到频道(按名称或频道ID):

    // Get a Channel by Name message.guild.channels.find(channel => channel.name === "channel-name"); // returns <Channel>

    我建议您创建一个频道名称(或 ID)数组并遍历该数组。例如:

    const channels = ['channel-1', 'channel-2'];
    channels.forEach(c => {
      const ch = message.guild.channels.find(channel => channel.name === c);
      ch.send(announce);
    });
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2020-09-25
      • 2018-03-23
      • 1970-01-01
      • 2021-04-06
      • 2017-07-28
      • 1970-01-01
      • 2018-09-21
      • 2017-09-29
      相关资源
      最近更新 更多