DiscordLua
discorddiscord
v1.2.4
Install

util

Class
On this page

util is a DiscordLua class representing a Discord resource. Obtain it from a manager, an event payload, or the constructor when one is documented below.

class Module 1.2.4 Source

Methods

util.basename()

# static

Performs basename on this util. Call it from bot code when you need that operation on the current object.

Signature

util.basename(path, ext)

Parameters

NameTypeRequiredBehavior
path any required Passed through to the implementation as written.
ext any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.basename(path, ext)

util.class()

# static

Define a DiscordLua class table with callable construction and :new.

Signature

util.class(name)

Parameters

NameTypeRequiredBehavior
name any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.class(name)

util.cleanCodeBlockContent()

# static

Performs clean code block content on this util. Call it from bot code when you need that operation on the current object.

Signature

util.cleanCodeBlockContent(text)

Parameters

NameTypeRequiredBehavior
text any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.cleanCodeBlockContent(text)

util.cleanContent()

# static

Performs clean content on this util. Call it from bot code when you need that operation on the current object.

Signature

util.cleanContent(text, context)

Parameters

NameTypeRequiredBehavior
text any required Passed through to the implementation as written.
context any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.cleanContent(text, context)

util.count()

# static

Performs count on this util. Call it from bot code when you need that operation on the current object.

Signature

