Skip to content

Commit

Permalink
feat(help): add more help when encountering the wrong feature name
Browse files Browse the repository at this point in the history
add a new field to show that a flag --help exists
  • Loading branch information
C0ZEN committed Jun 27, 2021
1 parent 87fb002 commit 3cabff8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
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
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 @@ -64,6 +66,7 @@ export class DiscordMessageCommandFeatureWrongFeatureNameErrorService extends Di
this._getMessageEmbedFieldError(featureName),
this._getMessageEmbedFieldAllFeatures(),
this._getMessageEmbedFieldFeatureExample(anyDiscordMessage, commands),
this._getMessageEmbedFieldNeedHelp(anyDiscordMessage, commands),
];
}

Expand All @@ -73,4 +76,24 @@ export class DiscordMessageCommandFeatureWrongFeatureNameErrorService extends Di
value: `\`${featureName}\` is not an existing feature...\nLet me show you the list of available features with an example and maybe try again with a valid one this time, ok?`,
};
}

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!`,
};
}
}

0 comments on commit 3cabff8

Please sign in to comment.