【问题标题】:How do I send a message to all guilds in discord.js with v12+ (12.0.0 and up)如何使用 v12+(12.0.0 及更高版本)向 discord.js 中的所有公会发送消息
【发布时间】:2018-08-19 08:10:32
【问题描述】:
if (command === "sendguildmessages") {
    if (message.author.id === "231956829159161856") {
        var guildList = client.guilds.array();
        try {
            guildList.forEach(guild => guild.defaultChannel.send("messageToSend"));
        } catch (err) {
            console.log("Could not send message to a (few) guild(s)!");
        }
    } else {
        message.reply(`You cant do that!`)
    }
} else

我尝试使用 v11.2,但那是一个 K.O. 它说它已过时,需要更新。我可以用这段代码替换什么?

【问题讨论】:

  • 所以您只想将消息"messageToSend" 发送到每个Guild 中的一个频道,或者每个Guild 中的所有TextChannels
  • 每个公会1个频道

标签: javascript node.js discord.js


【解决方案1】:

defaultChannel() 已被弃用并且没有替代方案。 而且您需要指定发送消息的通道,但是由于某些服务器具有唯一的通道名称,因此它将无法正常工作……除非它们都具有相同的通道名称并且保持不变(有些窥视者更改了名称一般)。

嗯.. 我为它编写了一个代码(如果频道名称为“general”,则可以使用)

if (command === "sendguildmessages") {
  if (message.author.id === "231956829159161856") {
    try {
      let toSay = "messageToSend"
      this.client.guilds.map((guild) => {
        let found = 0
        guild.channels.map((c) => {
          if (found === 0) {
            if (c.type === "text") {
              if (c.permissionsFor(this.client.user).has("VIEW_CHANNEL") === true) {
                if (c.permissionsFor(this.client.user).has("SEND_MESSAGES") === true) {
                  c.send(toSay);
                  found = 1;
                }
              }
            }
          }
        });
      });
    }
    catch (err) {
      console.log("Could not send message to a (few) guild(s)!");
    }
  } else {
    message.reply("You cant do that!")
  }
}

取自:https://github.com/itsYuuki/SmoreBot/blob/master/commands/control/gann.js

【讨论】:

  • 我试过了,但我有这个错误:EnhandledPromiseRejectionWarning: TypeError: client.guilds.map is not a function
  • discord.JS 现在使用缓存,所以你需要使用 client.guilds.cache 和 guild.channels.cache
猜你喜欢
  • 2020-12-10
  • 2023-03-20
  • 2021-02-08
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 2020-07-07
  • 2021-06-26
  • 2021-04-27
相关资源
最近更新 更多