How do I send a message to all guilds in discord.js with v12+ (12.0.0 and up)

回眸只為那壹抹淺笑 提交于 2020-03-21 07:08:10

问题


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

I tried using v11.2 but that was a K.O. It says that it is outdated and needs to be updates. What can I replace with this code?


回答1:


defaultChannel() is already deprecated and has no alternative for it. And you need to specify the channel on where to send the message, but since some servers have unique channel names, it won't work...unless they all have the same channel name and left it unchanged (some peeps change the name of general).

Well.. I made a code for it (works if the channels have the name "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!")
  }
}

Taken from: https://github.com/itsYuuki/SmoreBot/blob/master/commands/control/gann.js



来源:https://stackoverflow.com/questions/49216044/how-do-i-send-a-message-to-all-guilds-in-discord-js-with-v12-12-0-0-and-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!