0
votes

I'm trying to edit an embed message sent by my bot (today) using this code:

const args = message.content.slice(prefix.length).split(' ');
if (!args.length) {
    return message.channel.send('Non hai specificato il messaggio!');
}

const nuovoMsg = args.slice(3).join(' ');
const chId = args[1];
const msgId = args[2];

const messaggioNuovo = new Discord.MessageEmbed()
    .setColor('#c95f34')
    .setDescription(`${nuovoMsg}`);

const msgc = message.guild.channels.cache.get(chId).messages.cache.get(msgId);
msgc.edit(messaggioNuovo);

In a Discord text channel I sent :

njb!embede 709683421210869842 709711387324317746 Prova della modifica

Console Error :

TypeError: Cannot read property 'edit' of undefined
    at Object.execute (C:\NNJPBot\commands\embede.js:32:8)
    at Client.<anonymous> (C:\NNJPBot\index.js:81:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\NNJPBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\NNJPBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\NNJPBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\NNJPBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\NNJPBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\NNJPBot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)

Info : njb! : Prefix

embede : Command

1
I can't see where message.guild.channels.cache.get(...).messages.cache.fetch is being called. Could you please show the code that caused the error (at embede.js:30:70)? - cherryblossom
Sorry, wrong code and error. Now I've fixed it. Sorry again! - Renato

1 Answers

0
votes

What's happening is the message cache does not have the message with msgId. By default, Discord.js caches up to 200 messages per channel, so most likely the message with msgId has been removed from the cache.

You would need to fetch the message:

// With await (requires it to be in an async function)
const msgc = await message.guild.channels.cache.get(chId).messages.fetch(msgId)
msgc.edit(messaggioNuovo)

// With then
message.guild.channels.cache.get(chId).messages.fetch(msgId).then(msgc =>
  msgc.edit(messaggioNuovo)
)