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(flag): add a help flag for the feature command #1538

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions src/enums/service-name.enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ describe(`ServiceNameEnum`, (): void => {
);
});

it(`should have a member "DISCORD_MESSAGE_COMMAND_FEATURE_FLAGS_SERVICE"`, (): void => {
expect.assertions(1);

expect(ServiceNameEnum.DISCORD_MESSAGE_COMMAND_FEATURE_FLAGS_SERVICE).toStrictEqual(
`DiscordMessageCommandFeatureFlagsService`
);
});

it(`should have a member "DISCORD_MESSAGE_COMMAND_FEATURE_NOON_CONFIG_CORE_SERVICE"`, (): void => {
expect.assertions(1);

Expand Down
1 change: 1 addition & 0 deletions src/enums/service-name.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum ServiceNameEnum {
DISCORD_MESSAGE_COMMAND_FEATURE_EMPTY_CONTENT_ERROR_SERVICE = `DiscordMessageCommandFeatureEmptyContentErrorService`,
DISCORD_MESSAGE_COMMAND_FEATURE_EMPTY_FEATURE_NAME_ERROR_SERVICE = `DiscordMessageCommandFeatureEmptyFeatureNameErrorService`,
DISCORD_MESSAGE_COMMAND_FEATURE_EMPTY_FLAGS_ERROR_SERVICE = `DiscordMessageCommandFeatureEmptyFlagsErrorService`,
DISCORD_MESSAGE_COMMAND_FEATURE_FLAGS_SERVICE = `DiscordMessageCommandFeatureFlagsService`,
DISCORD_MESSAGE_COMMAND_FEATURE_NOON_CONFIG_CORE_SERVICE = `DiscordMessageCommandFeatureNoonConfigCoreService`,
DISCORD_MESSAGE_COMMAND_FEATURE_NOON_CONFIG_MUTATOR_SERVICE = `DiscordMessageCommandFeatureNoonConfigMutatorService`,
DISCORD_MESSAGE_COMMAND_FEATURE_NOON_CONFIG_SERVICE = `DiscordMessageCommandFeatureNoonConfigService`,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DiscordMessageCommandFeatureEmptyFeatureNameErrorService } from './serv
import { DiscordMessageCommandFeatureWrongFeatureNameErrorService } from './services/feature-names/discord-message-command-feature-wrong-feature-name-error.service';
import { DiscordMessageCommandFeatureDuplicatedFlagsErrorService } from './services/flags/discord-message-command-feature-duplicated-flags-error.service';
import { DiscordMessageCommandFeatureEmptyFlagsErrorService } from './services/flags/discord-message-command-feature-empty-flags-error.service';
import { DiscordMessageCommandFeatureFlagsService } from './services/flags/discord-message-command-feature-flags.service';
import { DiscordMessageCommandFeatureOppositeFlagsErrorService } from './services/flags/discord-message-command-feature-opposite-flags-error.service';
import { DiscordMessageCommandFeatureWrongFlagsErrorService } from './services/flags/discord-message-command-feature-wrong-flags-error.service';
import { AbstractService } from '../../../../../../classes/services/abstract.service';
Expand Down Expand Up @@ -70,6 +71,10 @@ export class DiscordMessageCommandFeatureService extends AbstractService {
const message: Message = anyDiscordMessage as Message;
const featureName: string | null = this._getFeatureName(message.content);

if (DiscordMessageCommandFeatureFlagsService.getInstance().hasFlag(message.content)) {
return DiscordMessageCommandFeatureFlagsService.getInstance().getMessageResponse(message.content);
}

if (_.isNil(featureName)) {
LoggerService.getInstance().debug({
context: this._serviceName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DiscordMessageCommandFeatureFlagEnum } from './discord-message-command-feature-flag.enum';
import { getEnumLength } from '../../../../../../../functions/checks/get-enum-length';

describe(`DiscordMessageCommandFeatureFlagEnum`, (): void => {
it(`should have 2 members`, (): void => {
expect.assertions(1);

expect(getEnumLength(DiscordMessageCommandFeatureFlagEnum)).toStrictEqual(2);
});

it(`should have a member "H"`, (): void => {
expect.assertions(1);

expect(DiscordMessageCommandFeatureFlagEnum.H).toStrictEqual(`h`);
});

it(`should have a member "HELP"`, (): void => {
expect.assertions(1);

expect(DiscordMessageCommandFeatureFlagEnum.HELP).toStrictEqual(`help`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum DiscordMessageCommandFeatureFlagEnum {
/**
* Shortcut for {@link HELP}
*/
H = `h`,
HELP = `help`,
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
expect(result.options.embed?.color).toStrictEqual(ColorEnum.CANDY);
});

it(`should return a Discord message response embed with 3 fields`, async (): Promise<void> => {
it(`should return a Discord message response embed with 4 fields`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields).toHaveLength(3);
expect(result.options.embed?.fields).toHaveLength(4);
});

it(`should return a Discord message response embed with a field explaining the error`, async (): Promise<void> => {
Expand Down Expand Up @@ -180,6 +180,18 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);
discordMessageConfigServiceGetMessageCommandPrefixSpy.mockReturnValue([`-`]);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the given Discord message content is not a valid command`, (): void => {
Expand All @@ -198,6 +210,18 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);
discordMessageConfigServiceGetMessageCommandPrefixSpy.mockReturnValue([`-`]);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the given Discord message content is a valid command as "!f"`, (): void => {
Expand All @@ -221,6 +245,17 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`!f noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag by taking the prefix and command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!f --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the prefix is "-"`, (): void => {
Expand All @@ -238,6 +273,17 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});
});

Expand All @@ -262,6 +308,17 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the prefix is "-"`, (): void => {
Expand All @@ -279,6 +336,17 @@ describe(`DiscordMessageCommandFeatureEmptyFeatureNameErrorService`, (): void =>
value: `\`-lunch noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag by taking the prefix and command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`-lunch --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ServiceNameEnum } from '../../../../../../../../enums/service-name.enum';
import { DiscordMessageCommandEnum } from '../../../../../enums/commands/discord-message-command.enum';
import { discordGetCommandAndPrefix } from '../../../../../functions/commands/getters/discord-get-command-and-prefix';
import { IDiscordMessageResponse } from '../../../../../interfaces/discord-message-response';
import { IAnyDiscordMessage } from '../../../../../types/any-discord-message';
import { DiscordMessageConfigService } from '../../../../config/discord-message-config.service';
import { DiscordMessageCommandCliErrorService } from '../../../discord-message-command-cli-error.service';
import { DiscordMessageCommandFeatureErrorCoreService } from '../discord-message-command-feature-error-core.service';
import { EmbedFieldData, MessageEmbedOptions } from 'discord.js';
Expand Down Expand Up @@ -61,6 +63,7 @@ export class DiscordMessageCommandFeatureEmptyFeatureNameErrorService extends Di
this._getMessageEmbedFieldError(),
this._getMessageEmbedFieldAllFeatures(),
this._getMessageEmbedFieldFeatureExample(anyDiscordMessage, commands),
this._getMessageEmbedFieldNeedHelp(anyDiscordMessage, commands),
];
}

Expand All @@ -70,4 +73,24 @@ export class DiscordMessageCommandFeatureEmptyFeatureNameErrorService extends Di
value: `You did not specify the name of the feature you wish to configure.\nI will not guess it for you so please try again with a feature name!\nAnd because I am kind and generous here is the list of all the features you can configure with an example.`,
};
}

private _getMessageEmbedFieldNeedHelp(
{ content }: Readonly<IAnyDiscordMessage>,
commands: Readonly<DiscordMessageCommandEnum>[]
): EmbedFieldData {
let userCommand: string | null = discordGetCommandAndPrefix({
commands,
message: _.isNil(content) ? `` : content,
prefixes: DiscordMessageConfigService.getInstance().getMessageCommandPrefix(),
});

if (_.isNil(userCommand)) {
userCommand = `!${_.toLower(DiscordMessageCommandEnum.FEATURE)}`;
}

return {
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`${userCommand} --help\` and I will try my best to help you!`,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
expect(result.options.embed?.color).toStrictEqual(ColorEnum.CANDY);
});

it(`should return a Discord message response embed with 3 fields`, async (): Promise<void> => {
it(`should return a Discord message response embed with 4 fields`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields).toHaveLength(3);
expect(result.options.embed?.fields).toHaveLength(4);
});

it(`should return a Discord message response embed with a field explaining the error`, async (): Promise<void> => {
Expand Down Expand Up @@ -182,6 +182,18 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);
discordMessageConfigServiceGetMessageCommandPrefixSpy.mockReturnValue([`-`]);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the given Discord message content is not a valid command`, (): void => {
Expand All @@ -200,6 +212,18 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);
discordMessageConfigServiceGetMessageCommandPrefixSpy.mockReturnValue([`-`]);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the given Discord message content is a valid command as "!f"`, (): void => {
Expand All @@ -223,6 +247,17 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`!f noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag taking the prefix and command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!f --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the prefix is "-"`, (): void => {
Expand All @@ -240,6 +275,17 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});
});

Expand All @@ -264,6 +310,17 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`!feature noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag and the "!feature" command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`!feature --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});

describe(`when the prefix is "-"`, (): void => {
Expand All @@ -281,6 +338,17 @@ describe(`DiscordMessageCommandFeatureWrongFeatureNameErrorService`, (): void =>
value: `\`-lunch noon\``,
} as EmbedFieldData);
});

it(`should return a Discord message response embed with a field to show an example of the command with the help flag by taking the prefix and command`, async (): Promise<void> => {
expect.assertions(1);

const result = await service.getMessageResponse(anyDiscordMessage, commands, featureName);

expect(result.options.embed?.fields?.[3]).toStrictEqual({
name: `Need help?`,
value: `If you need my help, you can also specify the help flag \`-lunch --help\` and I will try my best to help you!`,
} as EmbedFieldData);
});
});
});

Expand Down