@lunibee/builders 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,58 @@
1
- # @lunibee/builders
2
-
3
- > Part of the lunibee ecosystem.
4
-
5
- ## Basic use
6
-
7
- ```ts
8
- import { ... } from '@lunibee/builders';
9
-
10
- // Example basic usage
11
- ```
1
+ # @lunibee/builders
2
+
3
+ > Fluent, validated builders for embeds, components, modals, commands and attachments.
4
+
5
+ ```bash
6
+ bun add @lunibee/builders
7
+ ```
8
+
9
+ ```ts
10
+ import {
11
+ EmbedBuilder,
12
+ ActionRowBuilder,
13
+ ButtonBuilder,
14
+ ButtonStyle,
15
+ StringSelectMenuBuilder,
16
+ SlashCommandBuilder,
17
+ } from "@lunibee/builders";
18
+
19
+ const embed = new EmbedBuilder()
20
+ .setTitle("Welcome")
21
+ .setDescription("Pick a role below.")
22
+ .setColor(0xf5c542)
23
+ .addFields([{ name: "Server", value: "Lunibee" }]);
24
+
25
+ const buttons = new ActionRowBuilder().addComponents(
26
+ new ButtonBuilder().setCustomId("yes").setLabel("Yes").setStyle(ButtonStyle.Success),
27
+ );
28
+
29
+ const menu = new ActionRowBuilder().addComponents(
30
+ new StringSelectMenuBuilder()
31
+ .setCustomId("role")
32
+ .addOptions({ label: "Developer", value: "dev" }),
33
+ );
34
+
35
+ const ping = new SlashCommandBuilder()
36
+ .setName("ping")
37
+ .setDescription("Replies with pong")
38
+ .addStringOption((option) => option.setName("message").setDescription("Echo text"));
39
+
40
+ console.log(embed.toJSON(), buttons.toJSON(), menu.toJSON(), ping.toJSON());
41
+ ```
42
+
43
+ ## Builders
44
+
45
+ | Area | Builders |
46
+ |---|---|
47
+ | Embeds | `EmbedBuilder` |
48
+ | Buttons & rows | `ButtonBuilder`, `ActionRowBuilder` |
49
+ | Select menus | `StringSelectBuilder` (alias `StringSelectMenuBuilder`), `EntitySelectBuilder`, `UserSelectMenuBuilder`, `RoleSelectMenuBuilder`, `ChannelSelectMenuBuilder`, `MentionableSelectMenuBuilder` |
50
+ | Modals | `ModalBuilder`, `TextInputBuilder` |
51
+ | Slash commands | `SlashCommandBuilder`, `SubcommandBuilder`, `SubcommandGroupBuilder`, option builders |
52
+ | Context menus | `ContextMenuCommandBuilder` (`setType(2 \| 3)`), `UserCommandBuilder`, `MessageCommandBuilder` |
53
+ | Components V2 | `ContainerBuilder`, `SectionBuilder`, `TextDisplayBuilder`, `MediaGalleryBuilder`, `FileComponentBuilder`, `SeparatorBuilder`, `ThumbnailBuilder` |
54
+ | Files | `AttachmentBuilder` |
55
+
56
+ Every builder validates Discord's limits as you set values and serialises with `toJSON()`.
57
+
58
+ Docs: https://lunibee.js.org/core-concepts/builders/
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
- {
2
- "name": "@lunibee/builders",
3
- "version": "0.1.7",
4
- "type": "module",
5
- "module": "./src/index.ts",
6
- "types": "./src/index.ts",
7
- "exports": {
8
- ".": "./src/index.ts"
9
- }
10
- }
1
+ {
2
+ "name": "@lunibee/builders",
3
+ "version": "0.1.8",
4
+ "type": "module",
5
+ "module": "./src/index.ts",
6
+ "types": "./src/index.ts",
7
+ "exports": {
8
+ ".": "./src/index.ts"
9
+ }
10
+ }
package/src/attachment.ts CHANGED
@@ -1,68 +1,70 @@
1
- import { readFile } from "node:fs/promises";
2
- import { basename } from "node:path";
3
-
4
- export interface AttachmentData {
5
- name: string;
6
- description?: string;
7
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
8
- }
9
-
10
- /** Builder for message attachments and file uploads. */
11
- export class AttachmentBuilder {
12
- public name: string;
13
- public description?: string;
14
- public file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
15
-
16
- public constructor(
17
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
18
- data?: Partial<Omit<AttachmentData, "file">> | string,
19
- ) {
20
- this.file = file;
21
- if (typeof data === "string") {
22
- this.name = data;
23
- } else {
24
- this.name =
25
- data?.name ??
26
- (typeof file === "string" ? basename(file) : "file.bin");
27
- this.description = data?.description;
28
- }
29
- }
30
-
31
- /** Sets the attachment filename. */
32
- public setName(name: string): this {
33
- this.name = name;
34
- return this;
35
- }
36
-
37
- /** Sets the attachment description / alt text. */
38
- public setDescription(description: string): this {
39
- this.description = description;
40
- return this;
41
- }
42
-
43
- /** Sets the file contents. */
44
- public setFile(
45
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
46
- ): this {
47
- this.file = file;
48
- return this;
49
- }
50
-
51
- /** Resolves the attachment payload to a binary Uint8Array. */
52
- public async toBuffer(): Promise<Uint8Array> {
53
- if (typeof this.file === "string") {
54
- const buffer = await readFile(this.file);
55
- return new Uint8Array(buffer);
56
- }
57
- if (this.file instanceof Uint8Array) return this.file;
58
- if (this.file instanceof ArrayBuffer) return new Uint8Array(this.file);
59
- if (this.file instanceof Blob) {
60
- const buf = await this.file.arrayBuffer();
61
- return new Uint8Array(buf);
62
- }
63
- if (typeof Buffer !== "undefined" && Buffer.isBuffer(this.file)) {
64
- return new Uint8Array(this.file);
65
- }
66
- throw new TypeError("Attachment file must be a supported binary value or file path.");
67
- }
68
- }
1
+ import { readFile } from "node:fs/promises";
2
+ import { basename } from "node:path";
3
+
4
+ export interface AttachmentData {
5
+ name: string;
6
+ description?: string;
7
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
8
+ }
9
+
10
+ /** Builder for message attachments and file uploads. */
11
+ export class AttachmentBuilder {
12
+ public name: string;
13
+ public description?: string;
14
+ public file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
15
+
16
+ public constructor(
17
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
18
+ data?: Partial<Omit<AttachmentData, "file">> | string,
19
+ ) {
20
+ this.file = file;
21
+ if (typeof data === "string") {
22
+ this.name = data;
23
+ } else {
24
+ this.name =
25
+ data?.name ??
26
+ (typeof file === "string" ? basename(file) : "file.bin");
27
+ this.description = data?.description;
28
+ }
29
+ }
30
+
31
+ /** Sets the attachment filename. */
32
+ public setName(name: string): this {
33
+ this.name = name;
34
+ return this;
35
+ }
36
+
37
+ /** Sets the attachment description / alt text. */
38
+ public setDescription(description: string): this {
39
+ this.description = description;
40
+ return this;
41
+ }
42
+
43
+ /** Sets the file contents. */
44
+ public setFile(
45
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
46
+ ): this {
47
+ this.file = file;
48
+ return this;
49
+ }
50
+
51
+ /** Resolves the attachment payload to a binary Uint8Array. */
52
+ public async toBuffer(): Promise<Uint8Array> {
53
+ if (typeof this.file === "string") {
54
+ const buffer = await readFile(this.file);
55
+ return new Uint8Array(buffer);
56
+ }
57
+ if (this.file instanceof Uint8Array) return this.file;
58
+ if (this.file instanceof ArrayBuffer) return new Uint8Array(this.file);
59
+ if (this.file instanceof Blob) {
60
+ const buf = await this.file.arrayBuffer();
61
+ return new Uint8Array(buf);
62
+ }
63
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(this.file)) {
64
+ return new Uint8Array(this.file);
65
+ }
66
+ throw new TypeError(
67
+ "Attachment file must be a supported binary value or file path.",
68
+ );
69
+ }
70
+ }