Discord.js v12-向特定频道中的所有服务器发送公告

时间:2020-09-21 09:23:18

标签: node.js discord.js

我目前正在尝试为事件或类似内容创建某种公告命令。 我目前的问题是它要么将消息发送X次到一台服务器(X =机器人所在的服务器数量),要么将其发送到一个随机通道,而不是我想要发送的消息(我仍然不知道为什么会这样。

module.exports.run = async (bot, message, args) => {
  async function announcement(guild, user) {
    const threshold = 0.85;
    const channel = message.guild.channels.cache.find((channel) => {
      const result = stringSimilarity.compareTwoStrings(
        channel.name.toLowerCase(),
        config.events
      );
      if (result >= threshold) {
        return true;
      }
      return false;
    });
    bot.guilds.cache.forEach((guild) => {
      console.log(guild.name);
      //This is where I would send the message
      //channel.send('message here')
    });
  }
  async function myFunc() {
    for (let guild of bot.guilds.cache) {
      const response = true;
      if (response) {
        announcement();
      }
    }
    resolve();
  }
};

myFunc();

是的,我知道,我的承诺目前还算没用,仍然必须为此做些事情。

1 个答案:

答案 0 :(得分:0)

您需要为每个公会找到一个频道,因此必须像这样创建

bot.guilds.cache.forEach((guild) => {
 console.log(guild.name);

 //This would make the client to send a message "Announcement!" to a channel named "news" in every server, if such a channel didn't exist in a guild, the message wouldn't be sent for that exact guild
 try {
  guild.channels.cache.find((x) => x.name == 'news').send('Announcement!');
 } catch {
  //If message couldn't be sent
  console.log("Message wasn't sent for " + guild.name);
 }
});