Search code examples
fetchdiscord.jsmessageedit

Why am I getting an error fetching messages?


there is an error calling the script. Can you ask for help?

Below I paste the code and the error.

let chn = client.channels.find(channel => channel.id === '717019301357420554');
let msg = chn.fetchMessage('717369584801546273');
msg.edit(embedit);
TypeError: msg.edit is not a function

Solution

  • Is this v11?

    Regardless fetching something is async, so you need to wait for msg to resolve.

    https://discord.js.org/#/docs/main/11.1.0/class/TextChannel?scrollTo=fetchMessage

    Here's how you can go about it:

    First one is using await which needs to be inside of an async function

    let chn = client.channels.find(channel => channel.id === '717019301357420554');
    let msg = await chn.fetchMessage('717369584801546273');
    msg.edit(embedit);
    

    Second is .then

    let chn = client.channels.find(channel => channel.id === '717019301357420554');
    chn.fetchMessage('717369584801546273').then(msg => msg.edit(embedit));
    

    If you want to save it in a variable

    let chn = client.channels.find(channel => channel.id === '717019301357420554');
    let msg;
    chn.fetchMessage('717369584801546273').then(m => msg = m);
    //note you will have to wait for the promise to resolve to use 
    //the variable msg correctly
    //any code beyond this isn't guranteed to have access to 
    

    These are some bad variable names btw, you shouldn't use abbreviations like chn rather channel, and embedit => embEdit. But up to you