在discord.js上禁止DM消息

时间:2018-06-23 15:44:38

标签: discord discord.js

我正在制造一个不和谐的机器人,我希望该机器人向被禁止的用户DM 它被禁止了。这是我目前的代码:

    if(command === "ban") {
  if(!message.member.roles.some(r=>["Administrator"].includes(r.name)) )
return message.reply("Sorry, you don't have permissions to use this!");

let member = message.mentions.members.first();
if(!member)
  return message.reply("Please mention a valid member of this server");
if(!member.bannable) 
  return message.reply("I cannot ban this user! Do they have a higher role? 
Do I have ban permissions?");

let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";

await member.ban(reason)
  .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
  message.member.tag.sendMessage('Hi! You were banned from ${member.user.server} because: {reason}. If you do not get why you were banned, please DM @doodthedoodthedood#2695.')
  }

2 个答案:

答案 0 :(得分:1)

在被禁止之前,您必须这样做。

在禁止用户之前,使用成员对象向他们发送消息。

await member.send("You are banned")
await member.ban(reason)

答案 1 :(得分:1)

您无法将邮件发送给用户,因为您已禁止他们,这意味着您不再共享服务器。
先发送消息。
.send是一个承诺。利用它来发挥自己的优势。

message.member.send(`ban reason here`).then(function(){
    message.member.ban(`reason`)
    console.log(`Successfully sent ban message to ${message.member.tag}`);
}).catch(function(){
   message.member.ban(`reason`)
   console.log(`Unsuccessfully sent ban message to ${message.member.tag}`);
});

使用.then.catch禁止用户,因此即使邮件未发送,他们仍然会被禁止