-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(builders): new select menus * chore: better re-exporting of deprecated classes * feat: new select menus * chore: typings * chore: add missing todo comment * chore: finish updating tests * chore: add runtime deprecation warnings * chore: format deprecation warning * feat(BaseInteraction): isAnySelectMenu * chore: requested changes * fix: deprecation comments * chore: update @deprecated comments in typings * chore: add tests for select menu type narrowing * fix: bad auto imports Co-authored-by: Julian Vennen <julian@aternos.org> * fix: properly handle resolved members * fix: collectors * chore: suggested changes Co-authored-by: Almeida <almeidx@pm.me> * fix(typings): bad class extends * feat(ChannelSelectMenuBuilder): validation * chore: update todo comment * refactor(ChannelSelectMenu): better handling of channel_types state * chore: style nit * chore: suggested nits Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: Julian Vennen <julian@aternos.org> Co-authored-by: Almeida <almeidx@pm.me> Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8b400ca
commit 5152abf
Showing
50 changed files
with
1,530 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/builders/src/components/selectMenu/BaseSelectMenu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { APISelectMenuComponent } from 'discord-api-types/v10'; | ||
import { customIdValidator, disabledValidator, minMaxValidator, placeholderValidator } from '../Assertions.js'; | ||
import { ComponentBuilder } from '../Component.js'; | ||
|
||
export class BaseSelectMenuBuilder< | ||
SelectMenuType extends APISelectMenuComponent, | ||
> extends ComponentBuilder<SelectMenuType> { | ||
/** | ||
* Sets the placeholder for this select menu | ||
* | ||
* @param placeholder - The placeholder to use for this select menu | ||
*/ | ||
public setPlaceholder(placeholder: string) { | ||
this.data.placeholder = placeholderValidator.parse(placeholder); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the minimum values that must be selected in the select menu | ||
* | ||
* @param minValues - The minimum values that must be selected | ||
*/ | ||
public setMinValues(minValues: number) { | ||
this.data.min_values = minMaxValidator.parse(minValues); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the maximum values that must be selected in the select menu | ||
* | ||
* @param maxValues - The maximum values that must be selected | ||
*/ | ||
public setMaxValues(maxValues: number) { | ||
this.data.max_values = minMaxValidator.parse(maxValues); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the custom id for this select menu | ||
* | ||
* @param customId - The custom id to use for this select menu | ||
*/ | ||
public setCustomId(customId: string) { | ||
this.data.custom_id = customIdValidator.parse(customId); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets whether this select menu is disabled | ||
* | ||
* @param disabled - Whether this select menu is disabled | ||
*/ | ||
public setDisabled(disabled = true) { | ||
this.data.disabled = disabledValidator.parse(disabled); | ||
return this; | ||
} | ||
|
||
public toJSON(): SelectMenuType { | ||
customIdValidator.parse(this.data.custom_id); | ||
return { | ||
...this.data, | ||
} as SelectMenuType; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/builders/src/components/selectMenu/ChannelSelectMenu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { APIChannelSelectComponent, ChannelType } from 'discord-api-types/v10'; | ||
import { ComponentType } from 'discord-api-types/v10'; | ||
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js'; | ||
import { channelTypesValidator, customIdValidator } from '../Assertions.js'; | ||
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; | ||
|
||
export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSelectComponent> { | ||
/** | ||
* Creates a new select menu from API data | ||
* | ||
* @param data - The API data to create this select menu with | ||
* @example | ||
* Creating a select menu from an API data object | ||
* ```ts | ||
* const selectMenu = new ChannelSelectMenuBuilder({ | ||
* custom_id: 'a cool select menu', | ||
* placeholder: 'select an option', | ||
* max_values: 2, | ||
* }); | ||
* ``` | ||
* @example | ||
* Creating a select menu using setters and API data | ||
* ```ts | ||
* const selectMenu = new ChannelSelectMenuBuilder({ | ||
* custom_id: 'a cool select menu', | ||
* }) | ||
* .addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement) | ||
* .setMinValues(2) | ||
* ``` | ||
*/ | ||
public constructor(data?: Partial<APIChannelSelectComponent>) { | ||
super({ ...data, type: ComponentType.ChannelSelect }); | ||
} | ||
|
||
public addChannelTypes(...types: RestOrArray<ChannelType>) { | ||
// eslint-disable-next-line no-param-reassign | ||
types = normalizeArray(types); | ||
|
||
this.data.channel_types ??= []; | ||
this.data.channel_types.push(...channelTypesValidator.parse(types)); | ||
return this; | ||
} | ||
|
||
public setChannelTypes(...types: RestOrArray<ChannelType>) { | ||
// eslint-disable-next-line no-param-reassign | ||
types = normalizeArray(types); | ||
|
||
this.data.channel_types ??= []; | ||
this.data.channel_types.splice(0, this.data.channel_types.length, ...channelTypesValidator.parse(types)); | ||
return this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc ComponentBuilder.toJSON} | ||
*/ | ||
public override toJSON(): APIChannelSelectComponent { | ||
customIdValidator.parse(this.data.custom_id); | ||
|
||
return { | ||
...this.data, | ||
} as APIChannelSelectComponent; | ||
} | ||
} |
Oops, something went wrong.