Pronto logo

Pronto

Namespace

modules

Methods

static

charLimit(str, type, limitopt) → {string|'No message content'}

modules.charLimit() ensures a provided string does not exceed the specified character limit

Parameters

  • str string

    The string to trim if necessary

  • type 'MESSAGE'  |  'EMBED_TITLE'  |  'EMBED_DESCRIPTION'  |  'EMBED_FIELD_NAME'  |  'EMBED_FIELD_VALUE'  |  'EMBED_FOOTER'  |  'EMBED_AUTHOR'

    The indended purpose of the string

    • If specified, the limit will be retrieved from an object of known API limits
    • NOTE: If a limit is also specified, this argument will be ignored and the limit will be applied
  • limit number <optional>

    The allowed character limit

Returns

  • string  |  'No message content'

    The character-limited string, or No message content if no string was provided

Examples

// returns 'I will get trimmed...'
modules.charLimit('I will get trimmed to 21 characters', null, 21);
// returns '```Codeblock formatting is respected...```'
modules.charLimit('```Codeblock formatting is respected by modules.charLimit()```', null, 42);

Source

static

dateTimeGroup(guildopt, nullable, dateopt) → {Promise.<string>}

modules.dateTimeGroup() formats a date-time group for the specified date, or the current date if no date is provided

Parameters

  • guild Discord.Guild <optional> <nullable>

    The <Guild> to format the date-time group for

    • If null | undefined, the <GuildConfiguration> of the default guild defined by config.ids.DEFAULT_GUILD will be used instead
  • date string  |  number  |  Date <optional>
    Date.now()

    A Date object, or a resolvable date accepted by the Date() constructor, such as:

    • A Unix timestamp as a <string> | <number>
    • A string such as 09:00 1 Aug 2021
    • If undefined, the current date will be used

Returns

Example

// <GuildConfiguration.settings.longDate> = 'HHMM "h" ddd, d mmm yy'

// returns '2121 h Tue, 21 Sep 21'
modules.dateTimeGroup(null, 1632216060000);

Source

static

difference(arrayOne, arrayTwo) → {Array.<any>}

modules.difference() finds and returns the difference between two arrays, where each element exists in only one of the provided arrays

Parameters

  • arrayOne Array.<any>

    The first <any[]> to compare

  • arrayTwo Array.<any>

    The second <any[]> to compare

Returns

  • Array.<any>

    The <any[]> of the elements that differ

Example

// returns [0, 2]
modules.difference([0, 1, 3], [1, 2, 3]);

Source

static

enumerateResources(array, toStringopt) → {string|Array.<string>}

modules.enumerateResources() enumerates a <string[]> of submitted lesson resources, by ordering any <MessageAttachment> links first, then numbering any URL resources as [Resource n]

Parameters

  • array Array.<string>

    A <string[]> of the lesson resources to enumerate

  • toString boolean <optional>
    false

    Whether to return the enumerated resources as a <string[]> or a newline-delimited <string>

Returns

  • string  |  Array.<string>

    A newline-separated <string>, or a <string[]> of the enumerated resources

    • If the input <string[]> is empty, a <string> | <string[]> of N/A will be returned

Examples

// returns ['[Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)', '[Resource 1](https://...)', '[Resource 2](https://...)']
modules.enumerateResources(['[Resource](https://...)', '[Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)', '[Resource](https://...)']);
// returns ['[Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)', '[Resource 1](https://...)', '[Resource 2](https://...)']
modules.enumerateResources(['[Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)\n[Resource](https://...)', '[Resource](https://...)']);
// returns {string}:
// [Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)\n
// [Resource 1](https://...)\n
// [Resource 2](https://...)
modules.enumerateResources(['[Resource](https://...)', '[Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)', '[Resource](https://...)'], true);
// returns 'N/A'
modules.enumerateResources([], true);

Source

static

equals(arrayOne, arrayTwo) → {boolean}

modules.equals() determines whether two arrays are identical, with all elements in the same order

  • WARNING: This implementation will produce unpredictable results with nested arrays or object elements

Parameters

  • arrayOne Array.<any>

    The first <any[]> to compare

  • arrayTwo Array.<any>

    The second <any[]> to compare

Returns

  • boolean

    Whether the two arrays are identical

Examples

// returns true
modules.equals([1, 2, 3], [1, 2, 3]);
// returns false
modules.equals([1, 2, 3], [3, 2, 1]);

Source

static

