From 1ebe2e685818e5afd6f5dab3f622a52a942bd3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Tue, 2 Aug 2022 21:34:16 -0400 Subject: [PATCH] closes #89 - Builders to helpers (#91) * chore: builders to helpers * fix: readme --- README.md | 12 +- .../src/builders/slash/ApplicationCommand.ts | 115 ----------------- packages/core/src/structures.ts | 12 +- packages/core/src/utils/util.ts | 4 +- .../builders/components/InputTextBuilder.ts | 4 +- .../components/MessageActionRowBuilder.ts | 6 +- .../components/MessageButtonBuilder.ts | 2 +- .../components/MessageSelectMenuBuilder.ts | 2 +- .../src/builders/embed-builder.ts | 2 +- .../src/builders/slash/ApplicationCommand.ts | 120 ++++++++++++++++++ .../slash/ApplicationCommandOption.ts | 5 +- packages/helpers/src/index.ts | 17 ++- turbo.json | 2 +- 13 files changed, 160 insertions(+), 143 deletions(-) delete mode 100644 packages/core/src/builders/slash/ApplicationCommand.ts rename packages/{core => helpers}/src/builders/components/InputTextBuilder.ts (87%) rename packages/{core => helpers}/src/builders/components/MessageActionRowBuilder.ts (78%) rename packages/{core => helpers}/src/builders/components/MessageButtonBuilder.ts (93%) rename packages/{core => helpers}/src/builders/components/MessageSelectMenuBuilder.ts (97%) rename packages/{core => helpers}/src/builders/embed-builder.ts (96%) create mode 100644 packages/helpers/src/builders/slash/ApplicationCommand.ts rename packages/{core => helpers}/src/builders/slash/ApplicationCommandOption.ts (97%) diff --git a/README.md b/README.md index 33ab99d..79c2b10 100644 --- a/README.md +++ b/README.md @@ -29,17 +29,17 @@ that you should not make software that does things it is not supposed to do. ## Example bot (TS/JS) ```js -import { ChatInputApplicationCommandBuilder, Session } from '@biscuitland/core'; +import { Session } from '@biscuitland/core'; import { GatewayIntents } from '@biscuitland/api-types'; const session = new Session({ token: 'your token', intents: GatewayIntents.Guilds }); const commands = [ - new ChatInputApplicationCommandBuilder() - .setName('ping') - .setDescription('Replies with pong!') - .toJSON() -] + { + name: 'ping', + description: 'Replies with pong!' + } +]; session.events.on('ready', async ({ user }) => { console.log('Logged in as:', user.username); diff --git a/packages/core/src/builders/slash/ApplicationCommand.ts b/packages/core/src/builders/slash/ApplicationCommand.ts deleted file mode 100644 index 97d9bd2..0000000 --- a/packages/core/src/builders/slash/ApplicationCommand.ts +++ /dev/null @@ -1,115 +0,0 @@ -import type { Localization, PermissionStrings } from '@biscuitland/api-types'; -import type { CreateApplicationCommand } from '../../biscuit'; -import { ApplicationCommandTypes } from '@biscuitland/api-types'; -import { OptionBased } from './ApplicationCommandOption'; - -export abstract class ApplicationCommandBuilder { - constructor( - type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput, - name: string = '', - description: string = '', - defaultMemberPermissions?: PermissionStrings[], - nameLocalizations?: Localization, - descriptionLocalizations?: Localization, - dmPermission: boolean = true, - ) { - this.type = type; - this.name = name; - this.description = description; - this.defaultMemberPermissions = defaultMemberPermissions; - this.nameLocalizations = nameLocalizations; - this.descriptionLocalizations = descriptionLocalizations; - this.dmPermission = dmPermission; - } - type: ApplicationCommandTypes; - name: string; - description: string; - defaultMemberPermissions?: PermissionStrings[]; - nameLocalizations?: Localization; - descriptionLocalizations?: Localization; - dmPermission: boolean; - - setType(type: ApplicationCommandTypes): this { - return (this.type = type), this; - } - - setName(name: string): this { - return (this.name = name), this; - } - - setDescription(description: string): this { - return (this.description = description), this; - } - - setDefaultMemberPermission(perm: PermissionStrings[]): this { - return (this.defaultMemberPermissions = perm), this; - } - - setNameLocalizations(l: Localization): this { - return (this.nameLocalizations = l), this; - } - - setDescriptionLocalizations(l: Localization): this { - return (this.descriptionLocalizations = l), this; - } - - setDmPermission(perm: boolean): this { - return (this.dmPermission = perm), this; - } -} - -export type MessageApplicationCommandBuilderJSON = { name: string; type: ApplicationCommandTypes.Message }; - -export class MessageApplicationCommandBuilder { - type: ApplicationCommandTypes; - name?: string; - constructor( - type?: ApplicationCommandTypes, - name?: string, - ) { - this.type = type ?? ApplicationCommandTypes.Message; - this.name = name; - } - - setName(name: string): this { - return (this.name = name), this; - } - - toJSON(): MessageApplicationCommandBuilderJSON { - if (!this.name) throw new TypeError('Propety \'name\' is required'); - - return { - type: ApplicationCommandTypes.Message, - name: this.name, - }; - } -} - -export class ChatInputApplicationCommandBuilder extends ApplicationCommandBuilder { - type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput; - - toJSON(): CreateApplicationCommand { - if (!this.type) throw new TypeError('Propety \'type\' is required'); - if (!this.name) throw new TypeError('Propety \'name\' is required'); - if (!this.description) { - throw new TypeError('Propety \'description\' is required'); - } - - return { - type: ApplicationCommandTypes.ChatInput, - name: this.name, - description: this.description, - options: this.options?.map((o) => o.toJSON()) ?? [], - default_member_permissions: this.defaultMemberPermissions, - name_localizations: this.nameLocalizations, - description_localizations: this.descriptionLocalizations, - dm_permission: this.dmPermission, - }; - } -} - -OptionBased.applyTo(ChatInputApplicationCommandBuilder); - -export interface ChatInputApplicationCommandBuilder extends ApplicationCommandBuilder, OptionBased { - // pass -} diff --git a/packages/core/src/structures.ts b/packages/core/src/structures.ts index 8bf2816..61f6406 100644 --- a/packages/core/src/structures.ts +++ b/packages/core/src/structures.ts @@ -34,9 +34,9 @@ export * from './structures/components'; export * from './structures/guilds'; // BUILDERS -export * from './builders/components/InputTextBuilder'; -export * from './builders/components/MessageActionRowBuilder'; -export * from './builders/components/MessageButtonBuilder'; -export * from './builders/components/MessageSelectMenuBuilder'; -export * from './builders/slash/ApplicationCommand'; -export * from './builders/slash/ApplicationCommandOption'; +export * from '../../helpers/src/builders/components/InputTextBuilder'; +export * from '../../helpers/src/builders/components/MessageActionRowBuilder'; +export * from '../../helpers/src/builders/components/MessageButtonBuilder'; +export * from '../../helpers/src/builders/components/MessageSelectMenuBuilder'; +export * from '../../helpers/src/builders/slash/ApplicationCommand'; +export * from '../../helpers/src/builders/slash/ApplicationCommandOption'; diff --git a/packages/core/src/utils/util.ts b/packages/core/src/utils/util.ts index 2aa8557..9a49ed2 100644 --- a/packages/core/src/utils/util.ts +++ b/packages/core/src/utils/util.ts @@ -1,6 +1,4 @@ -import type { ButtonBuilder } from '../builders/components/MessageButtonBuilder'; -import type { InputTextBuilder } from '../builders/components/InputTextBuilder'; -import type { SelectMenuBuilder } from '../builders/components/MessageSelectMenuBuilder'; +import type { SelectMenuBuilder, InputTextBuilder, ButtonBuilder } from '@biscuitland/helpers'; import type { Permissions } from '../structures/special/permissions'; import type { Snowflake } from '../snowflakes'; diff --git a/packages/core/src/builders/components/InputTextBuilder.ts b/packages/helpers/src/builders/components/InputTextBuilder.ts similarity index 87% rename from packages/core/src/builders/components/InputTextBuilder.ts rename to packages/helpers/src/builders/components/InputTextBuilder.ts index 45f8d92..9dd9757 100644 --- a/packages/core/src/builders/components/InputTextBuilder.ts +++ b/packages/helpers/src/builders/components/InputTextBuilder.ts @@ -1,9 +1,9 @@ -import type { DiscordInputTextComponent, MessageComponentTypes, TextStyles } from '@biscuitland/api-types'; +import { DiscordInputTextComponent, MessageComponentTypes, TextStyles } from '@biscuitland/api-types'; export class InputTextBuilder { constructor() { this.#data = {} as DiscordInputTextComponent; - this.type = 4; + this.type = MessageComponentTypes.InputText; } #data: DiscordInputTextComponent; type: MessageComponentTypes.InputText; diff --git a/packages/core/src/builders/components/MessageActionRowBuilder.ts b/packages/helpers/src/builders/components/MessageActionRowBuilder.ts similarity index 78% rename from packages/core/src/builders/components/MessageActionRowBuilder.ts rename to packages/helpers/src/builders/components/MessageActionRowBuilder.ts index 3f066ff..d20d281 100644 --- a/packages/core/src/builders/components/MessageActionRowBuilder.ts +++ b/packages/helpers/src/builders/components/MessageActionRowBuilder.ts @@ -1,10 +1,10 @@ -import type { DiscordActionRow, MessageComponentTypes } from '@biscuitland/api-types'; -import type { ComponentBuilder } from '../../utils/util'; +import { DiscordActionRow, MessageComponentTypes } from '@biscuitland/api-types'; +import type { ComponentBuilder } from '../../../../core/src/utils/util'; export class ActionRowBuilder { constructor() { this.components = [] as T[]; - this.type = 1; + this.type = MessageComponentTypes.ActionRow; } components: T[]; type: MessageComponentTypes.ActionRow; diff --git a/packages/core/src/builders/components/MessageButtonBuilder.ts b/packages/helpers/src/builders/components/MessageButtonBuilder.ts similarity index 93% rename from packages/core/src/builders/components/MessageButtonBuilder.ts rename to packages/helpers/src/builders/components/MessageButtonBuilder.ts index f2408be..db7de46 100644 --- a/packages/core/src/builders/components/MessageButtonBuilder.ts +++ b/packages/helpers/src/builders/components/MessageButtonBuilder.ts @@ -1,5 +1,5 @@ import { ButtonStyles, DiscordButtonComponent, MessageComponentTypes } from '@biscuitland/api-types'; -import type { ComponentEmoji } from '../../utils/util'; +import type { ComponentEmoji } from '../../../../core/src/utils/util'; export class ButtonBuilder { constructor() { diff --git a/packages/core/src/builders/components/MessageSelectMenuBuilder.ts b/packages/helpers/src/builders/components/MessageSelectMenuBuilder.ts similarity index 97% rename from packages/core/src/builders/components/MessageSelectMenuBuilder.ts rename to packages/helpers/src/builders/components/MessageSelectMenuBuilder.ts index edf419a..a687c3d 100644 --- a/packages/core/src/builders/components/MessageSelectMenuBuilder.ts +++ b/packages/helpers/src/builders/components/MessageSelectMenuBuilder.ts @@ -1,5 +1,5 @@ import type { DiscordSelectOption, DiscordSelectMenuComponent, } from '@biscuitland/api-types'; -import type { ComponentEmoji } from '../../utils/util'; +import type { ComponentEmoji } from '../../../../core/src/utils/util'; import { MessageComponentTypes } from '@biscuitland/api-types'; export class SelectMenuOptionBuilder { diff --git a/packages/core/src/builders/embed-builder.ts b/packages/helpers/src/builders/embed-builder.ts similarity index 96% rename from packages/core/src/builders/embed-builder.ts rename to packages/helpers/src/builders/embed-builder.ts index 49bfbee..f1b321d 100644 --- a/packages/core/src/builders/embed-builder.ts +++ b/packages/helpers/src/builders/embed-builder.ts @@ -1,4 +1,4 @@ -import type { DiscordEmbed, DiscordEmbedField, DiscordEmbedProvider } from '@biscuitland/api-types'; +import { DiscordEmbed, DiscordEmbedField, DiscordEmbedProvider } from '@biscuitland/api-types'; export interface EmbedFooter { text: string; diff --git a/packages/helpers/src/builders/slash/ApplicationCommand.ts b/packages/helpers/src/builders/slash/ApplicationCommand.ts new file mode 100644 index 0000000..22ca3a8 --- /dev/null +++ b/packages/helpers/src/builders/slash/ApplicationCommand.ts @@ -0,0 +1,120 @@ +import { + Localization, + PermissionStrings, + ApplicationCommandTypes +} from '@biscuitland/api-types'; +import { CreateApplicationCommand } from '@biscuitland/core'; +import { OptionBased } from './ApplicationCommandOption'; + +export abstract class ApplicationCommandBuilder { + constructor( + type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput, + name: string = '', + description: string = '', + defaultMemberPermissions?: PermissionStrings[], + nameLocalizations?: Localization, + descriptionLocalizations?: Localization, + dmPermission: boolean = true + ) { + this.type = type; + this.name = name; + this.description = description; + this.defaultMemberPermissions = defaultMemberPermissions; + this.nameLocalizations = nameLocalizations; + this.descriptionLocalizations = descriptionLocalizations; + this.dmPermission = dmPermission; + } + type: ApplicationCommandTypes; + name: string; + description: string; + defaultMemberPermissions?: PermissionStrings[]; + nameLocalizations?: Localization; + descriptionLocalizations?: Localization; + dmPermission: boolean; + + setType(type: ApplicationCommandTypes): this { + return (this.type = type), this; + } + + setName(name: string): this { + return (this.name = name), this; + } + + setDescription(description: string): this { + return (this.description = description), this; + } + + setDefaultMemberPermission(perm: PermissionStrings[]): this { + return (this.defaultMemberPermissions = perm), this; + } + + setNameLocalizations(l: Localization): this { + return (this.nameLocalizations = l), this; + } + + setDescriptionLocalizations(l: Localization): this { + return (this.descriptionLocalizations = l), this; + } + + setDmPermission(perm: boolean): this { + return (this.dmPermission = perm), this; + } +} + +export type MessageApplicationCommandBuilderJSON = { + name: string; + type: ApplicationCommandTypes.Message; +}; + +export class MessageApplicationCommandBuilder { + type: ApplicationCommandTypes; + name?: string; + constructor(type?: ApplicationCommandTypes, name?: string) { + this.type = type ?? ApplicationCommandTypes.Message; + this.name = name; + } + + setName(name: string): this { + return (this.name = name), this; + } + + toJSON(): MessageApplicationCommandBuilderJSON { + if (!this.name) throw new TypeError("Propety 'name' is required"); + + return { + type: ApplicationCommandTypes.Message, + name: this.name + }; + } +} + +export class ChatInputApplicationCommandBuilder extends ApplicationCommandBuilder { + type: ApplicationCommandTypes.ChatInput = ApplicationCommandTypes.ChatInput; + + toJSON(): CreateApplicationCommand { + if (!this.type) throw new TypeError("Propety 'type' is required"); + if (!this.name) throw new TypeError("Propety 'name' is required"); + if (!this.description) { + throw new TypeError("Propety 'description' is required"); + } + + return { + type: ApplicationCommandTypes.ChatInput, + name: this.name, + description: this.description, + options: this.options?.map(o => o.toJSON()) ?? [], + default_member_permissions: this.defaultMemberPermissions, + name_localizations: this.nameLocalizations, + description_localizations: this.descriptionLocalizations, + dm_permission: this.dmPermission + }; + } +} + +OptionBased.applyTo(ChatInputApplicationCommandBuilder); + +export interface ChatInputApplicationCommandBuilder + extends ApplicationCommandBuilder, + OptionBased { + // pass +} diff --git a/packages/core/src/builders/slash/ApplicationCommandOption.ts b/packages/helpers/src/builders/slash/ApplicationCommandOption.ts similarity index 97% rename from packages/core/src/builders/slash/ApplicationCommandOption.ts rename to packages/helpers/src/builders/slash/ApplicationCommandOption.ts index 0637cfd..1d84dd2 100644 --- a/packages/core/src/builders/slash/ApplicationCommandOption.ts +++ b/packages/helpers/src/builders/slash/ApplicationCommandOption.ts @@ -1,6 +1,5 @@ -import type { ChannelTypes, Localization, Locales } from '@biscuitland/api-types'; -import { ApplicationCommandOptionTypes } from '@biscuitland/api-types'; -import { ApplicationCommandOptionChoice } from '../../structures/interactions'; +import { ChannelTypes, Localization, Locales, ApplicationCommandOptionTypes } from '@biscuitland/api-types'; +import { ApplicationCommandOptionChoice } from '@biscuitland/core'; export type Localizations = typeof Locales[keyof typeof Locales] & string; diff --git a/packages/helpers/src/index.ts b/packages/helpers/src/index.ts index ca84f07..d093f0c 100644 --- a/packages/helpers/src/index.ts +++ b/packages/helpers/src/index.ts @@ -1 +1,16 @@ -export * from './collectors'; \ No newline at end of file +export * from './collectors'; + +/** Builders */ + +// Components +export * from './builders/components/InputTextBuilder'; +export * from './builders/components/MessageActionRowBuilder'; +export * from './builders/components/MessageButtonBuilder'; +export * from './builders/components/MessageSelectMenuBuilder'; + +// Slash +export * from './builders/slash/ApplicationCommand'; +export * from './builders/slash/ApplicationCommandOption'; + +// Embed +export * from './builders/embed-builder'; \ No newline at end of file diff --git a/turbo.json b/turbo.json index cfe7b36..79829b1 100644 --- a/turbo.json +++ b/turbo.json @@ -17,7 +17,7 @@ "@biscuitland/core#build": { "dependsOn": ["@biscuitland/api-types#build", "@biscuitland/rest#build", "@biscuitland/ws#build"] }, - "@biscuitland/extra#build": { + "@biscuitland/helpers#build": { "dependsOn": ["@biscuitland/core#build"] }, "clean": {