如何获取带有 ID 的消息,而不将其放入缓存中

How to fetch a message with the ID, without having it in cache

所以我编写了一个系统来保存某些频道(加上消息)的 ID 并将它们保存在一个文件中。重启机器人后,ID 将被读取并重复使用。因此,例如,获取特定消息以对其进行编辑。不幸的是,缓存未定义、为空或发生错误。有人可以告诉我如何获取不在缓存中的消息吗?

现有消息和频道的示例代码:

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;

错误:

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
  • fetch留言,给cache吧。
// 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;
})

希望这能解决您的问题!