izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Getting all the Channels (including Direct Message Channels) the Client is connected to #316

Closed suuf closed 5 years ago

suuf commented 5 years ago

Hi everyone

I'm trying to get all the Channels (including Direct Message Channels) the Client is connected to, through an on ready function.

For some reason the Direct Message Channels are not included in the channels property, so I tried to access the directMessages property, but it's not returning anything.

How can I get the Direct Message Channel IDs from the Client itself? (Do let me know if something in my logic is wrong :P)

cloudrac3r commented 5 years ago

Your logic is wrong. :P

Direct message channels are created as needed (if a message is sent or received there) but they are lost when the client is restarted. The persistent DM channel list that you as a user see in Discord is not a concept that exists for bots.

If you send a message to the bot via DM, and then check client.directMessages, you'll see the channel that it received a message in.

If you want to get the ID of a DMChannel for a specific user, for example to get messages from it, you can use client.createDMChannel(userID, callback). Once you have the channel ID, you can get messages from it.

If you just want to DM somebody, you can put their userID in the "to:" field of .sendMessage, and a DMChannel will be created automatically.

suuf commented 5 years ago

Nice, thank you 🙂