0

So I programmed a system that saves the ids of certain channels (plus messages) and saves them in a file. After restarting the bot, the IDs will be read and reused. For example, fetching the specific message to edit it, therefore. Unfortunately, the cache is undefined, empty or an error occurs. Could somebody show me how to fetch a message which isn't in the cache?

Example code for an existing message and channel:

const {Discord, Client, Intents, Permissions, MessageEmbed} = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MESSAGE_TYPING, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGE_TYPING, Intents.FLAGS.GUILD_VOICE_STATES]});

bot.channels.cache.get('597102497420673025').messages.cache.get('959413368911966251').embeds;

Error:

var test = bot.channels.cache.get('597102497420673025').messages.cache.get('959413368911966251').embeds;
                                                                                                  ^

TypeError: Cannot read properties of undefined (reading 'embeds')
at Client.<anonymous> (D:\E-verysBot\index.js:2288:99)
    at Client.emit (node:events:402:35)
    at WebSocketManager.triggerClientReady (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:17)
    at WebSocketManager.checkShardsReady (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:367:10)
    at WebSocketShard.<anonymous> (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:189:14)
    at WebSocketShard.emit (node:events:390:28)
    at WebSocketShard.checkReady (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
    at WebSocketShard.onPacket (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
    at WebSocketShard.onMessage (D:\E-verysBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (D:\E-verysBot\node_modules\ws\lib\event-target.js:199:18)

Node.js v17.3.0
Atom Brecher
  • 95
  • 1
  • 1
  • 11
  • You can just use `await guild.messages.fetch('channel-id')` – Caladan Apr 02 '22 at 17:47
  • How to proceed if I want to edit the fetched message? (synchronous) – Atom Brecher Apr 02 '22 at 17:52
  • I don't think that you can use `messages.fetch()` synchronously. To edit the message all you have to do is: `const messageToEdit = await guild.messages.fetch('message-id')`. (By the way, in my first comment, it should be `await guild.messages.fetch('message-id')`) – Caladan Apr 02 '22 at 17:57
  • So is there any way to fetch the message, like with cache synchronously? – Atom Brecher Apr 02 '22 at 18:10
  • `fetch()` retrieves data asynchronously, it will always check the cache first before making an API call. If you only want to check the cache you would use `...cache.get('id')` – Elitezen Apr 02 '22 at 19:40
  • So fetching also doesn't work, because its checking the cache and it's according to the error empty, respectively doesn't contain the specific message I need. – Atom Brecher Apr 02 '22 at 21:21

1 Answers1

1
  • You fetch a message, to cache it.
// asynchronously
const channel = client.channels.cache.get(`channelId`);
const message = await channel.messages.fetch(`messageId`);
return message.embeds;
// synchronously
const channel = client.channels.cache.get(`channelId`);
channel.messages.fetch(`messageId`).then(message => {
   return message.embeds;
})

I hope this fixes your problem!

Darshan B
  • 658
  • 1
  • 5
  • 19