简体   繁体   中英

Is there any way to get a dmChannel id just from the user object? (discord.js v11)

If I'm trying to figure out how to get the id of a dmChannel, with only the user's object so I can bulk delete in user's dms, how would I do this? The code I have so far is:

let dm = client.users.get('123481923481234').createDM()

the error I got was:

TypeError: Cannot read property 'createDM' of undefined

I also tried getting the user from a guild members list, here's the code:

        let dms = client.guilds.get('783871449466142750').then(guild=>{
          guild.members.get('432325763753050122').createDM().channel.id
        })

This got the error:

TypeError: client.guilds.get(...).then is not a function

Anyone know whats wrong? Any help would be appreciated!

Did you try looking at the discord.js docs? That's the website where discord.js documents exactly how to use every method and property available in discord.js, and you would be able to solve every issue you experienced in this question in seconds by simply looking at the documentation. The docs, being the place that tells you how to use everything in discord.js, should've been the first place you looked for answers; StackOverflow is more of a last resort situation that you come to when you are completely stuck, and this is certainly not one of those situations.

In the first issue you are having, the error tells you everything: Cannot read property 'createDM' of undefined . What does that tell us? It tells us that client.users.get('123481923481234') is undefined , or in other words, the user with that ID cannot be found in your bot's cache ( client.users represents the bot's user cache). Either you used an incorrect ID, or the bot's cache simply hasn't been updated to include that specific user. If you want to get that user even if they are not in your bot's cache, you can directly ask the Discord API for that information using client.fetchUser() . This method returns a Promise , so you'll want to use .then() to access its asynchronous data. ( .createDM() is also a Promise so you'll need a .then() to access the created DMChannel as well). Here's what it should look like:

client.fetchUser('123481923481234', true).then(user => {

    user.createDM().then(dmchannel => {

        //Now do whatever you want with the ID
        var channelID = dmchannel.id;

    });

})

As for the second thing you tried, that's just completely wrong. I don't know how you created that code, but you definitely didn't look at the docs to see if you were doing it right; it honestly looks like you just guessed without checking how it actually works. First of all client.guilds.get() does not return a Promise , so you don't use .then() on it. You should already know that, since you didn't use .then() on the .get() in the first thing you tried. Second, as previously mentioned, .createDM() does return a Promise and therefore does require a .then() which you did not use in your attempt. You ended up doing the opposite of what you needed to do in each of the two above parts of this second attempt. Third, the Promise of .createDM() provides you with a DMChannel object and not a DM object, so you're attempting to do DMChannel.channel.id when you should just be doing DMChannel.id . Fixing all of that might get this second attempt to work, but it might still not work due to the reason your first attempt did not work. If that's the case, you would just need to switch all of your uses of .get() in this second attempt to its fetching equivalent, which you can find easily on the docs. For the sake of a simple, readable answer, I am not going to use the fetching equivalents in this second answer. So here's how your second attempt should look:

var guild = client.guilds.get('783871449466142750');
guild.members.get('432325763753050122').createDM().then(dmchannel => {
    //Now do whatever you want with the ID
    var channelID = dmchannel.id;
});

Here are the links to the pages in the documentation that explain how to use the methods and objects in these two answers. Please go to these pages and look at how the methods work, and refer to the documentation in the future when you're not sure how something works, when you want to figure out how to do something, or when something you've developed in discord.js is not working properly. Most of the methods on the docs come with provided, understandable examples along with documentation of all available options and parameters. Only ask a question here if it cannot be answered by the docs.

https://discord.js.org/#/docs/main/v11/class/Client?scrollTo=fetchUser https://discord.js.org/#/docs/main/v11/class/User?scrollTo=createDM https://discord.js.org/#/docs/main/v11/class/DMChannel https://discord.js.org/#/docs/collection/master/class/Collection?scrollTo=get

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM