detritus

Utilsnamespace

Builders for embeds, components and modals, plus markup, permission and regex helpers.

On this page
import { Utils } from 'detritus-client';
3 namespaces · 35 classes · 33 interfaces · 17 type aliases · 2 variables · 28 functions

Namespaces

Grouped exports, reached as properties of the package.

Classes

Classes you create or receive from the library.

classComponentActionBase The most basic Structure class, every structure extends this 4 properties · 3 methods classComponentActionRow Utils Component Action Row Structure 9 properties · 11 methods classComponentButton Utils Component Button Structure 8 properties · 8 methods classComponentCheckbox Utils Component Checkbox Structure, only usable in modals inside of a label 3 properties · 3 methods classComponentCheckboxGroup Utils Component Checkbox Group Structure, only usable in modals inside of a label 6 properties · 7 methods classComponentContainer Utils Component Container Structure 7 properties · 17 methods classComponentContext 71 properties · 13 methods classComponentFile Utils Component File Structure 4 properties · 5 methods classComponentFileUpload Utils Component File Upload Structure, only usable in modals inside of a label 6 properties · 7 methods classComponentInputText Utils Component Input Text Structure 9 properties classComponentLabel Utils Component Label Structure, used to wrap a single component inside of a modal 5 properties · 12 methods classComponentMediaGallery Utils Component Media Gallery Structure 3 properties · 5 methods classComponentMediaGalleryItem Utils Component Media Gallery Item Structure 3 properties · 4 methods classComponentMenuActionItem Utils Component Menu Action Item Structure, an item inside of an overflow menu's group 8 properties · 8 methods classComponentOverflowMenu Utils Component Overflow Menu Structure, the "..." button that opens a menu of grouped items Can go inside of action rows (like a button) or be a section's accessory, needs components v2 7 properties · 8 methods classComponentOverflowMenuGroup Utils Component Overflow Menu Group Structure, a list of menu action items with an optional header 4 properties · 6 methods classComponentRadioGroup Utils Component Radio Group Structure, only usable in modals inside of a label 4 properties · 5 methods classComponents Utils Components Structure 6 properties · 28 methods classComponentSection Utils Component Section Structure 8 properties · 11 methods classComponentSelectMenu Utils Component Select Menu Structure 10 properties · 13 methods classComponentSelectMenuDefaultValue Utils Component Select Menu Default Value Structure 2 properties · 2 methods classComponentSelectMenuOption Utils Component Select Menu Option Structure 5 properties · 7 methods classComponentSeparator Utils Component Separator Structure 4 properties · 5 methods classComponentTextDisplay Utils Component Text Display Structure 3 properties · 4 methods classComponentThumbnail Utils Component Thumbnail Structure 5 properties · 6 methods classEmbed Utils Embed Structure 16 properties · 12 methods classEmbedAuthor Utils Embed Author Structure, used for [Embed] Structures 4 properties classEmbedField Utils Embed Field Structure, used for [Embed] Structures 3 properties classEmbedFooter Utils Embed Footer Structure, used for [Embed] Structures 3 properties classEmbedImage Utils Embed Image Structure, used for [Embed] Structures 4 properties classEmbedProvider Utils Provider Structure, used for [Embed] Structures 2 properties classEmbedThumbnail Utils Embed Thumbnail Structure, used for [Embed] Structures classEmbedVideo Utils Embed Video Structure, used for [Embed] Structures classInteractionModal The most basic Structure class, every structure extends this 5 properties · 19 methods classInteractionModalContext 59 properties · 13 methods

Interfaces

Object shapes for options, payloads and events.

interfaceComponentActionData 33 properties interfaceComponentActionRowData 2 properties interfaceComponentContainerData 6 properties interfaceComponentFileData 4 properties interfaceComponentLabelData 5 properties interfaceComponentMediaGalleryData 3 properties interfaceComponentMediaGalleryItemData 3 properties interfaceComponentOverflowMenuData 3 properties interfaceComponentOverflowMenuGroupData 2 properties interfaceComponentSectionData 4 properties interfaceComponentSelectMenuDefaultValueData 2 properties interfaceComponentSelectMenuOptionData 6 properties interfaceComponentSeparatorData 4 properties interfaceComponentsOptions 6 properties interfaceComponentTextDisplayData 3 properties interfaceComponentThumbnailData 5 properties interfaceCreateComponentListenerOrNone 2 properties interfaceDiscordRegexPayloadEmoji 2 properties interfaceDiscordRegexPayloadGuildNavigation 2 properties interfaceDiscordRegexPayloadJumpChannel 2 properties interfaceDiscordRegexPayloadJumpChannelMessage 2 properties interfaceDiscordRegexPayloadMentionChannelOrRole 2 properties interfaceDiscordRegexPayloadMentionCommand 2 properties interfaceDiscordRegexPayloadMentionUser 2 properties interfaceDiscordRegexPayloadSoundboardSound 2 properties interfaceDiscordRegexPayloadTextCodeblock 2 properties interfaceDiscordRegexPayloadTextGeneric 2 properties interfaceDiscordRegexPayloadTextHeader 2 properties interfaceDiscordRegexPayloadTextSubtext 2 properties interfaceDiscordRegexPayloadTimestamp 2 properties interfaceImportedFile 2 properties interfaceInteractionModalOptions 8 properties interfaceNormalizeApplicationCommandOptions 1 property

