向 discord.js 中的命令 arg 指定的用户发送 DM

send DM to user specified by command arg in discord.js

我正在为我的服务器制作一个机器人并希望它向用户发送 DM,我知道 message.author.send("Your message here.") 但我想要的是通过命令完成它。例如。 !dm {user} {message}。我该怎么做?

一旦你解析了你的参数,你就可以在你的命令中使用这个代码(确保它在一个异步函数中)。

let mention = args[1].match(/^<@!?(\d+)>$/)[1];
if (!mention) return message.channel.send('Invalid user.');

let recipient = await client.fetchUser(mention);
recipient.send(args.slice(2));