Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add chatInputApplicationCommandMention formatter #8546

Merged
merged 2 commits into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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