This repository has been archived by the owner on Jan 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ApplicationCommandOptions): clean up code for builder options (#68)
- Loading branch information
1 parent
a9addd5
commit b5d0b15
Showing
22 changed files
with
318 additions
and
257 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
src/interactions/slashCommands/mixins/ApplicationCommandNumericOptionMinMaxValueMixin.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,16 @@ | ||
export abstract class ApplicationCommandNumericOptionMinMaxValueMixin { | ||
protected readonly maxValue?: number; | ||
protected readonly minValue?: number; | ||
|
||
/** | ||
* Sets the maximum number value of this option | ||
* @param max The maximum value this option can be | ||
*/ | ||
public abstract setMaxValue(max: number): this; | ||
|
||
/** | ||
* Sets the minimum number value of this option | ||
* @param min The minimum value this option can be | ||
*/ | ||
public abstract setMinValue(min: number): this; | ||
} |
32 changes: 32 additions & 0 deletions
32
src/interactions/slashCommands/mixins/ApplicationCommandOptionBase.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,32 @@ | ||
import type { APIApplicationCommandBasicOption, ApplicationCommandOptionType } from 'discord-api-types/v9'; | ||
import { validateRequiredParameters, validateRequired } from '../Assertions'; | ||
import { SharedNameAndDescription } from './NameAndDescription'; | ||
|
||
export abstract class ApplicationCommandOptionBase extends SharedNameAndDescription { | ||
public abstract readonly type: ApplicationCommandOptionType; | ||
|
||
public readonly required = false; | ||
|
||
/** | ||
* Marks the option as required | ||
* | ||
* @param required If this option should be required | ||
*/ | ||
public setRequired(required: boolean) { | ||
// Assert that you actually passed a boolean | ||
validateRequired(required); | ||
|
||
Reflect.set(this, 'required', required); | ||
|
||
return this; | ||
} | ||
|
||
public abstract toJSON(): APIApplicationCommandBasicOption; | ||
|
||
protected runRequiredValidations() { | ||
validateRequiredParameters(this.name, this.description, []); | ||
|
||
// Assert that you actually passed a boolean | ||
validateRequired(this.required); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/interactions/slashCommands/mixins/ApplicationCommandOptionChannelTypesMixin.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,55 @@ | ||
import { ChannelType } from 'discord-api-types/v9'; | ||
import { z, ZodLiteral } from 'zod'; | ||
|
||
// Only allow valid channel types to be used. (This can't be dynamic because const enums are erased at runtime) | ||
const allowedChannelTypes = [ | ||
ChannelType.GuildText, | ||
ChannelType.GuildVoice, | ||
ChannelType.GuildCategory, | ||
ChannelType.GuildNews, | ||
ChannelType.GuildStore, | ||
ChannelType.GuildNewsThread, | ||
ChannelType.GuildPublicThread, | ||
ChannelType.GuildPrivateThread, | ||
ChannelType.GuildStageVoice, | ||
] as const; | ||
|
||
export type ApplicationCommandOptionAllowedChannelTypes = typeof allowedChannelTypes[number]; | ||
|
||
const channelTypePredicate = 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 | ||
* | ||
* @param channelType The type of channel to allow | ||
*/ | ||
public addChannelType(channelType: ApplicationCommandOptionAllowedChannelTypes) { | ||
if (this.channel_types === undefined) { | ||
Reflect.set(this, 'channel_types', []); | ||
} | ||
|
||
channelTypePredicate.parse(channelType); | ||
this.channel_types!.push(channelType); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
Oops, something went wrong.