使用 discord.js V12 获取消息对我不起作用。[Discord.js V12]

Cho*_*ini 6 javascript node.js discord.js

正如您在我的标题中看到的那样,我无法使用 discord.js 获取消息。

在 discord.js v11 中,我使用了这个:

var bot = new Discord.Client();
bot.on('ready', () => {
  bot.channels.get(channelID).fetchMessages({ around: messageID, limit: 1 })
    .then(async msg => {
      //my code here 
    });
});
Run Code Online (Sandbox Code Playgroud)

Id discord.js v12 应该是这样的:

var bot = new Discord.Client();
bot.on('ready', () => {
  bot.channels.cache.get(channelID).messages.fetch({ around: messageID, limit: 1 })
    .then(async msg => {
      //my code here 
    });
});
Run Code Online (Sandbox Code Playgroud)

但它对我不起作用..

你能帮我吗?可能有其他选择。

感谢您的帮助 !

编辑1:它返回: (node:17184) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'messages' of undefined

Tom*_*dos 1

Discord.js v12 不使用client.channels.cache.get(id) ,但 client.channels.resolve(id) https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=resolve

我不太明白消息属性的含义,因为通道在文档中没有类似的属性。https://discord.js.org/#/docs/main/stable/class/Channel

  • `client.channels.cache.get(id)` 在 v12 中工作得很好。Manager 的 `cache` 属性是一个继承了 `Map#get()` 的扩展 Maps 的 Collection。在这种特定情况下,两者都应返回相同的值(前者使用后者)。此外,基本 Channel 结构没有 `messages` 属性,但 TextChannel 有([此处](https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=messages ))。 (3认同)