George
George

Reputation: 5

How can I add a variable in a code block in a Discord message

How can I write a code block (using the discord.js library) in a Discord message like this:

Message image

I am trying with the variable num, but it still gets recognized as text and doesn't get replaced with the actual value

.addField("```change ${num}```");

Upvotes: 0

Views: 4241

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6806

The image you put is not an embed, it's just a message where someone has been mentioned, and so it's yellow.

To make a code block in Discord you have to type a normal message like this:

```
your code text here
```

Discord will then use markdown to display it as a code block. For further help on Discord markdown, please see this article.

If you're trying to use that num variable inside a string, you should start and end that string with backticks (`). Please note that if you want to put other backticks in the string you have to escape them with a backward slash (\).
Here's an example:

`\`\`\`\nYour text: ${num}\n\`\`\``

You can always append the variable with a plus + operator:

"```\nYour text: " + num + "\n```"

Upvotes: 4

Related Questions