From 89c525d052dbec3c3a46b58cfd293877a869a166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Wed, 27 Mar 2024 17:53:28 -0400 Subject: [PATCH] feat: Make message components great (#163) * fix: mentionables defaults * feat: make message api components great * fix: add url to button link --- package.json | 21 ++---- src/components/ActionRow.ts | 9 ++- src/components/BaseComponent.ts | 39 ++++++++++ src/components/BaseSelectMenuComponent.ts | 31 ++++++++ src/components/ButtonComponent.ts | 71 +++++++++++-------- src/components/ChannelSelectMenuComponent.ts | 14 ++-- .../MentionableSelectMenuComponent.ts | 8 ++- src/components/RoleSelectMenuComponent.ts | 8 ++- src/components/StringSelectMenuComponent.ts | 12 ++-- src/components/TextInputComponent.ts | 40 ++++++++--- src/components/UserSelectMenuComponent.ts | 8 ++- src/components/index.ts | 2 +- src/structures/extra/BaseComponent.ts | 13 ---- .../extra/BaseSelectMenuComponent.ts | 23 ------ 14 files changed, 184 insertions(+), 115 deletions(-) create mode 100644 src/components/BaseComponent.ts create mode 100644 src/components/BaseSelectMenuComponent.ts delete mode 100644 src/structures/extra/BaseComponent.ts delete mode 100644 src/structures/extra/BaseSelectMenuComponent.ts diff --git a/package.json b/package.json index 329224b..5104c09 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,7 @@ "main": "./lib/index.js", "module": "./lib/index.js", "types": "./lib/index.d.ts", - "files": [ - "lib/**" - ], + "files": ["lib/**"], "scripts": { "build": "tsc --outDir ./lib", "prepublishOnly": "npm run build", @@ -15,7 +13,7 @@ "lint": "biome lint --apply ./src", "format": "biome format --write ./src", "check-h": "biome check --apply ./src", - "check": "biome check --apply --changed --no-errors-on-unmatched ./src" + "check": "biome check --apply --no-errors-on-unmatched ./src" }, "author": "MARCROCK22", "license": "Apache-2.0", @@ -27,10 +25,7 @@ "ws": "^8.16.0" }, "lint-staged": { - "*.ts": [ - "biome check --apply", - "biome format --write" - ] + "*.ts": ["biome check --apply", "biome format --write"] }, "devDependencies": { "@biomejs/biome": "1.6.0", @@ -55,13 +50,7 @@ "bugs": { "url": "https://github.com/tiramisulabs/seyfert" }, - "keywords": [ - "api", - "discord", - "bots", - "typescript", - "botdev" - ], + "keywords": ["api", "discord", "bots", "typescript", "botdev"], "publishConfig": { "access": "public" }, @@ -71,4 +60,4 @@ "url": "https://github.com/socram03" } ] -} \ No newline at end of file +} diff --git a/src/components/ActionRow.ts b/src/components/ActionRow.ts index 21d0cd4..3c3e8cf 100644 --- a/src/components/ActionRow.ts +++ b/src/components/ActionRow.ts @@ -1,18 +1,21 @@ import type { APIMessageActionRowComponent, ComponentType } from '../common'; -import { BaseComponent } from '../structures/extra/BaseComponent'; +import { BaseComponent } from './BaseComponent'; import type { ActionRowMessageComponents } from './index'; import { componentFactory } from './index'; export class MessageActionRowComponent< T extends ActionRowMessageComponents, > extends BaseComponent { + private ComponentsFactory: T[]; constructor(data: { type: ComponentType.ActionRow; components: APIMessageActionRowComponent[]; }) { super(data); - this.components = data.components.map(componentFactory) as T[]; + this.ComponentsFactory = data.components.map(componentFactory) as T[]; } - components: T[]; + get components() { + return this.ComponentsFactory; + } } diff --git a/src/components/BaseComponent.ts b/src/components/BaseComponent.ts new file mode 100644 index 0000000..563f508 --- /dev/null +++ b/src/components/BaseComponent.ts @@ -0,0 +1,39 @@ +import { fromComponent } from '../builders'; +import type { + APIActionRowComponent, + APIActionRowComponentTypes, + APIButtonComponent, + APIChannelSelectComponent, + APIMentionableSelectComponent, + APIRoleSelectComponent, + APIStringSelectComponent, + APITextInputComponent, + APIUserSelectComponent, + ComponentType, +} from '../common'; + +export class BaseComponent { + constructor(public data: APIComponentsMap[T]) {} + + get type() { + return this.data.type; + } + + toJSON() { + return this.data; + } + + toBuilder() { + return fromComponent(this.data); + } +} +export interface APIComponentsMap { + [ComponentType.ActionRow]: APIActionRowComponent; + [ComponentType.Button]: APIButtonComponent; + [ComponentType.ChannelSelect]: APIChannelSelectComponent; + [ComponentType.MentionableSelect]: APIMentionableSelectComponent; + [ComponentType.RoleSelect]: APIRoleSelectComponent; + [ComponentType.StringSelect]: APIStringSelectComponent; + [ComponentType.UserSelect]: APIUserSelectComponent; + [ComponentType.TextInput]: APITextInputComponent; +} diff --git a/src/components/BaseSelectMenuComponent.ts b/src/components/BaseSelectMenuComponent.ts new file mode 100644 index 0000000..223b7ad --- /dev/null +++ b/src/components/BaseSelectMenuComponent.ts @@ -0,0 +1,31 @@ +import type { ComponentType } from '../common'; +import { BaseComponent } from './BaseComponent'; + +export type APISelectMenuComponentTypes = + | ComponentType.ChannelSelect + | ComponentType.MentionableSelect + | ComponentType.RoleSelect + | ComponentType.StringSelect + | ComponentType.UserSelect; + +export class BaseSelectMenuComponent extends BaseComponent { + get customId() { + return this.data.custom_id; + } + + get disabed() { + return this.data.disabled; + } + + get max() { + return this.data.max_values; + } + + get min() { + return this.data.min_values; + } + + get placeholder() { + return this.data.placeholder; + } +} diff --git a/src/components/ButtonComponent.ts b/src/components/ButtonComponent.ts index a2fa590..c131f12 100644 --- a/src/components/ButtonComponent.ts +++ b/src/components/ButtonComponent.ts @@ -1,41 +1,54 @@ -import type { - APIButtonComponentWithCustomId, - APIButtonComponentWithURL, - APIMessageComponentEmoji, - ComponentType, -} from '../common'; -import { ButtonStyle } from '../common'; -import { BaseComponent } from '../structures/extra/BaseComponent'; +import { Button, type ButtonStylesForID } from '../builders'; +import type { ButtonStyle, ComponentType } from '../common'; +import { BaseComponent } from './BaseComponent'; export class LinkButtonComponent extends BaseComponent { - constructor(data: APIButtonComponentWithURL) { - super(data); - this.label = data.label; - this.emoji = data.emoji; - this.disabled = !!data.disabled; - this.url = data.url; + get style() { + return this.data.style as ButtonStyle.Link; } - style = ButtonStyle.Link; - label?: string; - emoji?: APIMessageComponentEmoji; - disabled: boolean; - url: string; + get url(): string { + // @ts-ignore + return this.data.url; + } + + get label() { + return this.data.label; + } + + get disabled() { + return this.data.disabled; + } + + get emoji() { + return this.data.emoji; + } + + toBuilder() { + return new Button(this.data as never); + } } export type ButtonStyleExludeLink = Exclude; export class ButtonComponent extends BaseComponent { - constructor(data: APIButtonComponentWithCustomId) { - super(data); - this.style = data.style; - this.label = data.label; - this.emoji = data.emoji; - this.disabled = !!data.disabled; + get style() { + return this.data.style as ButtonStylesForID; } - style: ButtonStyleExludeLink; - label?: string; - emoji?: APIMessageComponentEmoji; - disabled: boolean; + get label() { + return this.data.label; + } + + get disabled() { + return this.data.disabled; + } + + get emoji() { + return this.data.emoji; + } + + toBuilder() { + return new Button(this.data as never); + } } diff --git a/src/components/ChannelSelectMenuComponent.ts b/src/components/ChannelSelectMenuComponent.ts index 21347ae..60797c0 100644 --- a/src/components/ChannelSelectMenuComponent.ts +++ b/src/components/ChannelSelectMenuComponent.ts @@ -1,12 +1,12 @@ -import type { APIChannelSelectComponent, ChannelType, ComponentType } from '../common'; -import { BaseSelectMenuComponent } from '../structures/extra/BaseSelectMenuComponent'; +import type { ComponentType } from '../common'; +import { BaseSelectMenuComponent } from './BaseSelectMenuComponent'; export class ChannelSelectMenuComponent extends BaseSelectMenuComponent { - constructor(data: APIChannelSelectComponent) { - super(data); - - this.channelTypes = data.channel_types; + get channelsTypes() { + return this.data.channel_types; } - channelTypes?: ChannelType[]; + get defaultValues() { + return this.data.default_values; + } } diff --git a/src/components/MentionableSelectMenuComponent.ts b/src/components/MentionableSelectMenuComponent.ts index 253cb83..2f513f2 100644 --- a/src/components/MentionableSelectMenuComponent.ts +++ b/src/components/MentionableSelectMenuComponent.ts @@ -1,4 +1,8 @@ import type { ComponentType } from '../common'; -import { BaseSelectMenuComponent } from '../structures/extra/BaseSelectMenuComponent'; +import { BaseSelectMenuComponent } from './BaseSelectMenuComponent'; -export class MentionableSelectMenuComponent extends BaseSelectMenuComponent {} +export class MentionableSelectMenuComponent extends BaseSelectMenuComponent { + get defaultValues() { + return this.data.default_values; + } +} diff --git a/src/components/RoleSelectMenuComponent.ts b/src/components/RoleSelectMenuComponent.ts index 175788a..0f257c7 100644 --- a/src/components/RoleSelectMenuComponent.ts +++ b/src/components/RoleSelectMenuComponent.ts @@ -1,4 +1,8 @@ import type { ComponentType } from '../common'; -import { BaseSelectMenuComponent } from '../structures/extra/BaseSelectMenuComponent'; +import { BaseSelectMenuComponent } from './BaseSelectMenuComponent'; -export class RoleSelectMenuComponent extends BaseSelectMenuComponent {} +export class RoleSelectMenuComponent extends BaseSelectMenuComponent { + get defaultValues() { + return this.data.default_values; + } +} diff --git a/src/components/StringSelectMenuComponent.ts b/src/components/StringSelectMenuComponent.ts index 4bb997c..6ed8071 100644 --- a/src/components/StringSelectMenuComponent.ts +++ b/src/components/StringSelectMenuComponent.ts @@ -1,12 +1,8 @@ -import type { APISelectMenuOption, APIStringSelectComponent, ComponentType } from '../common'; -import { BaseSelectMenuComponent } from '../structures/extra/BaseSelectMenuComponent'; +import type { ComponentType } from '../common'; +import { BaseSelectMenuComponent } from './BaseSelectMenuComponent'; export class StringSelectMenuComponent extends BaseSelectMenuComponent { - constructor(data: APIStringSelectComponent) { - super(data); - - this.options = data.options; + get options() { + return this.data.options; } - - options: APISelectMenuOption[]; } diff --git a/src/components/TextInputComponent.ts b/src/components/TextInputComponent.ts index ebbdd9c..87ec198 100644 --- a/src/components/TextInputComponent.ts +++ b/src/components/TextInputComponent.ts @@ -1,14 +1,36 @@ -import type { APIModalComponent, APITextInputComponent, ComponentType } from '../common'; -import { BaseComponent } from '../structures/extra/BaseComponent'; +import type { ComponentType } from '../common'; +import { BaseComponent } from './BaseComponent'; export class TextInputComponent extends BaseComponent { - constructor(data: APITextInputComponent) { - super(data); - - this.customId = data.custom_id; - this.value = data.value!; + get customId() { + return this.data.custom_id; } - customId: string; - value: string | APIModalComponent; + get value() { + return this.data.value; + } + + get style() { + return this.data.style; + } + + get label() { + return this.data.label; + } + + get max() { + return this.data.max_length; + } + + get min() { + return this.data.min_length; + } + + get required() { + return this.data.required; + } + + get placeholder() { + return this.data.placeholder; + } } diff --git a/src/components/UserSelectMenuComponent.ts b/src/components/UserSelectMenuComponent.ts index 23343bc..4aa7ce5 100644 --- a/src/components/UserSelectMenuComponent.ts +++ b/src/components/UserSelectMenuComponent.ts @@ -1,4 +1,8 @@ import type { ComponentType } from '../common'; -import { BaseSelectMenuComponent } from '../structures/extra/BaseSelectMenuComponent'; +import { BaseSelectMenuComponent } from './BaseSelectMenuComponent'; -export class UserSelectMenuComponent extends BaseSelectMenuComponent {} +export class UserSelectMenuComponent extends BaseSelectMenuComponent { + get defaultValues() { + return this.data.default_values; + } +} diff --git a/src/components/index.ts b/src/components/index.ts index 01243d2..f6958e3 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,6 +1,6 @@ import type { APIMessageActionRowComponent } from '../common'; import { ButtonStyle, ComponentType } from '../common'; -import { BaseComponent } from '../structures/extra/BaseComponent'; +import { BaseComponent } from './BaseComponent'; import { ButtonComponent, LinkButtonComponent } from './ButtonComponent'; import { ChannelSelectMenuComponent } from './ChannelSelectMenuComponent'; import { MentionableSelectMenuComponent } from './MentionableSelectMenuComponent'; diff --git a/src/structures/extra/BaseComponent.ts b/src/structures/extra/BaseComponent.ts deleted file mode 100644 index 74feb52..0000000 --- a/src/structures/extra/BaseComponent.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { APIBaseComponent, ComponentType } from '../../common'; - -export interface BaseComponent extends APIBaseComponent {} - -export class BaseComponent { - constructor(data: APIBaseComponent) { - Object.assign(this, data); - } - - toJSON() { - return { type: this.type }; - } -} diff --git a/src/structures/extra/BaseSelectMenuComponent.ts b/src/structures/extra/BaseSelectMenuComponent.ts deleted file mode 100644 index a61d259..0000000 --- a/src/structures/extra/BaseSelectMenuComponent.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { APIBaseSelectMenuComponent, ComponentType, Identify, ObjectToLower } from '../../common'; -import { BaseComponent } from './BaseComponent'; - -export type APISelectMenuComponentTypes = - | ComponentType.ChannelSelect - | ComponentType.MentionableSelect - | ComponentType.RoleSelect - | ComponentType.StringSelect - | ComponentType.UserSelect; - -export interface BaseSelectMenuComponent - extends BaseComponent, - ObjectToLower, 'type'>>> {} - -export class BaseSelectMenuComponent extends BaseComponent { - constructor(data: APIBaseSelectMenuComponent) { - super(data); - } - - toJSON() { - return { ...this }; - } -}