![](/img/trans.png)
[英]How do I make a discord bot send a DM to someone mentioned in the command, using java script?
[英]Discord bot - send DM to mentioned user
我想让机器人向我提到的用户发送消息,例如我将输入 !hello @thomas#5555
机器人将向该用户发送 DM 消息“你好”
if(message.content.startsWith(prefix + "sendHello")){
const taggedUser = message.mentions.users.first();
const user = client.users.cache.get(taggedUser.id);
user.send('Hello');
}
怎么了? 不断得到这个:“ReferenceError:客户端未定义”
我在代码顶部定义了客户端,如下所示:“const Client = new Disocrd.Client();”
由于您将客户端定义为Client
,因此您需要使用它而不是client
。 您还可以使用message.client
从消息对象访问它。
首先确保您正确初始化您的客户端。
尝试:
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
使用 discord.js v13,您需要添加您希望客户端接收的意图。 Intents.FLAGS.GUILDS
意图选项是您的客户端正常工作所必需的。 你可以在这里看到所有的意图。
同样对于您的命令代码,如果有人标记了用户,您不需要再次获取用户。 只需使用taggedUser
变量。
const taggedUser = message.mentions.users.first(); // get User
if (!taggedUser) return message.channel.send('Please mention a user to fetch.'); // Check to see if a User was tagged.
taggedUser.send('Hi') // send to User
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.