Section titled FormattersFormatters
discord.js provides the @discordjs/formatters package which contains a variety of utilities you can use when writing your Discord bot.
Section titled Basic MarkdownBasic Markdown
These functions format strings into all the different markdown styles supported by Discord.
_10import { bold, italic, strikethrough, underscore, spoiler, quote, blockQuote } from 'discord.js';_10_10const string = 'Hello!';_10const boldString = bold(string);_10const italicString = italic(string);_10const strikethroughString = strikethrough(string);_10const underscoreString = underscore(string);_10const spoilerString = spoiler(string);_10const quoteString = quote(string);_10const blockquoteString = blockQuote(string);
Section titled LinksLinks
There are also two functions to format hyperlinks. hyperlink()
will format the URL into a masked markdown link, and hideLinkEmbed()
will wrap the URL in <>
, preventing it from embedding.
_10import { hyperlink, hideLinkEmbed } from 'discord.js';_10_10const url = 'https://discord.js.org/';_10const link = hyperlink('discord.js', url);_10const hiddenEmbed = hideLinkEmbed(url);
Section titled Code blocksCode blocks
You can use inlineCode()
and codeBlock()
to turn a string into an inline code block or a regular code block with or without syntax highlighting.
_10import { inlineCode, codeBlock } from 'discord.js';_10_10const jsString = 'const value = true;';_10const inline = inlineCode(jsString);_10const codeblock = codeBlock(jsString);_10const highlighted = codeBlock('js', jsString);
Section titled TimestampsTimestamps
With time()
, you can format Unix timestamps and dates into a Discord time string.
_10import { time, TimestampStyles } from 'discord.js';_10_10const date = new Date();_10const timeString = time(date);_10const relative = time(date, TimestampStyles.RelativeTime);
Section titled MentionsMentions
userMention()
, channelMention()
, and roleMention()
all exist to format Snowflakes into mentions.
_10import { channelMention, roleMention, userMention } from 'discord.js';_10_10const id = '123456789012345678';_10const channel = channelMention(id);_10const role = roleMention(id);_10const user = userMention(id);