如何使用命令向用户发送dm

时间:2019-10-04 20:21:10

标签: bots discord discord.js

我正在编写一个脚本以向某个用户dm发送脚本,例如我将写的一条消息。

!dm @user Hi!

然后,该漫游器会向该用户发送dm,只说“嗨!

它不允许我,有人可以检查我的代码吗?

if(command === "dm") {
      let member = message.mentions.members.first() || message.guild.members.get(args[0]);
      if(!member)
          return message.reply("Please mention a valid member of this server");
      let reason = args.slice(1).join(' ');
      mentionedUser.send(`${reason}`);

}

更新的代码:

  if(command === "dm") {
  let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  if(!member)
      return message.reply("Please mention a valid member of this server");

  let reason = args.slice(1).join(' ');
    if(!reason)
        return message.reply("Please write a message.");
  member.dmChannel.send(`${reason}`);
  message.reply(`${member.user.tag} received a message( ${reason} ) sent by:${message.author.tag}`);

  }

现在我收到此错误:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined

2 个答案:

答案 0 :(得分:0)

您可以通过先获取用户的DM来对用户进行DM

let member = message.mentions.members.first() || message.guild.members.get(args[0]);
//...
member.dmChannel.send("")

或者,您可以改用createDM()

答案 1 :(得分:0)

此答案与版本11及更高版本有关。


无需与用户访问客户端的DM通道(无论如何,都必须首先实例化该通道)。只需使用User.send()GuildMember.send()方法。

member.send(reason)
  .catch(console.error);