简体   繁体   English

如何在 discord.js v13 中向用户发送 DM

[英]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.我现在正在做的就是尝试向自己发送一个 dm,看看我是否可以让它工作。 I've tried:我试过了:

 client.users.cache.get(id).send('hi')
But I'm getting "TypeError: Cannot read properties of undefined (reading 'send')." 但我收到“TypeError:无法读取未定义的属性(读取'发送')。” 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() .使用UserManager#send() This will create a dm if needed, then send the message如果需要,这将创建一个 dm,然后发送消息

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.基本上它似乎发生的是get方法不像fetch那样一致。 So prefer to use the fetch method, it will look something like this:所以更喜欢使用 fetch 方法,它看起来像这样:

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:如果您不在异步范围内,则可以使用“then”方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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