fix: types (#59)

closes #58
This commit is contained in:
Marcos Susaña 2022-07-16 18:08:27 -04:00 committed by GitHub
parent e87f26efb6
commit ddfcab3142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 83 deletions

View File

@ -5,16 +5,13 @@ import { CreateApplicationCommand } from "../../../Session.ts";
export abstract class ApplicationCommandBuilder implements CreateApplicationCommand { export abstract class ApplicationCommandBuilder implements CreateApplicationCommand {
constructor( constructor(
// required type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput,
public type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput, name = "",
public name = "", description = "",
public description = "", defaultMemberPermissions?: PermissionStrings[],
// non-required nameLocalizations?: Localization,
public defaultMemberPermissions?: PermissionStrings[], descriptionLocalizations?: Localization,
// etc dmPermission = true,
public nameLocalizations?: Localization,
public descriptionLocalizations?: Localization,
public dmPermission = true,
) { ) {
this.type = type; this.type = type;
this.name = name; this.name = name;
@ -24,51 +21,59 @@ export abstract class ApplicationCommandBuilder implements CreateApplicationComm
this.descriptionLocalizations = descriptionLocalizations; this.descriptionLocalizations = descriptionLocalizations;
this.dmPermission = dmPermission; this.dmPermission = dmPermission;
} }
type: ApplicationCommandTypes;
name: string;
description: string;
defaultMemberPermissions?: PermissionStrings[];
nameLocalizations?: Localization;
descriptionLocalizations?: Localization;
dmPermission: boolean;
public setType(type: ApplicationCommandTypes): this { setType(type: ApplicationCommandTypes): this {
return (this.type = type), this; return (this.type = type), this;
} }
public setName(name: string): this { setName(name: string): this {
return (this.name = name), this; return (this.name = name), this;
} }
public setDescription(description: string): this { setDescription(description: string): this {
return (this.description = description), this; return (this.description = description), this;
} }
public setDefaultMemberPermission(perm: PermissionStrings[]): this { setDefaultMemberPermission(perm: PermissionStrings[]): this {
return (this.defaultMemberPermissions = perm), this; return (this.defaultMemberPermissions = perm), this;
} }
public setNameLocalizations(l: Localization): this { setNameLocalizations(l: Localization): this {
return (this.nameLocalizations = l), this; return (this.nameLocalizations = l), this;
} }
public setDescriptionLocalizations(l: Localization): this { setDescriptionLocalizations(l: Localization): this {
return (this.descriptionLocalizations = l), this; return (this.descriptionLocalizations = l), this;
} }
public setDmPermission(perm: boolean): this { setDmPermission(perm: boolean): this {
return (this.dmPermission = perm), this; return (this.dmPermission = perm), this;
} }
} }
export class MessageApplicationCommandBuilder { export class MessageApplicationCommandBuilder {
public constructor( type: ApplicationCommandTypes;
// required name?: string;
public type?: ApplicationCommandTypes, constructor(
public name?: string, type?: ApplicationCommandTypes,
name?: string,
) { ) {
this.type = ApplicationCommandTypes.Message; this.type = type ?? ApplicationCommandTypes.Message;
this.name = name; this.name = name;
} }
public setName(name: string): this { setName(name: string): this {
return (this.name = name), this; return (this.name = name), this;
} }
public toJSON(): { name: string; type: ApplicationCommandTypes.Message } { toJSON(): { name: string; type: ApplicationCommandTypes.Message } {
if (!this.name) throw new TypeError("Propety 'name' is required"); if (!this.name) throw new TypeError("Propety 'name' is required");
return { return {
@ -79,9 +84,9 @@ export class MessageApplicationCommandBuilder {
} }
export class ChatInputApplicationCommandBuilder extends ApplicationCommandBuilder { export class ChatInputApplicationCommandBuilder extends ApplicationCommandBuilder {
public type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput; type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput;
public toJSON(): CreateApplicationCommand { toJSON(): CreateApplicationCommand {
if (!this.type) throw new TypeError("Propety 'type' is required"); if (!this.type) throw new TypeError("Propety 'type' is required");
if (!this.name) throw new TypeError("Propety 'name' is required"); if (!this.name) throw new TypeError("Propety 'name' is required");
if (!this.description) { if (!this.description) {

View File

@ -2,20 +2,20 @@ import { ApplicationCommandOptionTypes, type ChannelTypes, type Localization } f
import { ApplicationCommandOptionChoice } from "../../interactions/BaseInteraction.ts"; import { ApplicationCommandOptionChoice } from "../../interactions/BaseInteraction.ts";
export class ChoiceBuilder { export class ChoiceBuilder {
public name?: string; name?: string;
public value?: string; value?: string;
public setName(name: string): ChoiceBuilder { setName(name: string): ChoiceBuilder {
this.name = name; this.name = name;
return this; return this;
} }
public setValue(value: string): this { setValue(value: string): this {
this.value = value; this.value = value;
return this; return this;
} }
public toJSON(): ApplicationCommandOptionChoice { toJSON(): ApplicationCommandOptionChoice {
if (!this.name) throw new TypeError("Property 'name' is required"); if (!this.name) throw new TypeError("Property 'name' is required");
if (!this.value) throw new TypeError("Property 'value' is required"); if (!this.value) throw new TypeError("Property 'value' is required");
@ -27,32 +27,35 @@ export class ChoiceBuilder {
} }
export class OptionBuilder { export class OptionBuilder {
public required?: boolean; required?: boolean;
public autocomplete?: boolean; autocomplete?: boolean;
type?: ApplicationCommandOptionTypes;
name?: string;
description?: string;
public constructor(public type?: ApplicationCommandOptionTypes, public name?: string, public description?: string) { constructor(type?: ApplicationCommandOptionTypes, name?: string, description?: string) {
this.type = type; this.type = type;
this.name = name; this.name = name;
this.description = description; this.description = description;
} }
public setType(type: ApplicationCommandOptionTypes): this { setType(type: ApplicationCommandOptionTypes): this {
return (this.type = type), this; return (this.type = type), this;
} }
public setName(name: string): this { setName(name: string): this {
return (this.name = name), this; return (this.name = name), this;
} }
public setDescription(description: string): this { setDescription(description: string): this {
return (this.description = description), this; return (this.description = description), this;
} }
public setRequired(required: boolean): this { setRequired(required: boolean): this {
return (this.required = required), this; return (this.required = required), this;
} }
public toJSON(): ApplicationCommandOption { toJSON(): ApplicationCommandOption {
if (!this.type) throw new TypeError("Property 'type' is required"); if (!this.type) throw new TypeError("Property 'type' is required");
if (!this.name) throw new TypeError("Property 'name' is required"); if (!this.name) throw new TypeError("Property 'name' is required");
if (!this.description) { if (!this.description) {
@ -71,14 +74,14 @@ export class OptionBuilder {
} }
export class OptionBuilderLimitedValues extends OptionBuilder { export class OptionBuilderLimitedValues extends OptionBuilder {
public choices?: ChoiceBuilder[]; choices?: ChoiceBuilder[];
public minValue?: number; minValue?: number;
public maxValue?: number; maxValue?: number;
public constructor( constructor(
public type?: ApplicationCommandOptionTypes.Integer | ApplicationCommandOptionTypes.Number, type?: ApplicationCommandOptionTypes.Integer | ApplicationCommandOptionTypes.Number,
public name?: string, name?: string,
public description?: string, description?: string,
) { ) {
super(); super();
this.type = type; this.type = type;
@ -86,22 +89,22 @@ export class OptionBuilderLimitedValues extends OptionBuilder {
this.description = description; this.description = description;
} }
public setMinValue(n: number): this { setMinValue(n: number): this {
return (this.minValue = n), this; return (this.minValue = n), this;
} }
public setMaxValue(n: number): this { setMaxValue(n: number): this {
return (this.maxValue = n), this; return (this.maxValue = n), this;
} }
public addChoice(fn: (choice: ChoiceBuilder) => ChoiceBuilder): this { addChoice(fn: (choice: ChoiceBuilder) => ChoiceBuilder): this {
const choice = fn(new ChoiceBuilder()); const choice = fn(new ChoiceBuilder());
this.choices ??= []; this.choices ??= [];
this.choices.push(choice); this.choices.push(choice);
return this; return this;
} }
public override toJSON(): ApplicationCommandOption { override toJSON(): ApplicationCommandOption {
return { return {
...super.toJSON(), ...super.toJSON(),
choices: this.choices?.map((c) => c.toJSON()) ?? [], choices: this.choices?.map((c) => c.toJSON()) ?? [],
@ -112,11 +115,11 @@ export class OptionBuilderLimitedValues extends OptionBuilder {
} }
export class OptionBuilderString extends OptionBuilder { export class OptionBuilderString extends OptionBuilder {
public choices?: ChoiceBuilder[]; choices?: ChoiceBuilder[];
public constructor( constructor(
public type?: ApplicationCommandOptionTypes.String, type?: ApplicationCommandOptionTypes.String,
public name?: string, name?: string,
public description?: string, description?: string,
) { ) {
super(); super();
this.type = type; this.type = type;
@ -125,14 +128,14 @@ export class OptionBuilderString extends OptionBuilder {
this; this;
} }
public addChoice(fn: (choice: ChoiceBuilder) => ChoiceBuilder): this { addChoice(fn: (choice: ChoiceBuilder) => ChoiceBuilder): this {
const choice = fn(new ChoiceBuilder()); const choice = fn(new ChoiceBuilder());
this.choices ??= []; this.choices ??= [];
this.choices.push(choice); this.choices.push(choice);
return this; return this;
} }
public override toJSON(): ApplicationCommandOption { override toJSON(): ApplicationCommandOption {
return { return {
...super.toJSON(), ...super.toJSON(),
choices: this.choices?.map((c) => c.toJSON()) ?? [], choices: this.choices?.map((c) => c.toJSON()) ?? [],
@ -141,11 +144,11 @@ export class OptionBuilderString extends OptionBuilder {
} }
export class OptionBuilderChannel extends OptionBuilder { export class OptionBuilderChannel extends OptionBuilder {
public channelTypes?: ChannelTypes[]; channelTypes?: ChannelTypes[];
public constructor( constructor(
public type?: ApplicationCommandOptionTypes.Channel, type?: ApplicationCommandOptionTypes.Channel,
public name?: string, name?: string,
public description?: string, description?: string,
) { ) {
super(); super();
this.type = type; this.type = type;
@ -154,13 +157,13 @@ export class OptionBuilderChannel extends OptionBuilder {
this; this;
} }
public addChannelTypes(...channels: ChannelTypes[]): this { addChannelTypes(...channels: ChannelTypes[]): this {
this.channelTypes ??= []; this.channelTypes ??= [];
this.channelTypes.push(...channels); this.channelTypes.push(...channels);
return this; return this;
} }
public override toJSON(): ApplicationCommandOption { override toJSON(): ApplicationCommandOption {
return { return {
...super.toJSON(), ...super.toJSON(),
channelTypes: this.channelTypes ?? [], channelTypes: this.channelTypes ?? [],
@ -173,7 +176,7 @@ export interface OptionBuilderLike {
} }
export class OptionBased { export class OptionBased {
public options?: options?:
& ( & (
| OptionBuilder[] | OptionBuilder[]
| OptionBuilderString[] | OptionBuilderString[]
@ -183,80 +186,80 @@ export class OptionBased {
) )
& OptionBuilderLike[]; & OptionBuilderLike[];
public addOption(fn: (option: OptionBuilder) => OptionBuilder, type?: ApplicationCommandOptionTypes): this { addOption(fn: (option: OptionBuilder) => OptionBuilder, type?: ApplicationCommandOptionTypes): this {
const option = fn(new OptionBuilder(type)); const option = fn(new OptionBuilder(type));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addNestedOption(fn: (option: OptionBuilder) => OptionBuilder): this { addNestedOption(fn: (option: OptionBuilder) => OptionBuilder): this {
const option = fn(new OptionBuilder(ApplicationCommandOptionTypes.SubCommand)); const option = fn(new OptionBuilder(ApplicationCommandOptionTypes.SubCommand));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addStringOption(fn: (option: OptionBuilderString) => OptionBuilderString): this { addStringOption(fn: (option: OptionBuilderString) => OptionBuilderString): this {
const option = fn(new OptionBuilderString(ApplicationCommandOptionTypes.String)); const option = fn(new OptionBuilderString(ApplicationCommandOptionTypes.String));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addIntegerOption(fn: (option: OptionBuilderLimitedValues) => OptionBuilderLimitedValues): this { addIntegerOption(fn: (option: OptionBuilderLimitedValues) => OptionBuilderLimitedValues): this {
const option = fn(new OptionBuilderLimitedValues(ApplicationCommandOptionTypes.Integer)); const option = fn(new OptionBuilderLimitedValues(ApplicationCommandOptionTypes.Integer));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addNumberOption(fn: (option: OptionBuilderLimitedValues) => OptionBuilderLimitedValues): this { addNumberOption(fn: (option: OptionBuilderLimitedValues) => OptionBuilderLimitedValues): this {
const option = fn(new OptionBuilderLimitedValues(ApplicationCommandOptionTypes.Number)); const option = fn(new OptionBuilderLimitedValues(ApplicationCommandOptionTypes.Number));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addBooleanOption(fn: (option: OptionBuilder) => OptionBuilder): this { addBooleanOption(fn: (option: OptionBuilder) => OptionBuilder): this {
return this.addOption(fn, ApplicationCommandOptionTypes.Boolean); return this.addOption(fn, ApplicationCommandOptionTypes.Boolean);
} }
public addSubCommand(fn: (option: OptionBuilderNested) => OptionBuilderNested): this { addSubCommand(fn: (option: OptionBuilderNested) => OptionBuilderNested): this {
const option = fn(new OptionBuilderNested(ApplicationCommandOptionTypes.SubCommand)); const option = fn(new OptionBuilderNested(ApplicationCommandOptionTypes.SubCommand));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addSubCommandGroup(fn: (option: OptionBuilderNested) => OptionBuilderNested): this { addSubCommandGroup(fn: (option: OptionBuilderNested) => OptionBuilderNested): this {
const option = fn(new OptionBuilderNested(ApplicationCommandOptionTypes.SubCommandGroup)); const option = fn(new OptionBuilderNested(ApplicationCommandOptionTypes.SubCommandGroup));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addUserOption(fn: (option: OptionBuilder) => OptionBuilder): this { addUserOption(fn: (option: OptionBuilder) => OptionBuilder): this {
return this.addOption(fn, ApplicationCommandOptionTypes.User); return this.addOption(fn, ApplicationCommandOptionTypes.User);
} }
public addChannelOption(fn: (option: OptionBuilderChannel) => OptionBuilderChannel): this { addChannelOption(fn: (option: OptionBuilderChannel) => OptionBuilderChannel): this {
const option = fn(new OptionBuilderChannel(ApplicationCommandOptionTypes.Channel)); const option = fn(new OptionBuilderChannel(ApplicationCommandOptionTypes.Channel));
this.options ??= []; this.options ??= [];
this.options.push(option); this.options.push(option);
return this; return this;
} }
public addRoleOption(fn: (option: OptionBuilder) => OptionBuilder): this { addRoleOption(fn: (option: OptionBuilder) => OptionBuilder): this {
return this.addOption(fn, ApplicationCommandOptionTypes.Role); return this.addOption(fn, ApplicationCommandOptionTypes.Role);
} }
public addMentionableOption(fn: (option: OptionBuilder) => OptionBuilder): this { addMentionableOption(fn: (option: OptionBuilder) => OptionBuilder): this {
return this.addOption(fn, ApplicationCommandOptionTypes.Mentionable); return this.addOption(fn, ApplicationCommandOptionTypes.Mentionable);
} }
// deno-lint-ignore ban-types // deno-lint-ignore ban-types
public static applyTo(klass: Function, ignore: Array<keyof OptionBased> = []): void { static applyTo(klass: Function, ignore: Array<keyof OptionBased> = []): void {
const methods: Array<keyof OptionBased> = [ const methods: Array<keyof OptionBased> = [
"addOption", "addOption",
"addNestedOption", "addNestedOption",
@ -281,10 +284,10 @@ export class OptionBased {
} }
export class OptionBuilderNested extends OptionBuilder { export class OptionBuilderNested extends OptionBuilder {
public constructor( constructor(
public type?: ApplicationCommandOptionTypes.SubCommand | ApplicationCommandOptionTypes.SubCommandGroup, type?: ApplicationCommandOptionTypes.SubCommand | ApplicationCommandOptionTypes.SubCommandGroup,
public name?: string, name?: string,
public description?: string, description?: string,
) { ) {
super(); super();
this.type = type; this.type = type;
@ -292,7 +295,7 @@ export class OptionBuilderNested extends OptionBuilder {
this.description = description; this.description = description;
} }
public override toJSON(): ApplicationCommandOption { override toJSON(): ApplicationCommandOption {
if (!this.type) throw new TypeError("Property 'type' is required"); if (!this.type) throw new TypeError("Property 'type' is required");
if (!this.name) throw new TypeError("Property 'name' is required"); if (!this.name) throw new TypeError("Property 'name' is required");
if (!this.description) { if (!this.description) {