【问题标题】:How to dm user before executing a ban in discord.js?如何在 discord.js 中执行禁令之前 dm 用户?
【发布时间】:2021-06-12 23:59:32
【问题描述】:

有人可以告诉我如何在用户被禁止之前向他发送 dm 吗?一切正常,但 dm.看来,如果我将 dm 代码放在现在的位置,用户首先会被禁止,然后会收到一条直接消息,这显然不起作用,因为机器人和用户不共享服务器。 我的代码:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
  name: 'ban',
  description: "Allows you to ban a member from the server!",
  execute(message, args) {
    const member = message.mentions.users.first();
    const notBanYourself = new Discord.MessageEmbed()
    .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("You cannot ban yourself!")
        .setFooter(message.author.username)
        .setTimestamp()
        ;

    if(member.id === message.author.id) return message.channel.send(notBanYourself);
    if(message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")){
        const memberTarget = message.guild.members.cache.get(member.id);
        const ban_dm = new Discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        memberTarget.send(ban_dm)
    if(member) {
        memberTarget.ban({ days: 3, reason:  args.slice(1).join(" ") });
        const banned = new Discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("User has been banned from the server successfully.")
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        message.channel.send(banned)
    }
        

    else {
        const notBanned = new Discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("There was an error: You need to mention the user, which you want to ban! \n If you did that, you probably do not have the permission to ban users.")
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        message.channel.send(notBanned)
    }
        
}
  }}

【问题讨论】:

    标签: discord discord.js bots dm


    【解决方案1】:

    尝试使用.then(),以使禁令仅在message.channel.send() 事件完成后发生。

    const { DiscordAPIError } = require("discord.js");
    const Discord = require('discord.js');
    const client = new Discord.Client();
    
    module.exports = {
        name: 'ban',
        description: "Allows you to ban a member from the server!",
        execute(message, args) {
    
            const member = message.mentions.users.first();
            const notBanYourself = new Discord.MessageEmbed()
                .setColor("#ff0000")
                .setTitle("Ban")
                .setDescription("You cannot ban yourself!")
                .setFooter(message.author.username)
                .setTimestamp()
    
            if (member.id === message.author.id) return message.channel.send(notBanYourself);
            if (message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")) {
    
                const memberTarget = message.guild.members.cache.get(member.id);
                const ban_dm = new Discord.MessageEmbed()
                    .setColor("#ff0000")
                    .setTitle("Ban")
                    .setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
                    .setFooter(message.author.username)
                    .setTimestamp()
    
                memberTarget.send(ban_dm).then(() => {
                    if (member) {
    
                        memberTarget.ban({ days: 3, reason: args.slice(1).join(" ") });
                        const banned = new Discord.MessageEmbed()
                            .setColor("#ff0000")
                            .setTitle("Ban")
                            .setDescription("User has been banned from the server successfully.")
                            .setFooter(message.author.username)
                            .setTimestamp()
                        message.channel.send(banned);
                    } else {
                        const notBanned = new Discord.MessageEmbed()
                            .setColor("#ff0000")
                            .setTitle("Ban")
                            .setDescription("There was an error: You need to mention the user, which you want to ban! \n If you did that, you probably do not have the permission to ban users.")
                            .setFooter(message.author.username)
                            .setTimestamp()
                        message.channel.send(notBanned);
                    }
                });
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 2020-09-27
      • 1970-01-01
      • 2018-12-02
      • 2021-10-11
      • 2021-10-17
      • 2021-09-05
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多