如何从整个公会获取消息

How to fetch a message from the entire guild

我想知道如何从整个公会中找到一条消息,而不仅仅是当前频道。

我试过了

我想让它查看整个公会并通过 ID 找到消息,然后发送消息所说的内容。另外,我试图让它寻找我想要从中接收消息的频道,但这也没有用。

如果可以通过代码获得更具体的信息,它应该查看 ID 为 479658126858256406 的名为 #qotd-suggestions 的频道,然后在那里找到消息,然后发送消息的内容。

此外,fetchMessages 的 message_ID 部分被替换为我在命令后所说的内容,所以如果我说 !send 485088208053469204 它应该发送 Message content: "Wow, the admins here are so lame."

Guild 方法无法做到这一点,但您可以简单地遍历频道并为每个频道调用 TextChannel.fetchMessage()
为此,您可以使用 Collection.forEach() but that would be a little more complex if you want to work with vars, so I suggest you to convert Guild.channels to an Array with Collection.array().

这是一个例子:

async function findMessage(message, ID) {
  let channels = message.guild.channels.filter(c => c.type == 'text').array();
  for (let current of channels) {
    let target = await current.fetchMessage(ID);
    if (target) return target;
  }
}

然后您可以通过传递触发命令的消息(从中获取公会)和您要查找的 ID 来使用此功能。它 returns 一个 Promise<Message> 或者,如果找不到消息,Promise<undefined>。您可以使用 awaitPromise.then():

获取消息
let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});