【问题标题】:how to send a message to every channel in every guild?如何向每个公会的每个频道发送消息?
【发布时间】:2020-05-24 16:57:49
【问题描述】:

类似于this question,但是我希望能够将其发送到它可以访问的每个频道! 在我通过 ID 验证自己和发出的命令后,在 on message 事件中我正在使用此代码:

      const listedChannels = [];
      msg.guild.channels.forEach(channel => {
      //get all channels
      client.channels.get(channel.id).send("you like bred? (message) ");
      //send a message to every channel in this guild
      });

但是我得到 .send 不是函数的错误...

我被告知在获得频道 ID 后使用.send

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    如果您要循环浏览所有频道,您只需将内容发送到您已经从msg.guild.channels.forEach(channel => {//code}) 获得的频道。

    .forEach 块中的内容替换为;

    channel.send("You like bred? (message)");

    虽然这会发送You like bred? (message)

    如果您想获得回复,或许可以查看 this answer,它解释了通过对不和谐消息的反应来收集回复。

    【讨论】:

      【解决方案2】:

      以下解释仅适用于 v11(稳定版)。

      Client.channels 是您的机器人正在观看的Channels 中的Collection。您只能将消息发送到文本频道,并且此收藏集也将包括 DM 频道。出于这个原因,我们可以使用Collection.filter() 来检索公会内仅包含文本通道的新集合。最后,您可以遍历通道并在每个通道上调用 TextChannel.send()。因为您正在处理Promises,所以我推荐Promise.all()/Collection.map() 组合(请参阅超链接文档)

      例如...

      // assuming "client" is your Discord Bot
      
      const channels = client.channels.filter(c => c.guild && c.type === 'text');
      Promise.all(channels.map(c => c.send('Hello, world!')))
        .then(msgs => console.log(`${msgs.length} successfully sent.`))
        .catch(console.error);
      

      【讨论】:

        【解决方案3】:

        您可以为此使用client.channels。检查频道类型是否为公会文本频道,然后尝试发送消息。

        client.channels.forEach(channel => {
            if(channel.type === 'text') channel.send('MSG').catch(console.error)
        })
        

        【讨论】:

          猜你喜欢
          • 2021-04-27
          • 2020-12-10
          • 2020-04-14
          • 1970-01-01
          • 2020-09-23
          • 2022-01-07
          • 2019-06-06
          • 1970-01-01
          • 2021-07-02
          相关资源
          最近更新 更多