Reputation: 13
I would like to know how to find a message with its id from the entire guild and not just the current channel.
I have tried
message.guild.channels.fetchMessage(message_ID)
client.channels.fetchMessage(message_ID)
message.channel.guild.fetchMessage(message_ID)
message.fetchMessage(message_ID)
message.guild.fetchMessage(message_ID)
I want it to look through the entire guild and find the message by ID then send what the message has said. Also, I have tried to have it look for the channel that I want the message from but that didn't work either.
If it's possible to get more specific with the code, it should look into a channel called #qotd-suggestions
with the ID of 479658126858256406
and then find the message there and then send the message's content.
Also, the message_ID part of fetchMessages
gets replaced with what I say after the command so if I say !send 485088208053469204
it should send Message content: "Wow, the admins here are so lame."
Upvotes: 1
Views: 4784
Reputation: 6796
There's no way to do that with a Guild
method, but you can simply loop through the channels and call TextChannel.fetchMessage()
for each of them.
In order to do that, you could use 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()
.
Here's an example:
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;
}
}
You can then use this function by passing the message that triggered the command (from which you get the guild) and the ID you're looking for. It returns a Promise<Message>
or, if the message is not found, Promise<undefined>
. You can get the message by either using await
or Promise.then()
:
let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});
Upvotes: 3