Skip to content

Commit

Permalink
refactor: re-introduce generic to choice mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Sep 5, 2024
1 parent 43d40f4 commit 526c5e4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export interface ApplicationCommandOptionWithChoicesData {
/**
* This mixin holds choices and autocomplete symbols used for options.
*/
export class ApplicationCommandOptionWithChoicesMixin {
export class ApplicationCommandOptionWithChoicesMixin<ChoiceType extends number | string> {
protected declare readonly data: ApplicationCommandOptionWithChoicesData;

/**
* Adds multiple choices to this option.
*
* @param choices - The choices to add
*/
public addChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number | string>>): this {
public addChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<ChoiceType>>): this {
const normalizedChoices = normalizeArray(choices);

this.data.choices ??= [];
Expand All @@ -31,7 +31,7 @@ export class ApplicationCommandOptionWithChoicesMixin {
*
* @param choices - The choices to set
*/
public setChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number | string>>): this {
public setChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<ChoiceType>>): this {
this.data.choices = normalizeArray(choices);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
import { ApplicationCommandOptionType } from 'discord-api-types/v10';
import { Mixin } from 'ts-mixer';
import type { RestOrArray } from '../../../util/normalizeArray.js';
import { integerOptionPredicate } from '../Assertions.js';
import { ApplicationCommandNumericOptionMinMaxValueMixin } from '../mixins/ApplicationCommandNumericOptionMinMaxValueMixin.js';
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase.js';
Expand All @@ -15,19 +13,11 @@ export class SlashCommandIntegerOption extends Mixin(
ApplicationCommandOptionBase,
ApplicationCommandNumericOptionMinMaxValueMixin,
ApplicationCommandOptionWithAutocompleteMixin,
ApplicationCommandOptionWithChoicesMixin,
ApplicationCommandOptionWithChoicesMixin<number>,
) {
protected override readonly predicate = integerOptionPredicate;

public constructor() {
super(ApplicationCommandOptionType.Integer);
}

public override addChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number>>): this {
return super.addChoices(...choices);
}

public override setChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number>>): this {
return super.setChoices(...choices);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
import { ApplicationCommandOptionType } from 'discord-api-types/v10';
import { Mixin } from 'ts-mixer';
import type { RestOrArray } from '../../../util/normalizeArray.js';
import { numberOptionPredicate } from '../Assertions.js';
import { ApplicationCommandNumericOptionMinMaxValueMixin } from '../mixins/ApplicationCommandNumericOptionMinMaxValueMixin.js';
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase.js';
Expand All @@ -15,19 +13,11 @@ export class SlashCommandNumberOption extends Mixin(
ApplicationCommandOptionBase,
ApplicationCommandNumericOptionMinMaxValueMixin,
ApplicationCommandOptionWithAutocompleteMixin,
ApplicationCommandOptionWithChoicesMixin,
ApplicationCommandOptionWithChoicesMixin<number>,
) {
protected override readonly predicate = numberOptionPredicate;

public constructor() {
super(ApplicationCommandOptionType.Number);
}

public override addChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number>>): this {
return super.addChoices(...choices);
}

public override setChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<number>>): this {
return super.setChoices(...choices);
}
}
17 changes: 2 additions & 15 deletions packages/builders/src/interactions/slashCommands/options/string.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
ApplicationCommandOptionType,
type APIApplicationCommandOptionChoice,
type APIApplicationCommandStringOption,
} from 'discord-api-types/v10';
import { ApplicationCommandOptionType, type APIApplicationCommandStringOption } from 'discord-api-types/v10';
import { Mixin } from 'ts-mixer';
import type { RestOrArray } from '../../../util/normalizeArray.js';
import { stringOptionPredicate } from '../Assertions.js';
import type { ApplicationCommandOptionBaseData } from '../mixins/ApplicationCommandOptionBase.js';
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase.js';
Expand All @@ -19,7 +14,7 @@ import { ApplicationCommandOptionWithChoicesMixin } from '../mixins/ApplicationC
export class SlashCommandStringOption extends Mixin(
ApplicationCommandOptionBase,
ApplicationCommandOptionWithAutocompleteMixin,
ApplicationCommandOptionWithChoicesMixin,
ApplicationCommandOptionWithChoicesMixin<string>,
) {
protected override readonly predicate = stringOptionPredicate;

Expand All @@ -32,14 +27,6 @@ export class SlashCommandStringOption extends Mixin(
super(ApplicationCommandOptionType.String);
}

public override addChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<string>>): this {
return super.addChoices(...choices);
}

public override setChoices(...choices: RestOrArray<APIApplicationCommandOptionChoice<string>>): this {
return super.setChoices(...choices);
}

/**
* Sets the maximum length of this string option.
*
Expand Down

0 comments on commit 526c5e4

Please sign in to comment.