'Discord.JS fetchMessage()

I have a problem with my Discord.JS Bot, I wanna to edit a embed in a spefic guild and channel, but when I try to run the commands, it errors out with this

/root/my-bot/my-bot.js:550
    guild.channels.get(channel).fetchMessage(user).edit(newMessage);
                               ^

TypeError: Cannot read property 'fetchMessage' of undefined
at Client.<anonymous> (/root/my-bot/my-bot.js:550:31)
at Client.emit (events.js:315:20)
at MessageCreateHandler.handle (/root/my-bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/root/my-bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65)
at WebSocketConnection.onPacket (/root/my-bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (/root/my-bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
at WebSocket.onMessage (/root/my-bot/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (/root/my-bot/node_modules/ws/lib/websocket.js:789:20)
at Receiver.emit (events.js:315:20)

Here's my command code:

 if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id')
     const channel = args[0];
     const user = args[1];
     let newMessage = args.slice(2).join(' ');
     guild.channels.get(channel).fetchMessage(user).edit(newMessage);
}


Solution 1:[1]

> If you are using Discord.js v11, the issue may come from the channel id you gave in the command.
> It appears that you try to fetch messages from an user but the method .fetchMessage() takes a message id as a parameter (See Discord.js documentation).
> You can access the fetched Message with .then(callback function)

Fix

if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id');
     const channel = args[0];
     const messageId = args[1];
     let newMessage = args.slice(2).join(' ');
     let fetchedChannel = guild.channels.get(channel);
     if (!fetchedChannel) return console.log('Channel not found');
     fetchedChannel.fetchMessage(messageId).then(message => {
         message.edit(newMessage);
     }).catch(error => console.error(error));
}

Solution 2:[2]

Here is a possible fix:

 const prefix = 'YOUR_PREFIX';
if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id');
     const channel = args[0];
     const messageId = args[1];
     let newMessage = args.slice(2).join(' ');
     let fetchedChannel = guild.channels.get(channel);
     if (!fetchedChannel) {
         return console.log('Channel not found');
     }
     fetchedChannel.fetchMessage(messageId).then(message) => {
         message.edit(newMessage);
     }).catch(error => console.error(error));
}

Solution 3:[3]

 if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id')
     const channel = args[0];
     const user = args[1];
     let newMessage = args.slice(2).join(' ');
     guild.channels.get(channel).messages.fetch(user).then((msg) => {
       msg.edit(newmessage);
     })
}

you can try this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Coding Master
Solution 3 KaizOffical