我将如何发布公告到上述频道?

时间:2018-10-03 16:20:34

标签: javascript node.js discord discord.js

我该怎么做,以便您说;announce #channel message?因此,它不是将消息发送到您在其中编写消息的通道中发送,而是将其发送到您提到的通道中?

if (message.content.toLowerCase().startsWith(prefix + `announce`)) {
  if (message.member.hasPermission("ADMINISTRATOR")) {
    let args = message.content.split(" ").slice(1).join(" ");
    let split = args.split("-");
    let url = args[2];
    message.channel.sendMessage("@everyone", {
      embed: {
        color: 0xFFFF00,
        title: "New Announcement!",
        description: split[0],
        url: split[1],
        timestamp: new Date(),
        footer: {
          icon_url: message.author.avatarURL,
          text: message.author.username
        }
      }
    });
  }
}

1 个答案:

答案 0 :(得分:2)

Message.mentions包含消息中的所有提及,其属性.channels为您提供提及的每个渠道。
知道您可以从提及中获得频道,然后将其从参数中删除。

这是一个例子:

if (message.content.toLowerCase().startsWith(prefix + `announce`)) {
  if (message.member.hasPermission("ADMINISTRATOR")) {
    // I've added this part
    let channel = message.mentions.channels.first(); // you get the first mentioned channel
    if (!channel) return message.reply("No channel mentioned."); // if it doesn't exist, you exit
    let args = message.content.split(" ").slice(2).join(" "); // if it exist, you remove the command AND the channel

    let split = args.split("-");
    let url = args[2];
    channel.sendMessage("@everyone", { // here you send it to your channel instead of the same one
      embed: {
        color: 0xFFFF00,
        title: "New Announcement!",
        description: split[0],
        url: split[1],
        timestamp: new Date(),
        footer: {
          icon_url: message.author.avatarURL,
          text: message.author.username
        }
      }
    });
  }
}