Skip to content

Commit

Permalink
refactor: use this spreads
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Jan 8, 2022
1 parent 787e26a commit c7c3b74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
22 changes: 8 additions & 14 deletions packages/builders/src/components/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import type { Component } from './Component';

export class ButtonComponent implements Component {
public readonly type = ComponentType.Button as const;
public readonly style?: ButtonStyle;
public readonly style!: ButtonStyle;
public readonly label?: string;
public readonly emoji?: APIMessageComponentEmoji;
public readonly disabled?: boolean;
public readonly customId?: string;
public readonly url?: string;
public readonly custom_id!: string;
public readonly url!: string;

public constructor(data?: APIButtonComponent) {
this.style = data?.style;
this.style = data?.style as ButtonStyle;
this.label = data?.label;
this.emoji = data?.emoji;
this.disabled = data?.disabled;
Expand All @@ -29,7 +29,7 @@ export class ButtonComponent implements Component {
if (data?.style === ButtonStyle.Link) {
this.url = data.url;
} else {
this.customId = data?.custom_id;
this.custom_id = data?.custom_id as string;
}
}

Expand Down Expand Up @@ -59,7 +59,7 @@ export class ButtonComponent implements Component {
*/
public setCustomId(customId: string) {
customIdValidator.parse(customId);
Reflect.set(this, 'customId', customId);
Reflect.set(this, 'custom_id', customId);
return this;
}

Expand Down Expand Up @@ -94,15 +94,9 @@ export class ButtonComponent implements Component {
}

public toJSON(): APIButtonComponent {
validateRequiredButtonParameters(this.style!, this.label, this.emoji, this.customId, this.url);
validateRequiredButtonParameters(this.style, this.label, this.emoji, this.custom_id, this.url);
return {
type: this.type,
style: this.style!,
label: this.label,
url: this.url!,
emoji: this.emoji,
disabled: this.disabled,
custom_id: this.customId!,
...this,
};
}
}
27 changes: 11 additions & 16 deletions packages/builders/src/components/selectMenu/SelectMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export class SelectMenuComponent implements Component {
public readonly type = ComponentType.SelectMenu as const;
public readonly options: SelectMenuOption[];
public readonly placeholder?: string;
public readonly minValues?: number;
public readonly maxValues?: number;
public readonly customId?: string;
public readonly min_values?: number;
public readonly max_values?: number;
public readonly custom_id!: string;
public readonly disabled?: boolean;

public constructor(data?: APISelectMenuComponent) {
this.options = data?.options.map((option) => new SelectMenuOption(option)) ?? [];
this.placeholder = data?.placeholder;
this.minValues = data?.min_values;
this.maxValues = data?.max_values;
this.customId = data?.custom_id;
this.min_values = data?.min_values;
this.max_values = data?.max_values;
this.custom_id = data?.custom_id as string;
this.disabled = data?.disabled;
}

Expand All @@ -46,7 +46,7 @@ export class SelectMenuComponent implements Component {
*/
public setMinValues(minValues: number) {
minMaxValidator.parse(minValues);
Reflect.set(this, 'minValues', minValues);
Reflect.set(this, 'min_values', minValues);
return this;
}

Expand All @@ -56,7 +56,7 @@ export class SelectMenuComponent implements Component {
*/
public setMaxValues(maxValues: number) {
minMaxValidator.parse(maxValues);
Reflect.set(this, 'maxValues', maxValues);
Reflect.set(this, 'max_values', maxValues);
return this;
}

Expand All @@ -66,7 +66,7 @@ export class SelectMenuComponent implements Component {
*/
public setCustomId(customId: string) {
customIdValidator.parse(customId);
Reflect.set(this, 'customId', customId);
Reflect.set(this, 'custom_id', customId);
return this;
}

Expand Down Expand Up @@ -100,15 +100,10 @@ export class SelectMenuComponent implements Component {
}

public toJSON(): APISelectMenuComponent {
validateRequiredSelectMenuParameters(this.options, this.customId);
validateRequiredSelectMenuParameters(this.options, this.custom_id);
return {
type: this.type,
custom_id: this.customId!,
...this,
options: this.options.map((option) => option.toJSON()),
placeholder: this.placeholder,
min_values: this.minValues,
max_values: this.maxValues,
disabled: this.disabled,
};
}
}
14 changes: 5 additions & 9 deletions packages/builders/src/components/selectMenu/SelectMenuOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
* Represents an option within a select menu component
*/
export class SelectMenuOption {
public readonly label?: string;
public readonly value?: string;
public readonly label!: string;
public readonly value!: string;
public readonly description?: string;
public readonly emoji?: APIMessageComponentEmoji;
public readonly default?: boolean;

public constructor(data?: APISelectMenuOption) {
this.label = data?.label;
this.value = data?.value;
this.label = data?.label as string;
this.value = data?.value as string;
this.description = data?.description;
this.emoji = data?.emoji;
this.default = data?.default;
Expand Down Expand Up @@ -75,11 +75,7 @@ export class SelectMenuOption {
public toJSON(): APISelectMenuOption {
validateRequiredSelectMenuOptionParameters(this.label, this.value);
return {
label: this.label!,
value: this.value!,
description: this.description,
emoji: this.emoji,
default: this.default,
...this,
};
}
}

0 comments on commit c7c3b74

Please sign in to comment.