Monday, July 11, 2022

[FIXED] How can I fetch a message from a channel? discord.js

Issue

I reused a snipe command code to make this fetch command but that's not really my issue here.

I'm trying to fetch a message from a channel and post it in a designated channel, for example: Grab the message in X, and post it in Y. If that makes sense, all I have so far are:

const Discord = require('discord.js');

module.exports = class FetchCommand extends BaseCommand {
  constructor() {
    super('fetch', 'fun', []);
  }

  async run(client, message, args) {
    const msg = client.snipes.get(message.channel.id);
    if (!msg) return message.channel.send('There are no messages to fetch.');
    
    const fetchEmbed = new Discord.MessageEmbed()
      .setAuthor(msg.author.tag, msg.author.displayAvatarURL())
      .setDescription(msg.content)
      .setTimestamp()

    message.channel.send(fetchEmbed);
  }
}

Help is very appreciated!

PS: As of right now, it fetches the messages from the channel it is running the command it. If I sent a message in X channel, and run the command in X channel it would fetch the message in the X channel. My goal is trying to fetch a message from a channel and post it in another channel.


Solution

If you have the channel ID and message ID: await message.guild.channels.cache.get('channel-id').messages.fetch('message-id') (async functions only)

If you just have the channel ID and want the last message that wasn't the command: (await message.guild.channels.cache.get('channel-id').messages.fetch({ count: 2 })).first()



Answered By - 0xLogN
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.