Search code examples
discord.js

How do I edit a message from a different channel that my bot sent?


I'm making a suggestion module on my bot, and I have everything else down, but am struggling on fetching the message the bot sent and editing it using a command that was sent in a different channel.

I tried doing await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' }) but it didn't work.


Solution

  • Your code, while in theory, should work, most likely doesnt due to unresolved promises.

    In the line

    await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' })
    

    The await at the beginning is trying to resolve the promise of the edit method, where there is another promise to be resolved at the <MessagesManager>#fetch method. In other words you're ignoring the promise returned from the fetch which will resolve no messages. Consider the following change:

    const messages = await message.guild.channels.cache.get("channel-id-here").messages;
    await messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' });
    

    Or if you really want a one-liner (I dont suggest this as it compromises on readability) you could use:

    await (await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here")).edit({ content: 'Test'