在 discord.js 上禁止 DM 消息 [英] Ban DM Message on discord.js

查看:14
本文介绍了在 discord.js 上禁止 DM 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个不和谐的机器人,我希望该机器人向被禁止的用户发送 DM 原因它被禁止了.这是我当前的代码:

I am making a discord bot, and I want the bot to DM the user that got banned why it got banned. Here is my current code for it:

    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.')
  }

推荐答案

您无法将消息发送给用户,因为您禁止了他们,这意味着您不再共享服务器.
先发送消息.
.send 是一个承诺.充分利用它.

You cant send the message to the user because you banned them, meaning you no longer share a server.
Send the message first.
.send is a promise. use it to your advantage.

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 来禁止用户,这样即使消息没有发送,他们仍然会被禁止

use .then and a .catch to ban the user so even if the message doesnt send they still get banned

这篇关于在 discord.js 上禁止 DM 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