Functions

Standalone helpers.

diffNormalized()

function src/utils/applicationcommands.ts:153
function diffNormalized(a: any, b: any, path?: string): string[]

Deep compares two normalized payloads, returning the paths that differ (e.g. options[0:user].description)

a
any
b
any
path?
string
Default ''

generateWaveform()

function src/utils/index.ts:183
function generateWaveform(samples: Float32Array, sampleRate?: number): string
samples
Float32Array
sampleRate?
number
Default 48000

getAttachmentUrlExpiry()

function src/utils/index.ts:221
function getAttachmentUrlExpiry(url: string): number

Unix ms of when a signed attachment url expires, 0 if it has no (valid hex) ex query parameter

getFiles()

function async src/utils/index.ts:241
function getFiles(directory: string, subdirectories?: boolean): Promise<string[]>

getFirstArgument()

function src/utils/index.ts:331
function getFirstArgument(value: string): [string, string]

getFormatFromHash()

function src/utils/index.ts:279
function getFormatFromHash(
  hash: string,
  format?: string | null,
  defaultFormat?: string,
): string
hash
string
format?
string | null
defaultFormat?
string
Default ImageFormats.PNG

importFilesIn()

function async src/utils/index.ts:101
function importFilesIn(
  directory: string,
  subdirectories: boolean,
  reload?: boolean,
): Promise<{ errors: Record<string, Error>; files: ImportedFile[] }>

Imports every command file in a directory, yielding every few ms since a sync require() loop blocks the event loop

directory
string
subdirectories
boolean
reload?
boolean
Default false

intToRGB()

function src/utils/index.ts:363
function intToRGB(int: number): { b: number; g: number; r: number }

isValidAttachmentUrl()

function src/utils/index.ts:380
function isValidAttachmentUrl(url: string): boolean

If an attachment url is signed (ex, is and a 64 character hm, all hex) and hasn't expired yet. Only the format is checked, the hm signature itself can only be verified by discord

normalizeLocalizations()

function src/utils/applicationcommands.ts:39
function normalizeLocalizations(
  value?: Record<string, string | undefined> | null,
): Record<string, string> | null

parseMentionsInText()

function src/utils/index.ts:407
function parseMentionsInText(
  context: Interaction | Message,
  content: string,
  options?: { escapeMentions?: boolean; guildSpecific?: boolean; nick?: boolean; text?: string },
): string
content
string
options?
{ escapeMentions?: boolean; guildSpecific?: boolean; nick?: boolean; text?: string }
options.escapeMentions?
boolean
options.guildSpecific?
boolean
options.nick?
boolean
options.text?
string

regex()

function src/utils/index.ts:573
function regex(
  regexName: string,
  content: string,
  onlyFirst?: boolean,
): DiscordRegexPayload
regexName
string
content
string
onlyFirst?
boolean
Default false

requireOrImport()

function async src/utils/index.ts:62
function requireOrImport(filepath: string, reload?: boolean): Promise<any>

Loads with require() so its cache can be cleared on reload, falling back to import() for es modules it can't load

filepath
string
reload?
boolean
Default false

Type aliases

Named types used across the API.

DiscordRegexPayload

type src/utils/index.ts:564

Variables

Exported values.

KeyGenerator

variable const src/utils/index.ts:676
const KeyGenerator: Readonly<{ ApplicationCommand: (command: ApplicationCommand | InteractionCommand<…>) => string; ApplicationCommandOption: (option: InteractionCommandOption<…> | ApplicationCommandOption) => string; ApplicationCommandOptionChoice: (choice: InteractionCommandOptionChoice | ApplicationCommandOptionChoice) => string }>
export const KeyGenerator = Object.freeze({
  ApplicationCommand: (command: ApplicationCommand | InteractionCommand): string => {
    return [
      command.name,
      command.description,
      command.type,
      (command.options) ? command.options.map((x) => x.key).join(':') : '',
      JSON.stringify(normalizeLocalizations(command.descriptionLocalizations)),
      JSON.stringify(normalizeLocalizations(command.nameLocalizations)),
      command.defaultMemberPermissions,
      (command.dmPermission === undefined || command.dmPermission === null) ? true : !!command.dmPermission,
      normalizeContexts(command.contexts),
      normalizeIntegrationTypes(command.integrationTypes),
      Boolean(command.nsfw),
    ].join('-');
  },
  ApplicationCommandOption: (option: ApplicationCommandOption | InteractionCommandOption): string => {
    return [
      option.name,
      option.description,
      option.type,
      !!option.required,
      !!option.autocomplete,
      (option.options) ? option.options.map((x) => x.key).join(':') : '',
      (option.choices) ? option.choices.map((x) => x.key).join(':') : '',
      JSON.stringify(normalizeLocalizations(option.descriptionLocalizations)),
      JSON.stringify(normalizeLocalizations(option.nameLocalizations)),
      JSON.stringify(option.channelTypes),
      option.maxValue,
      option.minValue,
      option.maxLength,
      option.minLength,
    ].join('-');
  },
  ApplicationCommandOptionChoice: (choice: ApplicationCommandOptionChoice | InteractionCommandOptionChoice): string => {
    return `${choice.name}-${choice.value}-${typeof(choice.value)}-${JSON.stringify(normalizeLocalizations(choice.nameLocalizations))}`;
  },
});