通过会员的用户名发送DM

时间:2020-06-20 16:44:43

标签: javascript discord.js

我不小心做了一次,我知道有可能,但是我无法复制以前的工作。

我想知道如何通过用户名向用户发送DM。 例如,查询!msg ThisUser“这是一条消息” 会DM它们“这是一条消息”

目标不是提及用户,而是手动向他们发送消息

我的尝试

const memberList = message.guild.members.cache.filter(user => !user.user.bot).array().map(user => user.user.username)

memberList[14].send('Hello')

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

if(message.content.startsWith('!msg'){
   const args = message.content.slice(4).split(/ +/);
   const username = args[0];
   const content = args[1];
   let member = message.guild.members.cache.find(member => member.username === username);
   member.user.send(content);
}

答案 1 :(得分:0)

我找到了答案,这是给其他人的

// Get the user objects of every member
const memberList = message.guild.members.cache.filter(user => !user.user.bot).array().map(user => user.user)

// Have a variable represent whether the user was found or not
const targetFound = memberList.find(member => member.username == args[1])

// Declare the message to send (args is predefined)
const messageToSend = args.splice(2).join(' ')

// If the username was found, send the message to them
if (targetFound) targetFound.send(messageToSend)