简体   繁体   English

Discord.js:从所有通道获取消息

[英]Discord.js: Fetch message from ALL channels

message.channel.messages.fetch does fine at fetching from the channel the command is run in, but I need to be able to fetch messages from ANY channel in the server. message.channel.messages.fetch可以很好地从运行命令的通道中获取消息,但我需要能够从服务器中的任何通道中获取消息。 For reference, I'm making a command to quote a message via message ID, but as of right now it can only quote messages from the same channel that the command is run in.作为参考,我正在创建一个命令来通过消息 ID 引用消息,但到目前为止,它只能引用来自运行该命令的同一通道的消息。

Loop through every channel and fetch messages in them.循环遍历每个通道并获取其中的消息。

message.guild.channels.cache.forEach(channel => {
  channel.messages.fetch().then(messages => {
    messages.forEach(msg => console.log(msg.content));
  });
});

This example fetches as many messages as possible from every channel in the server and logs the content of each one.此示例从服务器中的每个通道获取尽可能多的消息并记录每个通道的内容。 You could use an if statement to check if the message content is the specified quote to look for.您可以使用 if 语句来检查消息内容是否是要查找的指定引用。

MessageManager.fetch() MessageManager.fetch()

let found; message.guild.channels.cache.each(channel => { if(found) return; found = await channel.messages.fetch("ID_HERE").catch(() => undefined); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM