4

Purpose: I'm trying to make my bot check for reactions from a specific message.

I've read this post: Get Message By ID: Discord.js But it didn't help at all.

I've searched the internet to see how can I use .fetchMessage properly. But unfortunately didn't find any results.

This is my code:

client.channels.get('CHANNEL ID').fetchMessage('MESSAGE ID').then(async msg => { *CODE HERE* });

This is the error i get:

TypeError: client.channels.get is not a function

I do realise that client.channels.get is not a function and I should use this in a function but I don't know how to.

Discord.js version: 12.0.2

Node.js verison: 12.13
ESCoder
  • 15,431
  • 2
  • 19
  • 42
Synæsthesia
  • 47
  • 1
  • 7
  • https://discord.js.org/#/docs/main/stable/class/Client https://discord.js.org/#/docs/main/stable/class/ChannelManager `client.channels` returns a ChannelManager. There it shows you the available methods as well as how to use them: https://discord.js.org/#/docs/main/stable/class/ChannelManager?scrollTo=fetch – Tin Nguyen Mar 18 '20 at 14:12

2 Answers2

4

That answer was for v11, in v12 it has changed to:

client.channels.cache.get(chid).messages.cache.fetch(mesid)

However, it's important to note that client.channels.cache may contain non-text channels, if you are retrieving an ID that you know to be a TextChannel type, you will be fine but if the ID is being retrieved programmatically you need to check to ensure it is an instanceof TextChannel.

Tarazed
  • 1,707
  • 1
  • 7
  • 22
  • I get this error: `Cannot read property 'message' of undefined`. I'm using the textchannel's ID btw – Synæsthesia Mar 18 '20 at 14:28
  • Apologies, that was a typo, it should be the plural ``messages`` – Tarazed Mar 18 '20 at 14:30
  • It still sees it undefined :( – Synæsthesia Mar 18 '20 at 14:31
  • In that case, it sounds like it's not finding the channel so channels.cache.get() is returning undefined. Are you sure you have a correct ID and your bot can see the channel (permissions-wise)? – Tarazed Mar 18 '20 at 14:32
  • I've tried `console.log(client.channels.cache.get('689781285148950545').name);` in `client.on(ready)` func. it logs it correct. – Synæsthesia Mar 18 '20 at 14:34
  • That's got me a bit stumped. Can you try ``console.log(client.channels.cache.get('689781285148950545') instanceof Discord.TextChannel``) (where ``Discord`` is the name of your require variable for discord.js). It should log true or false. – Tarazed Mar 18 '20 at 14:43
  • I'm afraid I am out of ideas myself then. A TextChannel should always have a MessageManager, I'm not sure how that could be undefined. – Tarazed Mar 18 '20 at 14:53
  • Quick Update: When try to check it with a command it works. So the code is right, but the usage is not. I've written this code in my Index.js which is my bot's main script. Any thoughts how to use it, so it'll check the message all the time whenever it's online? – Synæsthesia Mar 20 '20 at 11:38
1

In v12 it has changed and uses managers and added this command to my bot and fixed it.

let channelMessage = client.channels.cache.get(channel_id) // Grab the channel
channelMessage.messages.fetch(message_id).then(messageFeteched => messageFeteched.delete({timeout: 5000})); // Delete the message after 5 seconds
NullDev
  • 6,739
  • 4
  • 30
  • 54
Ryyan
  • 11
  • 1