Expand description
§djs-formatters
Markdown, mention and timestamp formatting helpers for Discord messages.
Status: alpha. Every formatter and escaper of
@discordjs/formatters. The API may still change between alpha releases; see the changelog.
discord.js counterpart: @discordjs/formatters
§How the formatters work
Each formatter returns a small value that implements Display, so
nothing is allocated until it is written: put it in format! or write!, or call
.to_string(). Formatters take any Display content, so they nest (bold(italic("x")) is
**_x_**), and mentions take the typed IDs of djs-model.
Optional parts are methods on the returned value.
use djs_formatters::{TimestampStyle, bold, code_block, slash_command_mention, time, user_mention};
use djs_model::id::{CommandId, UserId};
let winner = UserId::new(80_351_110_224_678_912);
let text = format!(
"{} {} won {}!",
bold("Giveaway:"),
user_mention(winner),
time(1_618_953_630).style(TimestampStyle::RelativeTime),
);
assert_eq!(text, "**Giveaway:** <@80351110224678912> won <t:1618953630:R>!");
let help = slash_command_mention("tag", CommandId::new(1)).subcommand("show");
assert_eq!(help.to_string(), "</tag show:1>");
assert_eq!(code_block("fn main() {}").language("rust").to_string(), "```rust\nfn main() {}\n```");Formatters do not escape their content. Text from users goes through escape_markdown
first, which returns a Cow<str> and borrows the text when there is nothing to escape:
use djs_formatters::{EscapeOptions, bold, escape_markdown};
let name = "**not bold**";
assert_eq!(bold(escape_markdown(name)).to_string(), r"**\*\*not bold\*\***");
// Links and mentions are left alone, so they keep working.
assert_eq!(escape_markdown("https://x.dev/a_b <@1>"), "https://x.dev/a_b <@1>");
// discord.js' options, with its defaults.
let strict = EscapeOptions::new().heading(true).bulleted_list(true);
assert_eq!(strict.escape("# Title\n- item"), "\\# Title\n\\- item");§Formatters
| Formatter | Output | discord.js |
|---|---|---|
user_mention, channel_mention, role_mention | <@id>, <#id>, <@&id> | userMention, channelMention, roleMention |
linked_role_mention, GuildNavigation | <id:linked-roles:id>, <id:browse>, … | linkedRoleMention, GuildNavigationMentions |
slash_command_mention .subcommand(…) / .group(…, …) | </name sub:id> | chatInputApplicationCommandMention |
emoji .name(…) .animated(…) | <a:name:id> | formatEmoji |
time .style(…), TimestampStyle | <t:seconds:R> | time, TimestampStyles |
bold, italic, underline, strikethrough, spoiler | **x**, _x_, __x__, ~~x~~, ||x|| | the same names |
inline_code, code_block .language(…) | `x`, ```lang | inlineCode, codeBlock |
quote, block_quote, subtext, heading .level(…) | > x, >>> x, -# x, ## x | the same names |
unordered_list, ordered_list .start(…), ListItem | - x, 1. x, nested | unorderedList, orderedList |
hyperlink .title(…), hide_link_embed | [x](url "title"), <url> | hyperlink, hideLinkEmbed |
channel_link, message_link .guild(…) | https://discord.com/channels/… | channelLink, messageLink |
application_directory .sku(…) | https://discord.com/application-directory/… | applicationDirectory |
email .headers(…), phone_number | <a@b.c?subject=…>, <+1…> | email, phoneNumber |
Face | ¯\_(ツ)_/¯, … | Faces |
escape_markdown, EscapeOptions, escape_bold, escape_italic, … | escapeMarkdown, escapeBold, … |
§Escaping rules
The same options and defaults as discord.js, and the same result for ordinary text. Links
(http://, https://) and <…> tokens (mentions, emoji, timestamps, e-mail and phone
links) are copied as they are, so escaping never breaks them. In unusual input the rules
differ on purpose; see EscapeOptions for the full list.
§Part of djs-rs
djs-formatters is one crate of djs-rs, a high-performance
Rust library for building Discord bots modelled on discord.js v14. It can be used on its own
or through the main djs-rs crate, as djs_rs::formatters.
§License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Structs§
- Application
Directory - A link to an app’s store page in the App Directory, or to one of its SKUs. Returned by
application_directory. - Block
Quote - Content written as
>>> …. Returned byblock_quote. - Bold
- Content written as
**…**. Returned bybold. - Channel
Link - A link to a channel:
https://discord.com/channels/GUILD_ID/CHANNEL_ID, or@mein place of the server for a DM. Returned bychannel_link. - Channel
Mention - Mentions a channel:
<#CHANNEL_ID>. Returned bychannel_mention. - Code
Block - A code block:
```language\ncontent\n```. Returned bycode_block. - An e-mail link:
<address>or<address?header=value&…>. Returned byemail. - Emoji
- Shows a custom emoji:
<:NAME:ID>or, animated,<a:NAME:ID>. Returned byemoji. - Escape
Options - Which kinds of markdown
escapeneutralizes. - Heading
- A heading:
# content,## contentor### content. Returned byheading. - Hide
Link Embed - Content written as
<…>. Returned byhide_link_embed. - Hyperlink
- A masked link:
[content](url)or[content](url "title"). Returned byhyperlink. - Inline
Code - Content written as
…. Returned byinline_code. - Italic
- Content written as
_…_. Returned byitalic. - Linked
Role Mention - Links to a linked role:
<id:linked-roles:ROLE_ID>. Returned bylinked_role_mention. - Message
Link - A link to a message:
https://discord.com/channels/GUILD_ID/CHANNEL_ID/MESSAGE_ID. Returned bymessage_link. - Ordered
List - A numbered list:
1. item,2. item, … Returned byordered_list. - Phone
Number - A phone link:
<+number>. Returned byphone_number. - Quote
- Content written as
> …. Returned byquote. - Role
Mention - Mentions a role:
<@&ROLE_ID>. Returned byrole_mention. - Slash
Command Mention - Mentions a slash command, a clickable link that fills in the command:
</NAME:ID>,</NAME SUBCOMMAND:ID>or</NAME GROUP SUBCOMMAND:ID>. Returned byslash_command_mention. - Spoiler
- Content written as
||…||. Returned byspoiler. - Strikethrough
- Content written as
~~…~~. Returned bystrikethrough. - Subtext
- Content written as
-# …. Returned bysubtext. - Time
- A timestamp:
<t:SECONDS>or<t:SECONDS:STYLE>. Returned bytime. - Underline
- Content written as
__…__. Returned byunderline. - Unordered
List - A bulleted list: one
- itemper line. Returned byunordered_list. - User
Mention - Mentions a user:
<@USER_ID>. Returned byuser_mention.
Enums§
- Face
- A text face Discord offers as a slash command, ready to send.
- Guild
Navigation - A guild navigation link, opening a tab of the current server.
- Heading
Level - The size of a
heading. - List
Item - An item of a list: a line of text, or a nested list indented under the line before it.
- Timestamp
Style - How Discord shows a
time; the examples are for 20 April 2021, 16:20:30, in an English client.
Traits§
- List
Entry - What a list can hold: any
Displayvalue, or aListItem. - Unix
Seconds - A point in time that
timecan show: seconds since the Unix epoch.
Functions§
- application_
directory - Links to the App Directory store page of the app
application(https://discord.com/application-directory/ID/store), or of one of its SKUs withsku(D-213). Discord shows the link as a card. - block_
quote - Quotes
contentand everything after it in the message:>>> content. - bold
- Shows
contentin bold:**content**. - channel_
link - Links to the channel
channel, a DM unless theguildis set (D-213). - channel_
mention - Mentions the channel
idas a clickable link:<#103735883630395392>. - code_
block - Shows
contentas a block of code, highlighted for alanguagesuch asrustif set (D-213). - Links to the e-mail address
address; clicking it opens a new e-mail, with theheaders(such assubject) filled in if set (D-213). - emoji
- Shows the custom emoji
id:<:emoji:216154654256398347>. - escape_
bold - Escapes only
**intext(EscapeOptions::bold). - escape_
bulleted_ list - Escapes only bullets at the start of a line in
text(EscapeOptions::bulleted_list). - escape_
code_ block - Escapes only runs of three or more backticks in
text(EscapeOptions::code_block). - escape_
escape - Escapes only backslashes in
text(EscapeOptions::backslash). - escape_
heading - Escapes only headings at the start of a line in
text(EscapeOptions::heading). - escape_
inline_ code - Escapes only runs of one or two backticks in
text(EscapeOptions::inline_code). - escape_
italic - Escapes only a single
*or_intext(EscapeOptions::italic). - escape_
markdown - Escapes
textwith discord.js’ defaults (EscapeOptions::new): emphasis, code, strikethrough, spoilers and backslashes, outside links and mentions. - escape_
masked_ link - Escapes only masked links in
text(EscapeOptions::masked_link). - escape_
numbered_ list - Escapes only numbered list items at the start of a line in
text(EscapeOptions::numbered_list). - escape_
spoiler - Escapes only
||intext(EscapeOptions::spoiler). - escape_
strikethrough - Escapes only
~~intext(EscapeOptions::strikethrough). - escape_
underline - Escapes only
__intext(EscapeOptions::underline). - heading
- Shows
contentas a heading, the largest unless a smallerlevelis set (D-213). - hide_
link_ embed - Links to
urlwithout an embed preview:<url>. - hyperlink
- Shows
contentas a link tourl, with atitleshown on hover if set (D-213). - inline_
code - Shows
contentas code on one line:`content`. - italic
- Shows
contentin italics:_content_. - linked_
role_ mention - Links to the linked role
id; clicking it opens the connection dialog:<id:linked-roles:165511591545143296>. - message_
link - Links to the message
messageof the channelchannel, a DM unless theguildis set (D-213). - ordered_
list - Shows
itemsas a numbered list from 1, or fromstart(D-213); nested lists count from 1. - phone_
number - Links to the phone number
number, which must be international (start with+);Noneotherwise (discord.js throws an error). - quote
- Quotes one line:
> content. - role_
mention - Mentions the role
id:<@&165511591545143296>. - slash_
command_ mention - Mentions the slash command
namewith the IDid:</airhorn:816437322781949972>. - spoiler
- Hides
contentuntil it is clicked:||content||. - strikethrough
- Strikes
contentthrough:~~content~~. - subtext
- Shows
contentsmall and grey, as a line of subtext:-# content. - time
- Shows
atin each reader’s time zone and language:<t:1618953630>. - underline
- Underlines
content:__content__. - unordered_
list - Shows
itemsas a bulleted list, one per line;ListItem::Nestedindents a list under the line before it (two spaces per level). - user_
mention - Mentions the user
id:<@80351110224678912>.