Utilities
ReferenceOn this page
Formatters
Markdown helpers also exported on the discord table (discord.bold, discord.time, discord.underscore, …).
Formatters.userMention
Signature
Formatters.userMention(id)| Name | Type | Required | Behavior |
|---|---|---|---|
id |
string |
required | Snowflake id string. |
Formatters.channelMention
Signature
Formatters.channelMention(id)| Name | Type | Required | Behavior |
|---|---|---|---|
id |
string |
required | Snowflake id string. |
Formatters.roleMention
Signature
Formatters.roleMention(id)| Name | Type | Required | Behavior |
|---|---|---|---|
id |
string |
required | Snowflake id string. |
Formatters.slashCommandMention
Signature
Formatters.slashCommandMention(name, commandId)| Name | Type | Required | Behavior |
|---|---|---|---|
name |
any |
required | Passed through to the implementation as written. |
commandId |
string |
required | Snowflake id string. |
Formatters.chatInputApplicationCommandMention
Signature
Formatters.chatInputApplicationCommandMention(name, a, b, c)| Name | Type | Required | Behavior |
|---|---|---|---|
name |
any |
required | Passed through to the implementation as written. |
a |
any |
required | Passed through to the implementation as written. |
b |
any |
required | Passed through to the implementation as written. |
c |
any |
required | Passed through to the implementation as written. |
Formatters.channelLink
Signature
Formatters.channelLink(channelId, guildId)| Name | Type | Required | Behavior |
|---|---|---|---|
channelId |
string |
required | Snowflake id string. |
guildId |
string |
required | Snowflake id string. |
Formatters.messageLink
Signature
Formatters.messageLink(channelId, messageId, guildId)| Name | Type | Required | Behavior |
|---|---|---|---|
channelId |
string |
required | Snowflake id string. |
messageId |
string |
required | Snowflake id string. |
guildId |
string |
required | Snowflake id string. |
Formatters.applicationDirectory
Signature
Formatters.applicationDirectory(applicationId, skuId)| Name | Type | Required | Behavior |
|---|---|---|---|
applicationId |
string |
required | Snowflake id string. |
skuId |
string |
required | Snowflake id string. |
Formatters.list
Signature
Formatters.list(items, options)| Name | Type | Required | Behavior |
|---|---|---|---|
items |
number |
required | Duration in milliseconds. |
options |
table |
optional | Lua option table. Field names match the corresponding DiscordLua option type when one exists. |
Formatters.orderedList
Signature
Formatters.orderedList(items, startNumber)| Name | Type | Required | Behavior |
|---|---|---|---|
items |
number |
required | Duration in milliseconds. |
startNumber |
any |
required | Passed through to the implementation as written. |
Formatters.unorderedList
Signature
Formatters.unorderedList(items)| Name | Type | Required | Behavior |
|---|---|---|---|
items |
number |
required | Duration in milliseconds. |
Formatters.formatEmoji
Signature
Formatters.formatEmoji(name, id, animated)| Name | Type | Required | Behavior |
|---|---|---|---|
name |
any |
required | Passed through to the implementation as written. |
id |
string |
required | Snowflake id string. |
animated |
any |
required | Passed through to the implementation as written. |
Formatters.time
Signature
Formatters.time(seconds, style)| Name | Type | Required | Behavior |
|---|---|---|---|
seconds |
any |
required | Passed through to the implementation as written. |
style |
any |
optional | Passed through to the implementation as written. |
Formatters.bold
Signature
Formatters.bold(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.italic
Signature
Formatters.italic(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.underline
Signature
Formatters.underline(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.strikethrough
Signature
Formatters.strikethrough(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.spoiler
Signature
Formatters.spoiler(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.inlineCode
Signature
Formatters.inlineCode(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.codeBlock
Signature
Formatters.codeBlock(languageOrText, text)| Name | Type | Required | Behavior |
|---|---|---|---|
languageOrText |
any |
required | Passed through to the implementation as written. |
text |
any |
required | Passed through to the implementation as written. |
Formatters.quote
Signature
Formatters.quote(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.blockQuote
Signature
Formatters.blockQuote(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.hyperlink
Signature
Formatters.hyperlink(content, url)| Name | Type | Required | Behavior |
|---|---|---|---|
content |
string | table |
required | Message content string, or a send/reply payload table. |
url |
any |
required | Passed through to the implementation as written. |
Formatters.hideLinkEmbed
Signature
Formatters.hideLinkEmbed(url)| Name | Type | Required | Behavior |
|---|---|---|---|
url |
any |
required | Passed through to the implementation as written. |
Formatters.emoji
Signature
Formatters.emoji(name, id, animated)| Name | Type | Required | Behavior |
|---|---|---|---|
name |
any |
required | Passed through to the implementation as written. |
id |
string |
required | Snowflake id string. |
animated |
any |
required | Passed through to the implementation as written. |
Formatters.escapeMarkdown
Signature
Formatters.escapeMarkdown(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeCodeBlock
Signature
Formatters.escapeCodeBlock(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeInlineCode
Signature
Formatters.escapeInlineCode(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.heading
Signature
Formatters.heading(text, level)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
level |
any |
required | Passed through to the implementation as written. |
Formatters.subtext
Signature
Formatters.subtext(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeHeading
Signature
Formatters.escapeHeading(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeBulletedList
Signature
Formatters.escapeBulletedList(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeNumberedList
Signature
Formatters.escapeNumberedList(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeMaskedLink
Signature
Formatters.escapeMaskedLink(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeBold
Signature
Formatters.escapeBold(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeItalic
Signature
Formatters.escapeItalic(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeUnderline
Signature
Formatters.escapeUnderline(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeSpoiler
Signature
Formatters.escapeSpoiler(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
Formatters.escapeStrikethrough
Signature
Formatters.escapeStrikethrough(text)| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
discord.util
The same helpers are also exported on the package root: discord.parseEmoji, discord.resolveColor, discord.resolveImage, discord.cleanContent, discord.fetchRecommendedShardCount, and the other names listed below.
util.class
Signature
discord.util.class(name)Define a DiscordLua class table with callable construction and :new.
| Name | Type | Required | Behavior |
|---|---|---|---|
name |
any |
required | Passed through to the implementation as written. |
util.is_builder
Signature
discord.util.is_builder(value)Predicate that returns whether this util matches is_builder. Use it to branch before calling type-specific methods.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.encode_one
Signature
discord.util.encode_one(value)Encode a single Lua value for a REST body field.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.encode_list
Signature
discord.util.encode_list(list)Encode an array of values for a REST body field.
| Name | Type | Required | Behavior |
|---|---|---|---|
list |
any |
required | Passed through to the implementation as written. |
util.is_json_null
Signature
discord.util.is_json_null(value)Predicate that returns whether this util matches is_json_null. Use it to branch before calling type-specific methods.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.encode_message
Signature
discord.util.encode_message(contentOrOptions)Encode a message send/edit payload for REST.
| Name | Type | Required | Behavior |
|---|---|---|---|
contentOrOptions |
table |
optional | Lua option table. Field names match the corresponding DiscordLua option type when one exists. |
util.encode_poll_media
Signature
discord.util.encode_poll_media(media)Encode poll question or answer media.
| Name | Type | Required | Behavior |
|---|---|---|---|
media |
any |
required | Passed through to the implementation as written. |
util.encode_poll_answer
Signature
discord.util.encode_poll_answer(answer)Encode a single poll answer.
| Name | Type | Required | Behavior |
|---|---|---|---|
answer |
any |
required | Passed through to the implementation as written. |
util.encode_poll
Signature
discord.util.encode_poll(options)Encode a poll object for a message payload.
| Name | Type | Required | Behavior |
|---|---|---|---|
options |
table |
optional | Lua option table. Field names match the corresponding DiscordLua option type when one exists. |
util.resolveRoleColor
Signature
discord.util.resolveRoleColor(value)Resolve a role color value the same way role create/edit payloads accept it.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.resolveRoleColors
Signature
discord.util.resolveRoleColors(colors)Resolve a table of role color fields before encoding a role payload.
| Name | Type | Required | Behavior |
|---|---|---|---|
colors |
any |
required | Passed through to the implementation as written. |
util.encodeRoleOptions
Signature
discord.util.encodeRoleOptions(options)Encode role create/edit options, including colors and permissions.
| Name | Type | Required | Behavior |
|---|---|---|---|
options |
table |
optional | Lua option table. Field names match the corresponding DiscordLua option type when one exists. |
util.parse_hex
Signature
discord.util.parse_hex(text)Parse a hex color string into a numeric color value.
| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
util.resolveCode
Signature
discord.util.resolveCode(data, pattern)Extract an invite or template code from a URL or raw code string.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
pattern |
any |
required | Passed through to the implementation as written. |
util.resolveInviteCode
Signature
discord.util.resolveInviteCode(data)Extract a Discord invite code from a URL or raw code string.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
util.resolveGuildTemplateCode
Signature
discord.util.resolveGuildTemplateCode(data)Extract a guild template code from a URL or raw code string.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
util.resolveBase64
Signature
discord.util.resolveBase64(data)Encode binary or text input as a base64 string for REST image fields.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
util.resolveImage
Signature
discord.util.resolveImage(image)Turn a file path, data URI, or buffer into the image data DiscordLua sends on REST.
| Name | Type | Required | Behavior |
|---|---|---|---|
image |
any |
required | Passed through to the implementation as written. |
util.toSnakeCase
Signature
discord.util.toSnakeCase(text)Convert a camelCase identifier into snake_case for REST body keys.
| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
util.flatten
Signature
discord.util.flatten(obj, props)Flatten nested option tables before encoding a REST payload.
| Name | Type | Required | Behavior |
|---|---|---|---|
obj |
any |
required | Passed through to the implementation as written. |
props |
any |
required | Passed through to the implementation as written. |
util.parseEmoji
Signature
discord.util.parseEmoji(text)Parse an emoji mention, unicode character, or name into a table DiscordLua can send.
| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
util.resolvePartialEmoji
Signature
discord.util.resolvePartialEmoji(emoji)Normalize an emoji mention or id/name table into a partial emoji payload.
| Name | Type | Required | Behavior |
|---|---|---|---|
emoji |
any |
required | Passed through to the implementation as written. |
util.resolveGuildEmoji
Signature
discord.util.resolveGuildEmoji(guild, emoji)Resolve a guild emoji from an id, mention, or emoji object.
| Name | Type | Required | Behavior |
|---|---|---|---|
guild |
any |
required | Passed through to the implementation as written. |
emoji |
any |
required | Passed through to the implementation as written. |
util.makeError
Signature
discord.util.makeError(data)Construct a DiscordLua error object from a name and message.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
util.makePlainError
Signature
discord.util.makePlainError(err)Construct a plain error table without Discord-specific fields.
| Name | Type | Required | Behavior |
|---|---|---|---|
err |
any |
required | Passed through to the implementation as written. |
util.getSortableGroupTypes
Signature
discord.util.getSortableGroupTypes(channelType)Read the SortableGroupTypes value from this util.
| Name | Type | Required | Behavior |
|---|---|---|---|
channelType |
any |
required | Passed through to the implementation as written. |
util.moveElementInArray
Signature
discord.util.moveElementInArray(array, element, newIndex, offset)Move an element to a new index in a Lua array.
| Name | Type | Required | Behavior |
|---|---|---|---|
array |
any |
required | Passed through to the implementation as written. |
element |
any |
required | Passed through to the implementation as written. |
newIndex |
any |
required | Passed through to the implementation as written. |
offset |
any |
required | Passed through to the implementation as written. |
util.verifyString
Signature
discord.util.verifyString(data, errorType, errorMessage, allowEmpty)Assert that a value is a non-empty string, or raise.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
errorType |
any |
required | Passed through to the implementation as written. |
errorMessage |
any |
required | Passed through to the implementation as written. |
allowEmpty |
any |
required | Passed through to the implementation as written. |
util.resolveColor
Signature
discord.util.resolveColor(color)Convert a color name, hex string, number, or RGB table into the integer DiscordLua encodes on REST payloads.
| Name | Type | Required | Behavior |
|---|---|---|---|
color |
any |
required | Passed through to the implementation as written. |
util.discordSort
Signature
discord.util.discordSort(collection)Performs discord sort on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
collection |
any |
required | Passed through to the implementation as written. |
util.basename
Signature
discord.util.basename(path, ext)Performs basename on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
path |
any |
required | Passed through to the implementation as written. |
ext |
any |
required | Passed through to the implementation as written. |
util.findName
Signature
discord.util.findName(data)Performs find name on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
data |
any |
required | Passed through to the implementation as written. |
util.cleanContent
Signature
discord.util.cleanContent(text, context)Performs clean content on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
context |
any |
required | Passed through to the implementation as written. |
util.cleanCodeBlockContent
Signature
discord.util.cleanCodeBlockContent(text)Performs clean code block content on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
text |
any |
required | Passed through to the implementation as written. |
util.parseWebhookURL
Signature
discord.util.parseWebhookURL(url)Performs parse webhook url on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
url |
any |
required | Passed through to the implementation as written. |
util.transformResolved
Signature
discord.util.transformResolved(resolved)Performs transform resolved on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
resolved |
any |
required | Passed through to the implementation as written. |
util.resolveSKUId
Signature
discord.util.resolveSKUId(sku)Performs resolve skuid on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
sku |
any |
required | Passed through to the implementation as written. |
util.with_wrapped
Signature
discord.util.with_wrapped(client, wrap_fn, callback)Performs with wrapped on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
client |
any |
required | Passed through to the implementation as written. |
wrap_fn |
any |
required | Passed through to the implementation as written. |
callback |
function |
optional | Optional completion callback function(err, result). If omitted, failures raise a DiscordLua error. |
util.join_args
Signature
discord.util.join_args(args, startIndex, fallback)Performs join args on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
args |
any |
required | Passed through to the implementation as written. |
startIndex |
any |
required | Passed through to the implementation as written. |
fallback |
any |
required | Passed through to the implementation as written. |
util.permission_name
Signature
discord.util.permission_name(bit)Performs permission name on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
bit |
any |
required | Passed through to the implementation as written. |
util.public_error
Signature
discord.util.public_error(err)Performs public error on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
err |
any |
required | Passed through to the implementation as written. |
util.request
Signature
discord.util.request(client, method, path, body, callback)Performs request on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
client |
any |
required | Passed through to the implementation as written. |
method |
any |
required | Passed through to the implementation as written. |
path |
any |
required | Passed through to the implementation as written. |
body |
any |
required | Passed through to the implementation as written. |
callback |
function |
optional | Optional completion callback function(err, result). If omitted, failures raise a DiscordLua error. |
util.message_ids_from_json
Signature
discord.util.message_ids_from_json(body)Performs message ids from json on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
body |
any |
required | Passed through to the implementation as written. |
util.count
Signature
discord.util.count(value)Performs count on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.parse_duration_ms
Signature
discord.util.parse_duration_ms(value)Performs parse duration ms on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
value |
any |
required | Passed through to the implementation as written. |
util.timeout_until
Signature
discord.util.timeout_until(duration)Performs timeout until on this util. Call it from bot code when you need that operation on the current object.
| Name | Type | Required | Behavior |
|---|---|---|---|
duration |
any |
required | Passed through to the implementation as written. |