extractId(str) → {Discord.Snowflake|false}

modules.extractId() parses a <User.id> from a potential <UserMention | MemberMention> string

Parameters

  • str Discord.MemberMention  |  Discord.MemberMention  |  string

    The potential mention string

Returns

  • Discord.Snowflake  |  false

    The parsed <User.id>, or false if str is not a <UserMention | MemberMention>

Examples

// returns '192181901065322496'
modules.extractId('<@192181901065322496>');
// returns false
modules.extractId('Pronto');

Source

static

formatDuration(raw, isElapsedopt) → {string}

modules.formatDuration() formats a duration of time or a historical Unix timestamp (ms) into a formatted string describing the elapsed duration

Parameters

  • raw number

    A duration of elapsed time in milliseconds, or a Unix timestamp (ms) in the past

  • isElapsed boolean <optional>
    false

    Whether raw is a duration of elapsed time, or is a timestamp whose duration since must be calculated

Returns

  • string

    The formatted string describing the elapsed duration

Example

// returns '21 days, 9 hrs, 6 min'
modules.formatDuration(1847160000, true);

Source

static

formatList(listObject, formatopt) → {string}

modules.formatList() formats a supplied listObject into a formatted list string

Parameters

  • listObject Object.<string, string>

    The list object to convert into a formatted list

    • Each [key, value] pair in the object represents the 'key: value' of each list entry
  • format Array.<string> <optional>
    ['**', ':** ']

    The formatting to apply [before, after] each key in the list

Returns

  • string

    The newline-separated list string

Examples

// returns {string}:
// **KeyOne:** ValueOne\n
// **KeyTwo:** ValueTwo
modules.formatList({
	'KeyOne': 'ValueOne',
	'KeyTwo': 'ValueTwo',
});
// returns {string}:
// `KeyOne` - ValueOne\n
// `KeyTwo` - ValueTwo
modules.formatList({
	'KeyOne': 'ValueOne',
	'KeyTwo': 'ValueTwo',
}, ['`', '` - ']);

Source

static

formatRoles(array, mentionopt, breakAtopt) → {string}

modules.formatRoles() formats a <Snowflake[] | Role[]> into a formatted string of role mentions (or escaped mentions)

Parameters

  • array Array.<Discord.Snowflake>  |  Array.<Discord.Role>

    A <Role.id[] | Role[]> to convert into a formatted string

  • mention boolean <optional>
    false

    Whether to mention the roles, or escape the mentions

  • breakAt number <optional>

    The number of roles to allow on each line, before inserting a line break

Returns

  • string

    The formatted string of role mentions

Examples

// returns {string}:
// @\u200bJames @\u200bAdministrators\n
// @\u200bModerators
modules.formatRoles(['285282768286646272', '285283460078239785', '285283751695745033'], false, 2);
// returns '<@&285282768286646272> <@&285283460078239785> <@&285283751695745033>'
modules.formatRoles(['285282768286646272', '285283460078239785', '285283751695745033'], true);

Source

static

isURL(str) → {boolean}

modules.isURL() uses the URL() constructor to validate whether a specified string is a valid URL

Parameters

  • str string

    The string to validate

Returns

  • boolean

    Whether the string is a valid URL

Examples

// returns true
modules.isURL('https://github.com/JamesNZL/Pronto');
// returns false
modules.isURL('i.am.not.a.url');

Source

static

jsCodeBlock(str) → {string}

modules.jsCodeBlock() formats a specified string into a Markdown codeblock with JavaScript syntax highlighting

Parameters

  • str string

    The string to format

Returns

  • string

    The string formatted into a Markdown JavaScript codeblock

Example

// returns '```js\nconsole.log("Hello world!")```'
modules.jsCodeBlock('console.log("Hello world!")');

Source

static

merge(target, source) → {Object}

modules.merge() performs a deep merge of a source object into a target object, by assigning any non-existing properties and overwriting values of existing properties

  • Nested objects are recursed through, to ensure all properties of the source object are merged into target

Parameters

  • target Object

    The target object to merge into

  • source Object

    The source object from which to copy properties

Returns

  • Object

    The source object merged into the target object

Example

const target = {
	one: 1,
	two: 2,
	three: 3,
};

const source = {
	two: 20,
	three: {
		four: 4,
	},
};

// returns {
//     one: 1,
//     two: 20,
//     three: {
//         four: 4,
//     },
// }
modules.merge(target, source);

Source

static

