Search code examples
discord.js

How can I DM the user an embed before he is kicked (discord.js)


I'm trying to make a blacklist command, and how do I make it so it sends an embed instead of normal message.
(Also, I'm new to discord.js)

Here's my script:


module.exports = {
  data: new SlashCommandBuilder()
    .setName('blacklist')
    .setDescription('Blacklist someone from the server')
    .addUserOption((option) => option.setName('member')
      .setDescription('Who are you blacklisting?')),
  async execute(interaction) {

    await interaction.deferReply();


    const member = interaction.options.getMember('member');


    if (!interaction.member.roles.cache.some(r => r.name === "Blacklist perms")) {
      return interaction.editReply({ content: 'You do not have permission to use this command!' });
    }

        member.send("You have been blacklisted!")
        member.kick().catch(err => {
      interaction.editReply({ content: `I do not have enough permissions to do that.`})
    })

    await interaction.editReply({ content: `${member} has been succesfully blacklisted!` });
  },
};```

Solution

  • Since your function is already async, simply await sending the message before you call kick.

    await member.send("You have been blacklisted!")
    member.kick().catch(err => {
        interaction.editReply({ content: 'I do not have enough permissions to do that.'})
    })
    

    You might also wanna catch() on the send method because users can have DMs from server members disabled or blocked your bot.

    You also might consider banning the user instead, because this system would fail if your bot ever happens to go offline when a blacklisted user tries to join. Additionally, depending on how fast your bot reacts, the user may still have time to spam in channels or do something harmful before your bot is able to kick them.

    To send an embed either use an EmbedBuilder or a plain JavaScript object with the correct embed structure.

    Example:

    member.send({ embeds:[{
        title: "Title",
        description: "Hello World!"
    }] })