Skip to content

Commit

Permalink
feat: add chatInputApplicationCommandMention formatter (#8546)
Browse files Browse the repository at this point in the history
feat: add `chatInputApplicationCommandMention()` formatter

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
almeidx and kodiakhq[bot] committed Sep 3, 2022
1 parent 0dc6844 commit d08a57c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/builders/__tests__/messages/formatters.test.ts
Expand Up @@ -2,6 +2,7 @@
import { URL } from 'node:url';
import { describe, test, expect, vitest } from 'vitest';
import {
chatInputApplicationCommandMention,
blockQuote,
bold,
channelLink,
Expand Down Expand Up @@ -137,6 +138,26 @@ describe('Message formatters', () => {
expect(roleMention('815434166602170409')).toEqual('<@&815434166602170409>');
});
});

describe('chatInputApplicationCommandMention', () => {
test('GIVEN commandName and commandId THEN returns "</[commandName]:[commandId]>"', () => {
expect(chatInputApplicationCommandMention('airhorn', '815434166602170409')).toEqual(
'</airhorn:815434166602170409>',
);
});

test('GIVEN commandName, subcommandName, and commandId THEN returns "</[commandName] [subcommandName]:[commandId]>"', () => {
expect(chatInputApplicationCommandMention('airhorn', 'sub', '815434166602170409')).toEqual(
'</airhorn sub:815434166602170409>',
);
});

test('GIVEN commandName, subcommandGroupName, subcommandName, and commandId THEN returns "</[commandName] [subcommandGroupName] [subcommandName]:[commandId]>"', () => {
expect(chatInputApplicationCommandMention('airhorn', 'group', 'sub', '815434166602170409')).toEqual(
'</airhorn group sub:815434166602170409>',
);
});
});
});

describe('formatEmoji', () => {
Expand Down
69 changes: 69 additions & 0 deletions packages/builders/src/messages/formatters.ts
Expand Up @@ -180,6 +180,75 @@ export function roleMention<C extends Snowflake>(roleId: C): `<@&${C}>` {
return `<@&${roleId}>`;
}

/**
* Formats an application command name, subcommand group name, subcommand name, and ID into an application command mention
*
* @param commandName - The application command name to format
* @param subcommandGroupName - The subcommand group name to format
* @param subcommandName - The subcommand name to format
* @param commandId - The application command ID to format
*/
export function chatInputApplicationCommandMention<
N extends string,
G extends string,
S extends string,
I extends Snowflake,
>(commandName: N, subcommandGroupName: G, subcommandName: S, commandId: I): `</${N} ${G} ${S}:${I}>`;

/**
* Formats an application command name, subcommand name, and ID into an application command mention
*
* @param commandName - The application command name to format
* @param subcommandName - The subcommand name to format
* @param commandId - The application command ID to format
*/
export function chatInputApplicationCommandMention<N extends string, S extends string, I extends Snowflake>(
commandName: N,
subcommandName: S,
commandId: I,
): `</${N} ${S}:${I}>`;

/**
* Formats an application command name and ID into an application command mention
*
* @param commandName - The application command name to format
* @param commandId - The application command ID to format
*/
export function chatInputApplicationCommandMention<N extends string, I extends Snowflake>(
commandName: N,
commandId: I,
): `</${N}:${I}>`;

/**
* Formats an application command name, subcommand group name, subcommand name, and ID into an application command mention
*
* @param commandName - The application command name to format
* @param subcommandGroupName - The subcommand group name to format
* @param subcommandName - The subcommand name to format
* @param commandId - The application command ID to format
*/
export function chatInputApplicationCommandMention<
N extends string,
G extends Snowflake | string,
S extends Snowflake | string,
I extends Snowflake,
>(
commandName: N,
subcommandGroupName: G,
subcommandName?: S,
commandId?: I,
): `</${N} ${G} ${S}:${I}>` | `</${N} ${G}:${S}>` | `</${N}:${G}>` {
if (typeof commandId !== 'undefined') {
return `</${commandName} ${subcommandGroupName} ${subcommandName!}:${commandId}>`;
}

if (typeof subcommandName !== 'undefined') {
return `</${commandName} ${subcommandGroupName}:${subcommandName}>`;
}

return `</${commandName}:${subcommandGroupName}>`;
}

/**
* Formats an emoji ID into a fully qualified emoji identifier
*
Expand Down
11 changes: 11 additions & 0 deletions packages/discord.js/src/util/Formatters.js
Expand Up @@ -21,6 +21,17 @@ const {
userMention,
} = require('@discordjs/builders');

/**
* Formats an application command name and id into an application command mention.
* @method chatInputApplicationCommandMention
* @param {string} commandName The name of the application command
* @param {string|Snowflake} subcommandGroupOrSubOrId
* The subcommand group name, subcommand name, or application command id
* @param {?(string|Snowflake)} [subcommandNameOrId] The subcommand name or application command id
* @param {?string} [commandId] The id of the application command
* @returns {string}
*/

/**
* Wraps the content inside a code block with an optional language.
* @method codeBlock
Expand Down

0 comments on commit d08a57c

Please sign in to comment.