util.count(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.count(value)

util.discordSort()

# static

Performs discord sort on this util. Call it from bot code when you need that operation on the current object.

Signature

util.discordSort(collection)

Parameters

NameTypeRequiredBehavior
collection any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.discordSort(collection)

util.encode_list()

# static

Encode an array of values for a REST body field.

Signature

util.encode_list(list)

Parameters

NameTypeRequiredBehavior
list any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_list(list)

util.encode_message()

# static

Encode a message send/edit payload for REST.

Signature

util.encode_message(contentOrOptions)

Parameters

NameTypeRequiredBehavior
contentOrOptions table optional Lua option table. Field names match the corresponding DiscordLua option type when one exists.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_message("Hello")

util.encode_one()

# static

Encode a single Lua value for a REST body field.

Signature

util.encode_one(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_one(value)

util.encode_poll()

# static

Encode a poll object for a message payload.

Signature

util.encode_poll(options)

Parameters

NameTypeRequiredBehavior
options table optional Lua option table. Field names match the corresponding DiscordLua option type when one exists.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_poll({})

util.encode_poll_answer()

# static

Encode a single poll answer.

Signature

util.encode_poll_answer(answer)

Parameters

NameTypeRequiredBehavior
answer any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_poll_answer(answer)

util.encode_poll_media()

# static

Encode poll question or answer media.

Signature

util.encode_poll_media(media)

Parameters

NameTypeRequiredBehavior
media any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encode_poll_media(media)

util.encodeRoleOptions()

# static

Encode role create/edit options, including colors and permissions.

Signature

util.encodeRoleOptions(options)

Parameters

NameTypeRequiredBehavior
options table optional Lua option table. Field names match the corresponding DiscordLua option type when one exists.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.encodeRoleOptions({})

util.findName()

# static

Performs find name on this util. Call it from bot code when you need that operation on the current object.

Signature

util.findName(data)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.findName(data)

util.flatten()

# static

Flatten nested option tables before encoding a REST payload.

Signature

util.flatten(obj, props)

Parameters

NameTypeRequiredBehavior
obj any required Passed through to the implementation as written.
props any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.flatten(obj, props)

util.getSortableGroupTypes()

# static

Read the SortableGroupTypes value from this util.

Signature

util.getSortableGroupTypes(channelType)

Parameters

NameTypeRequiredBehavior
channelType any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.getSortableGroupTypes(channelType)

util.is_builder()

# static

Predicate that returns whether this util matches is_builder. Use it to branch before calling type-specific methods.

Signature

util.is_builder(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.is_builder(value)

util.is_json_null()

# static

Predicate that returns whether this util matches is_json_null. Use it to branch before calling type-specific methods.

Signature

util.is_json_null(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.is_json_null(value)

util.join_args()

# static

Performs join args on this util. Call it from bot code when you need that operation on the current object.

Signature

util.join_args(args, startIndex, fallback)

Parameters

NameTypeRequiredBehavior
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.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.join_args(args, startIndex, fallback)

util.makeError()

# static

Construct a DiscordLua error object from a name and message.

Signature

util.makeError(data)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.makeError(data)

util.makePlainError()

# static

Construct a plain error table without Discord-specific fields.

Signature

util.makePlainError(err)

Parameters

NameTypeRequiredBehavior
err any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.makePlainError(err)

util.message_ids_from_json()

# static

Performs message ids from json on this util. Call it from bot code when you need that operation on the current object.

Signature

util.message_ids_from_json(body)

Parameters

NameTypeRequiredBehavior
body any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.message_ids_from_json(body)

util.moveElementInArray()

# static

Move an element to a new index in a Lua array.

Signature

util.moveElementInArray(array, element, newIndex, offset)

Parameters

NameTypeRequiredBehavior
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.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.moveElementInArray(array, element, newIndex, offset)

util.parse_duration_ms()

# static

Performs parse duration ms on this util. Call it from bot code when you need that operation on the current object.

Signature

util.parse_duration_ms(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.parse_duration_ms(value)

util.parse_hex()

# static

Parse a hex color string into a numeric color value.

Signature

util.parse_hex(text)

Parameters

NameTypeRequiredBehavior
text any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.parse_hex(text)

util.parseEmoji()

# static

Parse an emoji mention, unicode character, or name into a table DiscordLua can send.

Signature

util.parseEmoji(text)

Parameters

NameTypeRequiredBehavior
text any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.parseEmoji(text)

util.parseWebhookURL()

# static

Performs parse webhook url on this util. Call it from bot code when you need that operation on the current object.

Signature

util.parseWebhookURL(url)

Parameters

NameTypeRequiredBehavior
url any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.parseWebhookURL(url)

util.permission_name()

# static

Performs permission name on this util. Call it from bot code when you need that operation on the current object.

Signature

util.permission_name(bit)

Parameters

NameTypeRequiredBehavior
bit any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.permission_name(bit)

util.public_error()

# static

Performs public error on this util. Call it from bot code when you need that operation on the current object.

Signature

util.public_error(err)

Parameters

NameTypeRequiredBehavior
err any required Passed through to the implementation as written.

Returns

any.

Throws

A DiscordLua error when arguments are invalid or the request fails without a callback.

Example

lua
local discord = require("discord")
-- object is a util
util.public_error(err)

util.request()

# static

Performs request on this util. Call it from bot code when you need that operation on the current object.

Signature

util.request(client, method, path, body, callback)

Parameters

NameTypeRequiredBehavior
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.

Returns

any.

Throws

A DiscordLua error when arguments are invalid or the request fails without a callback.

Example

lua
local discord = require("discord")
-- object is a util
util.request(client, method, path, body, function(err, result)
    if err then error(err) end
end)

util.resolveBase64()

# static

Encode binary or text input as a base64 string for REST image fields.

Signature

util.resolveBase64(data)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveBase64(data)

util.resolveCode()

# static

Extract an invite or template code from a URL or raw code string.

Signature

util.resolveCode(data, pattern)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.
pattern any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveCode(data, pattern)

util.resolveColor()

# static

Convert a color name, hex string, number, or RGB table into the integer DiscordLua encodes on REST payloads.

Signature

util.resolveColor(color)

Parameters

NameTypeRequiredBehavior
color any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveColor(color)

util.resolveGuildEmoji()

# static

Resolve a guild emoji from an id, mention, or emoji object.

Signature

util.resolveGuildEmoji(guild, emoji)

Parameters

NameTypeRequiredBehavior
guild any required Passed through to the implementation as written.
emoji any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveGuildEmoji(guild, emoji)

util.resolveGuildTemplateCode()

# static

Extract a guild template code from a URL or raw code string.

Signature

util.resolveGuildTemplateCode(data)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveGuildTemplateCode(data)

util.resolveImage()

# static

Turn a file path, data URI, or buffer into the image data DiscordLua sends on REST.

Signature

util.resolveImage(image)

Parameters

NameTypeRequiredBehavior
image any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveImage(image)

util.resolveInviteCode()

# static

Extract a Discord invite code from a URL or raw code string.

Signature

util.resolveInviteCode(data)

Parameters

NameTypeRequiredBehavior
data any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveInviteCode(data)

util.resolvePartialEmoji()

# static

Normalize an emoji mention or id/name table into a partial emoji payload.

Signature

util.resolvePartialEmoji(emoji)

Parameters

NameTypeRequiredBehavior
emoji any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolvePartialEmoji(emoji)

util.resolveRoleColor()

# static

Resolve a role color value the same way role create/edit payloads accept it.

Signature

util.resolveRoleColor(value)

Parameters

NameTypeRequiredBehavior
value any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveRoleColor(value)

util.resolveRoleColors()

# static

Resolve a table of role color fields before encoding a role payload.

Signature

util.resolveRoleColors(colors)

Parameters

NameTypeRequiredBehavior
colors any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveRoleColors(colors)

util.resolveSKUId()

# static

Performs resolve skuid on this util. Call it from bot code when you need that operation on the current object.

Signature

util.resolveSKUId(sku)

Parameters

NameTypeRequiredBehavior
sku any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.resolveSKUId(sku)

util.timeout_until()

# static

Performs timeout until on this util. Call it from bot code when you need that operation on the current object.

Signature

util.timeout_until(duration)

Parameters

NameTypeRequiredBehavior
duration any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.timeout_until(duration)

util.toSnakeCase()

# static

Convert a camelCase identifier into snake_case for REST body keys.

Signature

util.toSnakeCase(text)

Parameters

NameTypeRequiredBehavior
text any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.toSnakeCase(text)

util.transformResolved()

# static

Performs transform resolved on this util. Call it from bot code when you need that operation on the current object.

Signature

util.transformResolved(resolved)

Parameters

NameTypeRequiredBehavior
resolved any required Passed through to the implementation as written.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.transformResolved(resolved)

util.verifyString()

# static

Assert that a value is a non-empty string, or raise.

Signature

util.verifyString(data, errorType, errorMessage, allowEmpty)

Parameters

NameTypeRequiredBehavior
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.

Returns

any.

Throws

A DiscordLua error when arguments are invalid or the request fails without a callback.

Example

lua
local discord = require("discord")
-- object is a util
util.verifyString(data, errorType, errorMessage, allowEmpty)

util.with_wrapped()

# static

Performs with wrapped on this util. Call it from bot code when you need that operation on the current object.

Signature

util.with_wrapped(client, wrap_fn, callback)

Parameters

NameTypeRequiredBehavior
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.

Returns

any.

Example

lua
local discord = require("discord")
-- object is a util
util.with_wrapped(client, wrap_fn, function(err, result)
    if err then error(err) end
end)

Static members

  • util.null

Examples

Obtain this object from a manager, wrap helper, or event payload — it is not constructed directly in typical bot code.

ActionRow · Activity · ActivityInstance · ActivityLocation · AnonymousGuild · ApplicationCommand · ApplicationRoleConnectionMetadata · Attachment

Version: discord 1.2.4 · runtime 1.0.0

Search Ctrl K Navigate Open Esc