Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch messages with discord.js V12 doesn't work for me. [Discord.js V12]

As you can read on my title, I can't fetch messages with discord.js.

In discord.js v11 I used this :

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

Id discord.js v12 it should be like this :

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 
    });
});

But it doesn't work for me..

Can you please help me for this ? May be with an other alternative.

Thank you for your help !

EDIT 1 : It return : (node:17184) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'messages' of undefined

like image 291
Choudini Avatar asked Mar 30 '20 03:03

Choudini


2 Answers

Well, I'm sorry, I figure it out that my code was correct, but I do not know why that doesn't worked when I tried it .. I literally wrote this:


bot.channels.cache.get('ChannelID').messages.fetch({ around: 'messageID', limit: 1 })

.then(async msg => {

 //my code here
})

That's the same shit, I lost hours for that '-'

Anyway, sorry for the lost of your time to.

I'll delete this.

like image 182
Choudini Avatar answered Sep 30 '22 15:09

Choudini


discord.js v12 doesnt use client.channels.cache.get(id) but client.channels.resolve(id) https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=resolve

I don't quite understand what you mean by messages property, as channel doesnt have any like that in documentation. https://discord.js.org/#/docs/main/stable/class/Channel

like image 43
Tomi Kordos Avatar answered Sep 30 '22 15:09

Tomi Kordos