0

trying to make a command like this /dm and the message will be sent as an embed

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
  const data = new SlashCommandBuilder()
    .setName('dm')
    .setDescription('Make me DM a user!')
  .setRequired(true)
    .addUserOption(option =>
        option.setName('user')
            .setDescription('The user you want to DM!')
      .setRequired(true));
  .addStringOption(option =>
    option.setName('message')
        .setDescription('The Message you want to send!')
        .setRequired(true));

async execute(interaction) {
  const {channel, client, options} = interaction;

  const user = options.getMember('user')
  const message = options.getString('message')


  if(user.id === '856920496314646579') {
    return await interaction.reply({
      content: "**I can't DM myself**"
    })
      .catch((err) => {})
  }

  user.send(message).catch(async (err) => {
    console.log(err)

    return await interaction.editReply({
      content: `failed to send message, please try again!`
    })
      .catch((err) => {})
  })


  await interaction.reply({
    content: `**${message}** Successfully sent to **${user}**! (this message will autodelete)`
  })
    .catch((err) => {})

  await setTimeout(() => { interaction.deleteReply()}, 3500);
  },
};

I'm expecting to send an embed message with slash commands but idk if got the code wrong or what can someone please just rewrite the code for me?

1 Answers1

0

Here is a rewrite for the above code:

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    const data = new SlashCommandBuilder()
        .setName('dm')
        .setDescription('Make me DM a user!')
        .setRequired(true)
        .addUserOption(option =>
            option.setName('user')
                .setDescription('The user you want to DM!')
                .setRequired(true));
  .addStringOption(option =>
                    option.setName('message')
                        .setDescription('The Message you want to send!')
                        .setRequired(true));

    async execute(interaction) {
        const { channel, client, options } = interaction;

        const user = options.getMember('user')
        const message = options.getString('message')


        if (user.id === '856920496314646579') {
            return await interaction.reply({
                content: "**I can't DM myself**"
            })
                .catch((err) => { })
        }
        let embed = new EmbedBuilder()
            .setDescription(`${message}`)
        user.send({ embeds: [embed] }).catch(async (err) => {
            console.log(err)

            return await interaction.editReply({
                content: `failed to send message, please try again!`
            })
                .catch((err) => { })
        })


        await interaction.reply({
            content: `** ${message}** Successfully sent to ** ${user}** !(this message will autodelete)`
        })
            .catch((err) => { })

        await setTimeout(() => { interaction.deleteReply() }, 3500);
    },
};

Hope it might help you. You can also refer to

Edit: So let me clear what I changed:

  1. First I imported EmbedBuilder from discord.js
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
  1. Then, I created an embed which contains message as description
let embed = new EmbedBuilder()
            .setDescription(`${message}`)
  1. After that I sent that embed to user's dm
user.send({ embeds: [embed] })

Here you can send more than 1 embed too at a time

<userObject>.send({ embeds: [embed, embed2, andso] })
Ansh Tyagi
  • 116
  • 1
  • 15