prefixCommand(command, guildopt) → {Promise.<string>}

modules.prefixCommand() applies the specified guild's command prefix to a <BaseCommand>'s command

Parameters

Returns

  • Promise.<string>

    The prefixed command

Example

// guild = <Guild>
// <GuildConfiguration.settings.prefix> = '!'

const { help } = await require('../commands/commands')(guild);

// returns '!help'
modules.prefixCommand(help, guild);

Source

static

processResources(attachmentopt, urlsopt) → {string}

modules.processResources() processes an input of lesson resource(s) into a string of the format

required by <Lesson.submittedResources>

Parameters

  • attachment Discord.MessageAttachment <optional>

    An input <MessageAttachment>

  • urls Array.<string> <optional>

    An input <string[]> of URLs

Returns

  • string

    A formatted string of Markdown hyperlinks, where each resource is separated by a newline

Example

// <MessageAttachment.name> = 'Message_Attachment_Name'
// <MessageAttachment.url> = 'https://cdn.discordapp.com/attachments/...'

// returns {string}:
// [Message_Attachment_Name](https://cdn.discordapp.com/attachments/...)\n
// [Resource](https://...)\n
// [Resource](https://...)
modules.processResources(<MessageAttachment>, ['https://...', 'https://...']);

Source

static

remove(array, elementnullable, indexopt) → {Array.<any>}

modules.remove() finds and removes a specified element from an <any[]>, or removes the element at a specified index, and returns the resultant array

Parameters

  • array Array.<any>

    The <any[]> to remove an element from

  • element any <nullable>

    The element to remove from the <any[]>

    • NOTE: If an index is specified, this argument will be ignored and the index element will be removed
  • index number <optional>

    The index of the element to remove from the <any[]>

Returns

  • Array.<any>

    The <any[]> with the element removed

Examples

// returns [0, 2]
modules.remove([0, 1, 2], null, 1);
// returns ['Zero', 'Two', 'Three']
modules.remove(['Zero', 'Pronto', 'Two', 'Three'], 'Pronto');
// returns ['Zero', 'One', 'Two']
modules.remove(['Zero', 'One', 'Two', 'Three'], 'One', 3);
// returns [0, 1, 2, 3]
modules.remove([0, 1, 2, 3], null, 5);

Source

static

sentenceCase(str) → {string}

modules.sentenceCase() converts a specified string to sentence case

Parameters

  • str string

    The string to convert

Returns

  • string

    The string in sentence case

Example

// returns 'Convert this sentence to sentence case'
modules.sentenceCase('convert this sentence to sentence case');

Source

static

sortMembersByRoles(guild) → {Promise.<function(Discord.GuildMember, Discord.GuildMember): number>}

modules.sortMembersByRoles() returns a compareFunction for Collection<Snowflake, GuildMember>.sort() that sorts the members in descending order of highest role

Parameters

Returns

  • Promise.<function(Discord.GuildMember, Discord.GuildMember): number>

    The compareFunction to be passed to the Collection<Snowflake, GuildMember>.sort() method

Example

// guild = <Guild>

// Sorts a Collection<Snowflake, GuildMember> in descending order of highest role
Collection<Snowflake, GuildMember>.sort(modules.sortMembersByRoles(guild));

Source

static

titleCase(str) → {string}

modules.titleCase() converts a specified string to title case

Parameters

  • str string

    The string to convert

Returns

  • string

    The string in title case

Example

// returns 'Convert This Sentence To Title Case'
modules.titleCase('convert this sentence to title case');

Source

static

updatedPermissions(stateOne, stateTwo) → {Array.<Discord.PermissionString>}

modules.updatedPermissions() finds the difference between the <Permissions> of two <GuildMember | Role> and returns it as a <PermissionString[]>

Parameters

  • stateOne Discord.GuildMember  |  Discord.Role

    The old <GuildMember | Role>

  • stateTwo Discord.GuildMember  |  Discord.Role

    The new <GuildMember | Role>

Returns

  • Array.<Discord.PermissionString>

    The <PermissionString[]> of the permissions that differ

Example

// <stateOne.permissions.toArray()> = ['MANAGE_CHANNELS', 'MANAGE_GUILD']
// <stateTwo.permissions.toArray()> = ['MANAGE_CHANNELS', 'MANAGE_MESSAGES']

// returns ['MANAGE_GUILD', 'MANAGE_MESSAGES']
modules.updatedPermissions(<stateOne>, <stateTwo>);

Source