Home > Software engineering >  How to customize an embed using args in discord.js
How to customize an embed using args in discord.js

Time:06-21

I am trying to make a command to send an embed but I can't figure out how to customize an embed using arguments

const { MessageEmbed } = require("discord.js")

module.exports = {
    category: 'Role Select Msg',
    description: 'Sends a embeded message for roles or for announcemnts',

    permissions: ['ADMINISTRATOR'],

    slash: 'both',
    guildOnly: true,

    minArgs: 2,
    expectedArgs: '<channel> <text>',
    expectedArgsTypes: ['CHANNEL', 'STRING'],

    callback: ({ message, interaction, args }) => {
        const channel = message ? message.mentions.channels.first() : interaction.options.getChannel('channel')
        const description = interaction.options.getString('text')
        if (!channel || channel.type !== 'GUILD_TEXT'){
            return 'Please tag a text channel.'
        }

        const embed = new MessageEmbed()
            .setDescription(description)

        channel.send(embed)

        if(interaction){
            interaction.reply({
                content: 'Sent message!',
                ephemeral: true
            })
        }
    }
}

I want to customize something like a title or description based on what someone that used the command input for .

CodePudding user response:

for a normal message command you can use args or split and collector for example:

// Defining the channel you want to send the embed to
const channel = await message.mentions.channels.first() || message.guild.channels.cache.get(args[0]) || message.channel;

// Defining the first embed
const embed = new MessageEmbed()
    .setDescription(`Please type your title and description separated by a \` \`, __example:__ Title Description`)
const msg = await message.channel.send({embeds: [embed]})

// Using message collector to collect the embed's title and description
const filter = m => m.author.id === message.author.id //Filter the message of only the user who run the command
                
message.channel.awaitMessages({
     filter,
     max: 1,
     time: 120000,
     errors: ["time"]
   }).then(async collected => {
           const msgContent = collected.first().content
           const embedFields = msgContent.split(' '); // You can use any symbol to split with not only  
           const customEmbed = new MessageEmbed()
               .setTitle(`${embedFields[0]}`)
               .setDescription(`${embedFields[1]}`)
               .setFooter({text: `${embedFields[2]}`})
           channel.send({embeds: [customEmbed]}) // send the embed to the channel
           message.reply({content: `Your embed was sent to ${channel}`})
}) // reply to the main command

If it was an interaction command, You can use Options:

// Defining the options
const channel = interaction.options.getChannel('channel')
const title = interaction.options.getString('title') // Title of the embed
const descr = interaction.options.getString('description') // Description for the embed
// You can add also add a custom footer:
const footer = interaction.options.getString('footer')

//Now it's simpler than using a normal message command
const customEmbed = new MessageEmbed()
    .setTitle(`${title}`)
    .setDescription(`${descr}`)
    .setFooter({text: `${footer}`})
channel.send({embeds: [customEmbed]})
interaction.reply({content: `Your embed was sent to ${channel}`, ephemeral: true}) // reply to your interaction
// Of course you need to add the options to the interaction, and check it required option or not

Example of the output: https://i.imgur.com/T1QAqg1.png

CodePudding user response:

After doing some trial and error, I've found one of some solutions for your problem.

if(message.author.id == "") {
        const embed = new MessageEmbed()
        .setDescription("To create a customized embed, you need to do an `args[0]` and `args[1]`")
        .setColor('RANDOM')
        .setTimestamp()

        message.channel.send({embeds: [embed]})

        const msg_filter = (m) => m.author.id === message.author.id;
        const collector = await message.channel.createMessageCollector({filter: msg_filter, max: 2, timer: 15000})

        collector.on('end', collected => {
            let i = 0;
            collected.forEach((value) => {
                i  ;
                if (i === 1) embed.setTitle(value.content);
                else if (i === 2) embed.setDescription(value.content);
            })
            console.log(i)
            message.channel.send({embeds: [embed]});
        })
    } else {
        return message.reply("```Only the Developer Can use these command.```")
    }

To achieve these kind of command, You need to use a collector function See more here

Using max: 2 makes it to wait for two args which is the arsg[0] and args[1], Then use any so you can make it to equal for .setTitle and .setDescription

  • Related