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
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
- Could you please explain what "doesn't work"? What do you expect to see? What are you actually seeing? Are there any reported error messages? – Phil Commented Mar 30, 2020 at 4:05
- Yes sorry, I edited my topic. – Choudini Commented Mar 30, 2020 at 4:19
- You shouldn't use the var keyword; prefer const and let instead. – ations Commented Apr 8, 2021 at 18:03
2 Answers
Reset to default 3Well, 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.
discord.js v12 doesnt use client.channels.cache.get(id)
but client.channels.resolve(id)
https://discord.js/#/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/#/docs/main/stable/class/Channel