Home > Software engineering >  discord.js \n doesn't break line in embed description
discord.js \n doesn't break line in embed description

Time:08-20

const channel = options.getChannel("channel");
const title = options.getString("title");
const msg = options.getString("description");

const ancEmbed = new MessageEmbed()
  .setColor('DARK_VIVID_PINK')
  .setTitle(title)
  .setDescription(msg)
       
channel.send({embeds: [ancEmbed]});

What might be wrong with this? \n don't get replaced into new lines.. Discord Embed Image

CodePudding user response:

It's because you're setting the description to the content of the user's command, and if the user inputs a \n, Discord automatically escapes it so that your interaction receives it as the text \n, not the newline \n. Here's a replace function using a regex that replaces all the text \n with an actual newline \n character of the content of the message:

var msg = options.getString("description");
msg = msg.replace(/\\n/g, "\n");
  • Related