BasZ
BasZ

Reputation: 37

Use user's args to create an embed

I'm trying to create a command that passes the args given by the message author in an embed.

This is the code

        const args = message.content.split(", ");

        var titleargs = args[1]
        var descriptionargs = args[2]
        var footerargs = args[3]
        {
          {
              var myInfo = new Discord.MessageEmbed()
                  .setTitle(titleargs)
                  .setDescription(descriptionargs)
                  .setFooter(footerargs)
                  .setColor(0xff0000)
  
  
                  message.channel.send(myInfo);
  
          }
      }

This code works, but I don't want to put the first ", " after the prefix and the command

What should I change?

EDIT: I'm using command handling

Upvotes: 1

Views: 47

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23189

You can remove the prefix and the command from the string and then split by ,. Check the following snippet:

const prefix = '&'
const command = 'announce'
const message = {
  content: '&announce a 1, b 2, c 3'
}

const args = message.content
  // remove the prefix and the command
  .slice(prefix.length + command.length)
  .split(',')
  // remove extra whitespaces
  .map(s => s.trim())

console.log(args)

You could also destructure the title, description, and footer args:

const args = message.content
  .slice(prefix.length + command.length)
  .split(',')
  .map((s) => s.trim())

const [title, description, footer] = args

const myInfo = new Discord.MessageEmbed()
  .setTitle(title)
  .setDescription(description)
  .setFooter(footer)
  .setColor(0xff0000)

message.channel.send(myInfo)

enter image description here

Upvotes: 2

Related Questions