0

Can someone please tell me how I can send a user a dm before he gets banned? Everything works fine, but the dm. It seems, that if i put the dm code where it is now, the user gets banned first, then gets a direct message which obviously does not work because the bot and the user do not share a server. My Code:

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)
    }
        
}
  }}
hubschrauber
  • 55
  • 10

1 Answers1

1

Try using .then(), to make it so that the ban only occurs after the message.channel.send() event has finished.

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);
                }
            });
        }
    }
}
szyven
  • 26
  • 7