@fluxerjs/builders 1.0.2
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/dist/index.d.mts +95 -0
- package/dist/index.d.ts +95 -0
- package/dist/index.js +268 -0
- package/dist/index.mjs +239 -0
- package/package.json +33 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { APIEmbed, APIMessageReference } from '@fluxerjs/types';
|
|
2
|
+
|
|
3
|
+
interface EmbedAuthorOptions {
|
|
4
|
+
name: string;
|
|
5
|
+
iconURL?: string;
|
|
6
|
+
url?: string;
|
|
7
|
+
}
|
|
8
|
+
interface EmbedFooterOptions {
|
|
9
|
+
text: string;
|
|
10
|
+
iconURL?: string;
|
|
11
|
+
}
|
|
12
|
+
interface EmbedFieldData {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
inline?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare class EmbedBuilder {
|
|
18
|
+
readonly data: Partial<APIEmbed>;
|
|
19
|
+
setTitle(title: string | null): this;
|
|
20
|
+
setDescription(description: string | null): this;
|
|
21
|
+
setURL(url: string | null): this;
|
|
22
|
+
setColor(color: number | string | [number, number, number] | null): this;
|
|
23
|
+
setTimestamp(timestamp?: Date | number | null): this;
|
|
24
|
+
setAuthor(options: EmbedAuthorOptions | null): this;
|
|
25
|
+
setFooter(options: EmbedFooterOptions | null): this;
|
|
26
|
+
setImage(url: string | null): this;
|
|
27
|
+
setThumbnail(url: string | null): this;
|
|
28
|
+
setVideo(url: string | null): this;
|
|
29
|
+
addFields(...fields: EmbedFieldData[]): this;
|
|
30
|
+
spliceFields(index: number, deleteCount: number, ...fields: EmbedFieldData[]): this;
|
|
31
|
+
toJSON(): APIEmbed;
|
|
32
|
+
static from(data: APIEmbed): EmbedBuilder;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Builder for message attachment metadata (filename, description, spoiler).
|
|
37
|
+
* Actual file data is passed separately when sending (e.g. FormData).
|
|
38
|
+
*/
|
|
39
|
+
interface AttachmentPayloadOptions {
|
|
40
|
+
name: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
spoiler?: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface APIAttachmentPayload {
|
|
45
|
+
id: number;
|
|
46
|
+
filename: string;
|
|
47
|
+
description?: string | null;
|
|
48
|
+
}
|
|
49
|
+
declare class AttachmentBuilder {
|
|
50
|
+
readonly id: number;
|
|
51
|
+
filename: string;
|
|
52
|
+
description?: string | null;
|
|
53
|
+
spoiler: boolean;
|
|
54
|
+
constructor(id: number, filename: string, options?: Partial<AttachmentPayloadOptions>);
|
|
55
|
+
setName(name: string): this;
|
|
56
|
+
setDescription(description: string | null): this;
|
|
57
|
+
setSpoiler(spoiler?: boolean): this;
|
|
58
|
+
toJSON(): APIAttachmentPayload;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface MessagePayloadData {
|
|
62
|
+
content?: string | null;
|
|
63
|
+
embeds?: APIEmbed[] | null;
|
|
64
|
+
attachments?: Array<{
|
|
65
|
+
id: number;
|
|
66
|
+
filename: string;
|
|
67
|
+
description?: string | null;
|
|
68
|
+
}>;
|
|
69
|
+
message_reference?: APIMessageReference | null;
|
|
70
|
+
tts?: boolean;
|
|
71
|
+
flags?: number;
|
|
72
|
+
}
|
|
73
|
+
declare class MessagePayload {
|
|
74
|
+
static readonly ContentMaxLength = 2000;
|
|
75
|
+
readonly data: MessagePayloadData;
|
|
76
|
+
setContent(content: string | null): this;
|
|
77
|
+
setEmbeds(embeds: (APIEmbed | EmbedBuilder)[] | null): this;
|
|
78
|
+
addEmbed(embed: APIEmbed | EmbedBuilder): this;
|
|
79
|
+
setAttachments(attachments: Array<AttachmentBuilder | {
|
|
80
|
+
id: number;
|
|
81
|
+
filename: string;
|
|
82
|
+
description?: string | null;
|
|
83
|
+
}> | null): this;
|
|
84
|
+
setReply(reference: {
|
|
85
|
+
channel_id: string;
|
|
86
|
+
message_id: string;
|
|
87
|
+
guild_id?: string | null;
|
|
88
|
+
} | APIMessageReference | null): this;
|
|
89
|
+
setTTS(tts: boolean): this;
|
|
90
|
+
setFlags(flags: number): this;
|
|
91
|
+
toJSON(): MessagePayloadData;
|
|
92
|
+
static create(contentOrOptions?: string | MessagePayloadData): MessagePayload;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { type APIAttachmentPayload, AttachmentBuilder, type AttachmentPayloadOptions, type EmbedAuthorOptions, EmbedBuilder, type EmbedFieldData, type EmbedFooterOptions, MessagePayload, type MessagePayloadData };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { APIEmbed, APIMessageReference } from '@fluxerjs/types';
|
|
2
|
+
|
|
3
|
+
interface EmbedAuthorOptions {
|
|
4
|
+
name: string;
|
|
5
|
+
iconURL?: string;
|
|
6
|
+
url?: string;
|
|
7
|
+
}
|
|
8
|
+
interface EmbedFooterOptions {
|
|
9
|
+
text: string;
|
|
10
|
+
iconURL?: string;
|
|
11
|
+
}
|
|
12
|
+
interface EmbedFieldData {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string;
|
|
15
|
+
inline?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare class EmbedBuilder {
|
|
18
|
+
readonly data: Partial<APIEmbed>;
|
|
19
|
+
setTitle(title: string | null): this;
|
|
20
|
+
setDescription(description: string | null): this;
|
|
21
|
+
setURL(url: string | null): this;
|
|
22
|
+
setColor(color: number | string | [number, number, number] | null): this;
|
|
23
|
+
setTimestamp(timestamp?: Date | number | null): this;
|
|
24
|
+
setAuthor(options: EmbedAuthorOptions | null): this;
|
|
25
|
+
setFooter(options: EmbedFooterOptions | null): this;
|
|
26
|
+
setImage(url: string | null): this;
|
|
27
|
+
setThumbnail(url: string | null): this;
|
|
28
|
+
setVideo(url: string | null): this;
|
|
29
|
+
addFields(...fields: EmbedFieldData[]): this;
|
|
30
|
+
spliceFields(index: number, deleteCount: number, ...fields: EmbedFieldData[]): this;
|
|
31
|
+
toJSON(): APIEmbed;
|
|
32
|
+
static from(data: APIEmbed): EmbedBuilder;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Builder for message attachment metadata (filename, description, spoiler).
|
|
37
|
+
* Actual file data is passed separately when sending (e.g. FormData).
|
|
38
|
+
*/
|
|
39
|
+
interface AttachmentPayloadOptions {
|
|
40
|
+
name: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
spoiler?: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface APIAttachmentPayload {
|
|
45
|
+
id: number;
|
|
46
|
+
filename: string;
|
|
47
|
+
description?: string | null;
|
|
48
|
+
}
|
|
49
|
+
declare class AttachmentBuilder {
|
|
50
|
+
readonly id: number;
|
|
51
|
+
filename: string;
|
|
52
|
+
description?: string | null;
|
|
53
|
+
spoiler: boolean;
|
|
54
|
+
constructor(id: number, filename: string, options?: Partial<AttachmentPayloadOptions>);
|
|
55
|
+
setName(name: string): this;
|
|
56
|
+
setDescription(description: string | null): this;
|
|
57
|
+
setSpoiler(spoiler?: boolean): this;
|
|
58
|
+
toJSON(): APIAttachmentPayload;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface MessagePayloadData {
|
|
62
|
+
content?: string | null;
|
|
63
|
+
embeds?: APIEmbed[] | null;
|
|
64
|
+
attachments?: Array<{
|
|
65
|
+
id: number;
|
|
66
|
+
filename: string;
|
|
67
|
+
description?: string | null;
|
|
68
|
+
}>;
|
|
69
|
+
message_reference?: APIMessageReference | null;
|
|
70
|
+
tts?: boolean;
|
|
71
|
+
flags?: number;
|
|
72
|
+
}
|
|
73
|
+
declare class MessagePayload {
|
|
74
|
+
static readonly ContentMaxLength = 2000;
|
|
75
|
+
readonly data: MessagePayloadData;
|
|
76
|
+
setContent(content: string | null): this;
|
|
77
|
+
setEmbeds(embeds: (APIEmbed | EmbedBuilder)[] | null): this;
|
|
78
|
+
addEmbed(embed: APIEmbed | EmbedBuilder): this;
|
|
79
|
+
setAttachments(attachments: Array<AttachmentBuilder | {
|
|
80
|
+
id: number;
|
|
81
|
+
filename: string;
|
|
82
|
+
description?: string | null;
|
|
83
|
+
}> | null): this;
|
|
84
|
+
setReply(reference: {
|
|
85
|
+
channel_id: string;
|
|
86
|
+
message_id: string;
|
|
87
|
+
guild_id?: string | null;
|
|
88
|
+
} | APIMessageReference | null): this;
|
|
89
|
+
setTTS(tts: boolean): this;
|
|
90
|
+
setFlags(flags: number): this;
|
|
91
|
+
toJSON(): MessagePayloadData;
|
|
92
|
+
static create(contentOrOptions?: string | MessagePayloadData): MessagePayload;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { type APIAttachmentPayload, AttachmentBuilder, type AttachmentPayloadOptions, type EmbedAuthorOptions, EmbedBuilder, type EmbedFieldData, type EmbedFooterOptions, MessagePayload, type MessagePayloadData };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AttachmentBuilder: () => AttachmentBuilder,
|
|
24
|
+
EmbedBuilder: () => EmbedBuilder,
|
|
25
|
+
MessagePayload: () => MessagePayload
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/messages/EmbedBuilder.ts
|
|
30
|
+
var import_util = require("@fluxerjs/util");
|
|
31
|
+
var EMBED_MAX = {
|
|
32
|
+
title: 256,
|
|
33
|
+
description: 4096,
|
|
34
|
+
fields: 25,
|
|
35
|
+
fieldName: 256,
|
|
36
|
+
fieldValue: 1024,
|
|
37
|
+
footerText: 2048,
|
|
38
|
+
authorName: 256,
|
|
39
|
+
total: 6e3
|
|
40
|
+
};
|
|
41
|
+
var EmbedBuilder = class _EmbedBuilder {
|
|
42
|
+
data = {};
|
|
43
|
+
setTitle(title) {
|
|
44
|
+
if (title !== null && title.length > EMBED_MAX.title) throw new RangeError(`Title must be \u2264${EMBED_MAX.title} characters`);
|
|
45
|
+
this.data.title = title ?? void 0;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
setDescription(description) {
|
|
49
|
+
if (description !== null && description.length > EMBED_MAX.description) throw new RangeError(`Description must be \u2264${EMBED_MAX.description} characters`);
|
|
50
|
+
this.data.description = description ?? void 0;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
setURL(url) {
|
|
54
|
+
this.data.url = url ?? void 0;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
setColor(color) {
|
|
58
|
+
if (color === null) {
|
|
59
|
+
this.data.color = void 0;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
this.data.color = typeof color === "number" ? color : (0, import_util.resolveColor)(color);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
setTimestamp(timestamp) {
|
|
66
|
+
if (timestamp === void 0 || timestamp === null) {
|
|
67
|
+
this.data.timestamp = void 0;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
const date = timestamp instanceof Date ? timestamp : new Date(timestamp);
|
|
71
|
+
this.data.timestamp = date.toISOString();
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
setAuthor(options) {
|
|
75
|
+
if (!options) {
|
|
76
|
+
this.data.author = void 0;
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
const name = options.name.slice(0, EMBED_MAX.authorName);
|
|
80
|
+
const author = { name };
|
|
81
|
+
if (options.url) author.url = options.url;
|
|
82
|
+
if (options.iconURL) author.icon_url = options.iconURL;
|
|
83
|
+
this.data.author = author;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
setFooter(options) {
|
|
87
|
+
if (!options) {
|
|
88
|
+
this.data.footer = void 0;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
const text = options.text.slice(0, EMBED_MAX.footerText);
|
|
92
|
+
const footer = { text };
|
|
93
|
+
if (options.iconURL) footer.icon_url = options.iconURL;
|
|
94
|
+
this.data.footer = footer;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
setImage(url) {
|
|
98
|
+
this.data.image = url ? { url } : void 0;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
setThumbnail(url) {
|
|
102
|
+
this.data.thumbnail = url ? { url } : void 0;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
setVideo(url) {
|
|
106
|
+
this.data.video = url ? { url } : void 0;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
addFields(...fields) {
|
|
110
|
+
const current = (this.data.fields ?? []).slice();
|
|
111
|
+
for (const f of fields) {
|
|
112
|
+
if (current.length >= EMBED_MAX.fields) break;
|
|
113
|
+
current.push({
|
|
114
|
+
name: f.name.slice(0, EMBED_MAX.fieldName),
|
|
115
|
+
value: f.value.slice(0, EMBED_MAX.fieldValue),
|
|
116
|
+
inline: f.inline
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
this.data.fields = current.length ? current : void 0;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
spliceFields(index, deleteCount, ...fields) {
|
|
123
|
+
const current = (this.data.fields ?? []).slice();
|
|
124
|
+
const toAdd = fields.map((f) => ({
|
|
125
|
+
name: f.name.slice(0, EMBED_MAX.fieldName),
|
|
126
|
+
value: f.value.slice(0, EMBED_MAX.fieldValue),
|
|
127
|
+
inline: f.inline
|
|
128
|
+
}));
|
|
129
|
+
current.splice(index, deleteCount, ...toAdd);
|
|
130
|
+
this.data.fields = current.length ? current : void 0;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
toJSON() {
|
|
134
|
+
const totalLength = [this.data.title, this.data.description, ...(this.data.fields ?? []).flatMap((f) => [f.name, f.value]), this.data.footer?.text].filter(Boolean).join("").length;
|
|
135
|
+
if (totalLength > EMBED_MAX.total) throw new RangeError(`Embed total length must be \u2264${EMBED_MAX.total}`);
|
|
136
|
+
return { ...this.data, type: this.data.type ?? "rich" };
|
|
137
|
+
}
|
|
138
|
+
static from(data) {
|
|
139
|
+
const b = new _EmbedBuilder();
|
|
140
|
+
b.data.title = data.title ?? void 0;
|
|
141
|
+
b.data.description = data.description ?? void 0;
|
|
142
|
+
b.data.url = data.url ?? void 0;
|
|
143
|
+
b.data.color = data.color ?? void 0;
|
|
144
|
+
b.data.timestamp = data.timestamp ?? void 0;
|
|
145
|
+
b.data.author = data.author ?? void 0;
|
|
146
|
+
b.data.footer = data.footer ?? void 0;
|
|
147
|
+
b.data.image = data.image ?? void 0;
|
|
148
|
+
b.data.thumbnail = data.thumbnail ?? void 0;
|
|
149
|
+
b.data.fields = data.fields ?? void 0;
|
|
150
|
+
b.data.type = data.type ?? "rich";
|
|
151
|
+
return b;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/messages/AttachmentBuilder.ts
|
|
156
|
+
var AttachmentBuilder = class {
|
|
157
|
+
id;
|
|
158
|
+
filename;
|
|
159
|
+
description;
|
|
160
|
+
spoiler;
|
|
161
|
+
constructor(id, filename, options) {
|
|
162
|
+
this.id = id;
|
|
163
|
+
this.filename = options?.spoiler ? `SPOILER_${filename}` : filename;
|
|
164
|
+
this.description = options?.description ?? void 0;
|
|
165
|
+
this.spoiler = options?.spoiler ?? false;
|
|
166
|
+
}
|
|
167
|
+
setName(name) {
|
|
168
|
+
this.filename = this.spoiler ? `SPOILER_${name}` : name;
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
setDescription(description) {
|
|
172
|
+
this.description = description ?? void 0;
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
setSpoiler(spoiler = true) {
|
|
176
|
+
this.spoiler = spoiler;
|
|
177
|
+
if (spoiler && !this.filename.startsWith("SPOILER_")) this.filename = `SPOILER_${this.filename}`;
|
|
178
|
+
else if (!spoiler && this.filename.startsWith("SPOILER_")) this.filename = this.filename.slice(8);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
toJSON() {
|
|
182
|
+
return {
|
|
183
|
+
id: this.id,
|
|
184
|
+
filename: this.filename,
|
|
185
|
+
description: this.description ?? void 0
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/messages/MessagePayload.ts
|
|
191
|
+
var CONTENT_MAX = 2e3;
|
|
192
|
+
var EMBEDS_MAX = 10;
|
|
193
|
+
var MessagePayload = class _MessagePayload {
|
|
194
|
+
static ContentMaxLength = CONTENT_MAX;
|
|
195
|
+
data = {};
|
|
196
|
+
setContent(content) {
|
|
197
|
+
if (content !== null && content.length > CONTENT_MAX) throw new RangeError(`Content must be \u2264${CONTENT_MAX} characters`);
|
|
198
|
+
this.data.content = content ?? void 0;
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
setEmbeds(embeds) {
|
|
202
|
+
if (!embeds?.length) {
|
|
203
|
+
this.data.embeds = void 0;
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
if (embeds.length > EMBEDS_MAX) throw new RangeError(`Embeds must be \u2264${EMBEDS_MAX}`);
|
|
207
|
+
this.data.embeds = embeds.map((e) => e instanceof EmbedBuilder ? e.toJSON() : e);
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
addEmbed(embed) {
|
|
211
|
+
const list = (this.data.embeds ?? []).slice();
|
|
212
|
+
if (list.length >= EMBEDS_MAX) throw new RangeError(`Embeds must be \u2264${EMBEDS_MAX}`);
|
|
213
|
+
list.push(embed instanceof EmbedBuilder ? embed.toJSON() : embed);
|
|
214
|
+
this.data.embeds = list;
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
setAttachments(attachments) {
|
|
218
|
+
if (!attachments?.length) {
|
|
219
|
+
this.data.attachments = void 0;
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
this.data.attachments = attachments.map((a) => a instanceof AttachmentBuilder ? a.toJSON() : a);
|
|
223
|
+
return this;
|
|
224
|
+
}
|
|
225
|
+
setReply(reference) {
|
|
226
|
+
if (!reference) {
|
|
227
|
+
this.data.message_reference = void 0;
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
this.data.message_reference = {
|
|
231
|
+
channel_id: reference.channel_id,
|
|
232
|
+
message_id: reference.message_id,
|
|
233
|
+
guild_id: reference.guild_id ?? void 0
|
|
234
|
+
};
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
setTTS(tts) {
|
|
238
|
+
this.data.tts = tts;
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
setFlags(flags) {
|
|
242
|
+
this.data.flags = flags;
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
toJSON() {
|
|
246
|
+
return { ...this.data };
|
|
247
|
+
}
|
|
248
|
+
static create(contentOrOptions) {
|
|
249
|
+
const payload = new _MessagePayload();
|
|
250
|
+
if (typeof contentOrOptions === "string") {
|
|
251
|
+
payload.setContent(contentOrOptions);
|
|
252
|
+
} else if (contentOrOptions && typeof contentOrOptions === "object") {
|
|
253
|
+
if (contentOrOptions.content !== void 0) payload.setContent(contentOrOptions.content ?? null);
|
|
254
|
+
if (contentOrOptions.embeds?.length) payload.setEmbeds(contentOrOptions.embeds);
|
|
255
|
+
if (contentOrOptions.attachments?.length) payload.setAttachments(contentOrOptions.attachments);
|
|
256
|
+
if (contentOrOptions.message_reference) payload.setReply(contentOrOptions.message_reference);
|
|
257
|
+
if (contentOrOptions.tts !== void 0) payload.setTTS(contentOrOptions.tts);
|
|
258
|
+
if (contentOrOptions.flags !== void 0) payload.setFlags(contentOrOptions.flags);
|
|
259
|
+
}
|
|
260
|
+
return payload;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
264
|
+
0 && (module.exports = {
|
|
265
|
+
AttachmentBuilder,
|
|
266
|
+
EmbedBuilder,
|
|
267
|
+
MessagePayload
|
|
268
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// src/messages/EmbedBuilder.ts
|
|
2
|
+
import { resolveColor } from "@fluxerjs/util";
|
|
3
|
+
var EMBED_MAX = {
|
|
4
|
+
title: 256,
|
|
5
|
+
description: 4096,
|
|
6
|
+
fields: 25,
|
|
7
|
+
fieldName: 256,
|
|
8
|
+
fieldValue: 1024,
|
|
9
|
+
footerText: 2048,
|
|
10
|
+
authorName: 256,
|
|
11
|
+
total: 6e3
|
|
12
|
+
};
|
|
13
|
+
var EmbedBuilder = class _EmbedBuilder {
|
|
14
|
+
data = {};
|
|
15
|
+
setTitle(title) {
|
|
16
|
+
if (title !== null && title.length > EMBED_MAX.title) throw new RangeError(`Title must be \u2264${EMBED_MAX.title} characters`);
|
|
17
|
+
this.data.title = title ?? void 0;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
setDescription(description) {
|
|
21
|
+
if (description !== null && description.length > EMBED_MAX.description) throw new RangeError(`Description must be \u2264${EMBED_MAX.description} characters`);
|
|
22
|
+
this.data.description = description ?? void 0;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
setURL(url) {
|
|
26
|
+
this.data.url = url ?? void 0;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
setColor(color) {
|
|
30
|
+
if (color === null) {
|
|
31
|
+
this.data.color = void 0;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
this.data.color = typeof color === "number" ? color : resolveColor(color);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
setTimestamp(timestamp) {
|
|
38
|
+
if (timestamp === void 0 || timestamp === null) {
|
|
39
|
+
this.data.timestamp = void 0;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
const date = timestamp instanceof Date ? timestamp : new Date(timestamp);
|
|
43
|
+
this.data.timestamp = date.toISOString();
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
setAuthor(options) {
|
|
47
|
+
if (!options) {
|
|
48
|
+
this.data.author = void 0;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
const name = options.name.slice(0, EMBED_MAX.authorName);
|
|
52
|
+
const author = { name };
|
|
53
|
+
if (options.url) author.url = options.url;
|
|
54
|
+
if (options.iconURL) author.icon_url = options.iconURL;
|
|
55
|
+
this.data.author = author;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
setFooter(options) {
|
|
59
|
+
if (!options) {
|
|
60
|
+
this.data.footer = void 0;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
const text = options.text.slice(0, EMBED_MAX.footerText);
|
|
64
|
+
const footer = { text };
|
|
65
|
+
if (options.iconURL) footer.icon_url = options.iconURL;
|
|
66
|
+
this.data.footer = footer;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
setImage(url) {
|
|
70
|
+
this.data.image = url ? { url } : void 0;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
setThumbnail(url) {
|
|
74
|
+
this.data.thumbnail = url ? { url } : void 0;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
setVideo(url) {
|
|
78
|
+
this.data.video = url ? { url } : void 0;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
addFields(...fields) {
|
|
82
|
+
const current = (this.data.fields ?? []).slice();
|
|
83
|
+
for (const f of fields) {
|
|
84
|
+
if (current.length >= EMBED_MAX.fields) break;
|
|
85
|
+
current.push({
|
|
86
|
+
name: f.name.slice(0, EMBED_MAX.fieldName),
|
|
87
|
+
value: f.value.slice(0, EMBED_MAX.fieldValue),
|
|
88
|
+
inline: f.inline
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
this.data.fields = current.length ? current : void 0;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
spliceFields(index, deleteCount, ...fields) {
|
|
95
|
+
const current = (this.data.fields ?? []).slice();
|
|
96
|
+
const toAdd = fields.map((f) => ({
|
|
97
|
+
name: f.name.slice(0, EMBED_MAX.fieldName),
|
|
98
|
+
value: f.value.slice(0, EMBED_MAX.fieldValue),
|
|
99
|
+
inline: f.inline
|
|
100
|
+
}));
|
|
101
|
+
current.splice(index, deleteCount, ...toAdd);
|
|
102
|
+
this.data.fields = current.length ? current : void 0;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
toJSON() {
|
|
106
|
+
const totalLength = [this.data.title, this.data.description, ...(this.data.fields ?? []).flatMap((f) => [f.name, f.value]), this.data.footer?.text].filter(Boolean).join("").length;
|
|
107
|
+
if (totalLength > EMBED_MAX.total) throw new RangeError(`Embed total length must be \u2264${EMBED_MAX.total}`);
|
|
108
|
+
return { ...this.data, type: this.data.type ?? "rich" };
|
|
109
|
+
}
|
|
110
|
+
static from(data) {
|
|
111
|
+
const b = new _EmbedBuilder();
|
|
112
|
+
b.data.title = data.title ?? void 0;
|
|
113
|
+
b.data.description = data.description ?? void 0;
|
|
114
|
+
b.data.url = data.url ?? void 0;
|
|
115
|
+
b.data.color = data.color ?? void 0;
|
|
116
|
+
b.data.timestamp = data.timestamp ?? void 0;
|
|
117
|
+
b.data.author = data.author ?? void 0;
|
|
118
|
+
b.data.footer = data.footer ?? void 0;
|
|
119
|
+
b.data.image = data.image ?? void 0;
|
|
120
|
+
b.data.thumbnail = data.thumbnail ?? void 0;
|
|
121
|
+
b.data.fields = data.fields ?? void 0;
|
|
122
|
+
b.data.type = data.type ?? "rich";
|
|
123
|
+
return b;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/messages/AttachmentBuilder.ts
|
|
128
|
+
var AttachmentBuilder = class {
|
|
129
|
+
id;
|
|
130
|
+
filename;
|
|
131
|
+
description;
|
|
132
|
+
spoiler;
|
|
133
|
+
constructor(id, filename, options) {
|
|
134
|
+
this.id = id;
|
|
135
|
+
this.filename = options?.spoiler ? `SPOILER_${filename}` : filename;
|
|
136
|
+
this.description = options?.description ?? void 0;
|
|
137
|
+
this.spoiler = options?.spoiler ?? false;
|
|
138
|
+
}
|
|
139
|
+
setName(name) {
|
|
140
|
+
this.filename = this.spoiler ? `SPOILER_${name}` : name;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
setDescription(description) {
|
|
144
|
+
this.description = description ?? void 0;
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
setSpoiler(spoiler = true) {
|
|
148
|
+
this.spoiler = spoiler;
|
|
149
|
+
if (spoiler && !this.filename.startsWith("SPOILER_")) this.filename = `SPOILER_${this.filename}`;
|
|
150
|
+
else if (!spoiler && this.filename.startsWith("SPOILER_")) this.filename = this.filename.slice(8);
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
toJSON() {
|
|
154
|
+
return {
|
|
155
|
+
id: this.id,
|
|
156
|
+
filename: this.filename,
|
|
157
|
+
description: this.description ?? void 0
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/messages/MessagePayload.ts
|
|
163
|
+
var CONTENT_MAX = 2e3;
|
|
164
|
+
var EMBEDS_MAX = 10;
|
|
165
|
+
var MessagePayload = class _MessagePayload {
|
|
166
|
+
static ContentMaxLength = CONTENT_MAX;
|
|
167
|
+
data = {};
|
|
168
|
+
setContent(content) {
|
|
169
|
+
if (content !== null && content.length > CONTENT_MAX) throw new RangeError(`Content must be \u2264${CONTENT_MAX} characters`);
|
|
170
|
+
this.data.content = content ?? void 0;
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
setEmbeds(embeds) {
|
|
174
|
+
if (!embeds?.length) {
|
|
175
|
+
this.data.embeds = void 0;
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
if (embeds.length > EMBEDS_MAX) throw new RangeError(`Embeds must be \u2264${EMBEDS_MAX}`);
|
|
179
|
+
this.data.embeds = embeds.map((e) => e instanceof EmbedBuilder ? e.toJSON() : e);
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
addEmbed(embed) {
|
|
183
|
+
const list = (this.data.embeds ?? []).slice();
|
|
184
|
+
if (list.length >= EMBEDS_MAX) throw new RangeError(`Embeds must be \u2264${EMBEDS_MAX}`);
|
|
185
|
+
list.push(embed instanceof EmbedBuilder ? embed.toJSON() : embed);
|
|
186
|
+
this.data.embeds = list;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
setAttachments(attachments) {
|
|
190
|
+
if (!attachments?.length) {
|
|
191
|
+
this.data.attachments = void 0;
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
this.data.attachments = attachments.map((a) => a instanceof AttachmentBuilder ? a.toJSON() : a);
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
setReply(reference) {
|
|
198
|
+
if (!reference) {
|
|
199
|
+
this.data.message_reference = void 0;
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
this.data.message_reference = {
|
|
203
|
+
channel_id: reference.channel_id,
|
|
204
|
+
message_id: reference.message_id,
|
|
205
|
+
guild_id: reference.guild_id ?? void 0
|
|
206
|
+
};
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
setTTS(tts) {
|
|
210
|
+
this.data.tts = tts;
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
setFlags(flags) {
|
|
214
|
+
this.data.flags = flags;
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
toJSON() {
|
|
218
|
+
return { ...this.data };
|
|
219
|
+
}
|
|
220
|
+
static create(contentOrOptions) {
|
|
221
|
+
const payload = new _MessagePayload();
|
|
222
|
+
if (typeof contentOrOptions === "string") {
|
|
223
|
+
payload.setContent(contentOrOptions);
|
|
224
|
+
} else if (contentOrOptions && typeof contentOrOptions === "object") {
|
|
225
|
+
if (contentOrOptions.content !== void 0) payload.setContent(contentOrOptions.content ?? null);
|
|
226
|
+
if (contentOrOptions.embeds?.length) payload.setEmbeds(contentOrOptions.embeds);
|
|
227
|
+
if (contentOrOptions.attachments?.length) payload.setAttachments(contentOrOptions.attachments);
|
|
228
|
+
if (contentOrOptions.message_reference) payload.setReply(contentOrOptions.message_reference);
|
|
229
|
+
if (contentOrOptions.tts !== void 0) payload.setTTS(contentOrOptions.tts);
|
|
230
|
+
if (contentOrOptions.flags !== void 0) payload.setFlags(contentOrOptions.flags);
|
|
231
|
+
}
|
|
232
|
+
return payload;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
export {
|
|
236
|
+
AttachmentBuilder,
|
|
237
|
+
EmbedBuilder,
|
|
238
|
+
MessagePayload
|
|
239
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fluxerjs/builders",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "1.0.2",
|
|
7
|
+
"description": "Builders for Fluxer messages and embeds",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@fluxerjs/util": "1.0.2",
|
|
23
|
+
"@fluxerjs/types": "1.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.3.0",
|
|
27
|
+
"typescript": "^5.6.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
31
|
+
"clean": "rm -rf dist"
|
|
32
|
+
}
|
|
33
|
+
}
|