简体   繁体   中英

How do I send a DM to a user in discord.js v13

All I'm doing right now is trying to send a dm to myself to see if I can get this working. I've tried:

 client.users.cache.get(id).send('hi')
But I'm getting "TypeError: Cannot read properties of undefined (reading 'send')." I suppose this has something to do with myself not being cached, but I'm unsure how to go about caching myself. Does anyone know how to properly do this?

Use UserManager#send() . This will create a dm if needed, then send the message

client.users.send(userId, "content")

Please take look at this post , it could resolve your problem.

Basically what it seems to happen is that the get method isn't consistent like fetch. So prefer to use the fetch method, it will look something like this:

const user = await client.users.cache.fetch(id)
user.send('hi')

if you're not in a asynchronous scope you could use the "then" approach:

client.users.cache.fetch(id)
  .then((user) => user.send('hi'))
  .catch(e => console.log(e))

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