Skip to content

Commit

Permalink
refactor(SlashCommandChannelOption): remove addChannelType
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Mar 2, 2022
1 parent 96eeb00 commit 7111a2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getChannelOption = () =>
.setName('owo')
.setDescription('Testing 123')
.setRequired(true)
.addChannelType(ChannelType.GuildText);
.addChannelTypes(ChannelType.GuildText);

const getStringOption = () =>
new SlashCommandStringOption().setName('owo').setDescription('Testing 123').setRequired(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,25 @@ describe('Slash Commands', () => {
test('GIVEN a builder with both choices and autocomplete THEN does throw an error', () => {
expect(() =>
getBuilder().addStringOption(
// @ts-expect-error Checking if check works JS-side too
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
getStringOption().setAutocomplete(true).addChoices(['Fancy Pants', 'fp_1']),
getStringOption().setAutocomplete(true).addChoices({ name: 'Fancy Pants', value: 'fp_1' }),
),
).toThrowError();

expect(() =>
getBuilder().addStringOption(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
getStringOption()
.setAutocomplete(true)
// @ts-expect-error Checking if check works JS-side too
.addChoices([
['Fancy Pants', 'fp_1'],
['Fancy Shoes', 'fs_1'],
['The Whole shebang', 'all'],
]),
.addChoices(
{ name: 'Fancy Pants', value: 'fp_1' },
{ name: 'Fancy Shoes', value: 'fs_1' },
{ name: 'The Whole shebang', value: 'all' },
),
),
).toThrowError();

expect(() =>
getBuilder().addStringOption(
// @ts-expect-error Checking if check works JS-side too
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
getStringOption().addChoices(['Fancy Pants', 'fp_1']).setAutocomplete(true),
getStringOption().addChoices({ name: 'Fancy Pants', value: 'fp_1' }).setAutocomplete(true),
),
).toThrowError();

Expand Down Expand Up @@ -229,20 +223,20 @@ describe('Slash Commands', () => {

test('GIVEN a builder with valid channel options and channel_types THEN does not throw an error', () => {
expect(() =>
getBuilder().addChannelOption(getChannelOption().addChannelType(ChannelType.GuildText)),
getBuilder().addChannelOption(getChannelOption().addChannelTypes(ChannelType.GuildText)),
).not.toThrowError();

expect(() => {
getBuilder().addChannelOption(
getChannelOption().addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText]),
getChannelOption().addChannelTypes(ChannelType.GuildNews, ChannelType.GuildText),
);
}).not.toThrowError();
});

test('GIVEN a builder with valid channel options and channel_types THEN does throw an error', () => {
expect(() => getBuilder().addChannelOption(getChannelOption().addChannelType(100))).toThrowError();
expect(() => getBuilder().addChannelOption(getChannelOption().addChannelTypes(100))).toThrowError();

expect(() => getBuilder().addChannelOption(getChannelOption().addChannelTypes([100, 200]))).toThrowError();
expect(() => getBuilder().addChannelOption(getChannelOption().addChannelTypes(100, 200))).toThrowError();
});

test('GIVEN a builder with invalid number min/max options THEN does throw an error', () => {
Expand Down
5 changes: 0 additions & 5 deletions packages/builders/docs/examples/Slash Command Builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ const boopCommand = new SlashCommandBuilder()
.setName('boop_reminder')
.setDescription('How often should we remind you to boop the user')
.addChoices({ name: 'Every day', value: 1 }, { name: 'Weekly', value: 7 })
// Or, if you prefer adding more choices at once, you can use an array
.addChoices(
['Every three months', 90],
['Yearly', 365],
),
);

// Get the final raw data that can be sent to Discord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,33 @@ const allowedChannelTypes = [

export type ApplicationCommandOptionAllowedChannelTypes = typeof allowedChannelTypes[number];

const channelTypePredicate = z.union(
allowedChannelTypes.map((type) => z.literal(type)) as [
ZodLiteral<ChannelType>,
ZodLiteral<ChannelType>,
...ZodLiteral<ChannelType>[]
],
const channelTypesPredicate = z.array(
z.union(
allowedChannelTypes.map((type) => z.literal(type)) as [
ZodLiteral<ChannelType>,
ZodLiteral<ChannelType>,
...ZodLiteral<ChannelType>[]
],
),
);

export class ApplicationCommandOptionChannelTypesMixin {
public readonly channel_types?: ApplicationCommandOptionAllowedChannelTypes[];

/**
* Adds a channel type to this option
* Adds channel types to this option
*
* @param channelType The type of channel to allow
* @param channelTypes The channel types to add
*/
public addChannelType(channelType: ApplicationCommandOptionAllowedChannelTypes) {
public addChannelTypes(...channelTypes: ApplicationCommandOptionAllowedChannelTypes[]) {
if (this.channel_types === undefined) {
Reflect.set(this, 'channel_types', []);
}

channelTypePredicate.parse(channelType);
this.channel_types!.push(channelType);
channelTypesPredicate.parse(channelTypes);

return this;
}
this.channel_types!.push(...channelTypes);

/**
* Adds channel types to this option
*
* @param channelTypes The channel types to add
*/
public addChannelTypes(channelTypes: ApplicationCommandOptionAllowedChannelTypes[]) {
channelTypes.forEach((channelType) => this.addChannelType(channelType));
return this;
}
}

0 comments on commit 7111a2f

Please sign in to comment.