closes #89 - Builders to helpers (#91)

* chore: builders to helpers

* fix: readme
This commit is contained in:
Marcos Susaña 2022-08-02 21:34:16 -04:00 committed by GitHub
parent 18bcc32868
commit 1ebe2e6858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 160 additions and 143 deletions

View File

@ -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);

View File

@ -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
}

View File

@ -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';

View File

@ -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';

View File

@ -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;

View File

@ -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<T extends ComponentBuilder> {
constructor() {
this.components = [] as T[];
this.type = 1;
this.type = MessageComponentTypes.ActionRow;
}
components: T[];
type: MessageComponentTypes.ActionRow;

View File

@ -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() {

View File

@ -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 {

View File

@ -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;

View File

@ -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
}

View File

@ -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;

View File

@ -1 +1,16 @@
export * from './collectors';
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';

View File

@ -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": {