qaut.js 1.0.0
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 +82 -0
- package/package.json +27 -0
- package/src/index.d.ts +122 -0
- package/src/index.js +322 -0
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# qaut.js
|
|
2
|
+
|
|
3
|
+
Official QauT bot framework for Node.js.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i qaut.js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Local development from this repository:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm i ../QauT.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const { Client, Events, GatewayIntentBits, EmbedBuilder, Status } = require('qaut.js');
|
|
21
|
+
|
|
22
|
+
const client = new Client({
|
|
23
|
+
token: process.env.QAUT_TOKEN,
|
|
24
|
+
intents: [
|
|
25
|
+
GatewayIntentBits.Guilds,
|
|
26
|
+
GatewayIntentBits.GuildMessages,
|
|
27
|
+
GatewayIntentBits.MessageContent
|
|
28
|
+
]
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
client.on(Events.ClientReady, async bot => {
|
|
32
|
+
console.log(`Ready as ${bot.username}`);
|
|
33
|
+
await client.setPresence({
|
|
34
|
+
status: Status.Online,
|
|
35
|
+
activity: { name: 'QauT servers', type: 3 }
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
client.on(Events.MessageCreate, async message => {
|
|
40
|
+
if (message.content === '!ping') {
|
|
41
|
+
await message.reply({
|
|
42
|
+
embeds: [
|
|
43
|
+
new EmbedBuilder()
|
|
44
|
+
.setTitle('Pong')
|
|
45
|
+
.setDescription('QauT.js is running.')
|
|
46
|
+
.setColor('#22d3ee')
|
|
47
|
+
.setTimestamp()
|
|
48
|
+
]
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
client.login();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Builders
|
|
57
|
+
|
|
58
|
+
- `EmbedBuilder`
|
|
59
|
+
- `ButtonBuilder`
|
|
60
|
+
- `ActionRowBuilder`
|
|
61
|
+
- `SlashCommandBuilder`
|
|
62
|
+
- `PermissionBitField`
|
|
63
|
+
- `IntentBitField`
|
|
64
|
+
|
|
65
|
+
## REST
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const { REST } = require('qaut.js');
|
|
69
|
+
|
|
70
|
+
const rest = new REST({ token: process.env.QAUT_TOKEN });
|
|
71
|
+
await rest.post('/bot/channels/123/messages', {
|
|
72
|
+
content: 'Hello from QauT.js'
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Developer Portal
|
|
77
|
+
|
|
78
|
+
Create and manage bot applications from:
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
https://qaut.gg/developers/applications
|
|
82
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "qaut.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official QauT bot framework for building bots and integrations.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "QauT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"qaut",
|
|
11
|
+
"qaut.js",
|
|
12
|
+
"bot",
|
|
13
|
+
"gateway",
|
|
14
|
+
"embed",
|
|
15
|
+
"oauth2"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "node test/smoke.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"README.md"
|
|
26
|
+
]
|
|
27
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
|
|
4
|
+
export declare const Events: {
|
|
5
|
+
readonly ClientReady: 'clientReady';
|
|
6
|
+
readonly Debug: 'debug';
|
|
7
|
+
readonly Error: 'error';
|
|
8
|
+
readonly MessageCreate: 'messageCreate';
|
|
9
|
+
readonly MessageUpdate: 'messageUpdate';
|
|
10
|
+
readonly MessageDelete: 'messageDelete';
|
|
11
|
+
readonly InteractionCreate: 'interactionCreate';
|
|
12
|
+
readonly GuildCreate: 'guildCreate';
|
|
13
|
+
readonly GuildUpdate: 'guildUpdate';
|
|
14
|
+
readonly GuildDelete: 'guildDelete';
|
|
15
|
+
readonly GuildMemberAdd: 'guildMemberAdd';
|
|
16
|
+
readonly GuildMemberRemove: 'guildMemberRemove';
|
|
17
|
+
readonly PresenceUpdate: 'presenceUpdate';
|
|
18
|
+
readonly WebhookEvent: 'webhookEvent';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export declare const GatewayIntentBits: Record<string, number>;
|
|
22
|
+
export declare const Status: Record<string, string>;
|
|
23
|
+
export declare const ActivityType: Record<string, number>;
|
|
24
|
+
export declare const PermissionFlagsBits: Record<string, bigint>;
|
|
25
|
+
export declare const DefaultRestBase = "https://qaut.gg/api";
|
|
26
|
+
|
|
27
|
+
export declare class QauTError extends Error {
|
|
28
|
+
details?: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare class BitField {
|
|
32
|
+
bitfield: bigint;
|
|
33
|
+
constructor(bits?: bigint | number | string | Array<bigint | number | string>);
|
|
34
|
+
has(bit: bigint | number | string): boolean;
|
|
35
|
+
add(...bits: Array<bigint | number | string | Array<bigint | number | string>>): this;
|
|
36
|
+
remove(...bits: Array<bigint | number | string | Array<bigint | number | string>>): this;
|
|
37
|
+
toString(): string;
|
|
38
|
+
valueOf(): bigint;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export declare class PermissionBitField extends BitField {}
|
|
42
|
+
export declare class IntentBitField extends BitField {}
|
|
43
|
+
|
|
44
|
+
export declare class EmbedBuilder {
|
|
45
|
+
constructor(data?: Record<string, unknown>);
|
|
46
|
+
setTitle(title: string): this;
|
|
47
|
+
setDescription(description: string): this;
|
|
48
|
+
setColor(color: string | number): this;
|
|
49
|
+
setURL(url: string): this;
|
|
50
|
+
setTimestamp(timestamp?: Date | string | number): this;
|
|
51
|
+
setAuthor(author: string | Record<string, unknown>): this;
|
|
52
|
+
setFooter(footer: string | Record<string, unknown>): this;
|
|
53
|
+
setImage(url: string): this;
|
|
54
|
+
setThumbnail(url: string): this;
|
|
55
|
+
addFields(...fields: Array<{ name: string; value: string; inline?: boolean } | Array<{ name: string; value: string; inline?: boolean }>>): this;
|
|
56
|
+
toJSON(): Record<string, unknown>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export declare class ButtonBuilder {
|
|
60
|
+
constructor(data?: Record<string, unknown>);
|
|
61
|
+
setCustomId(customId: string): this;
|
|
62
|
+
setLabel(label: string): this;
|
|
63
|
+
setStyle(style: string): this;
|
|
64
|
+
setURL(url: string): this;
|
|
65
|
+
setDisabled(disabled?: boolean): this;
|
|
66
|
+
toJSON(): Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export declare class ActionRowBuilder {
|
|
70
|
+
constructor(data?: { components?: unknown[] });
|
|
71
|
+
addComponents(...components: unknown[]): this;
|
|
72
|
+
toJSON(): Record<string, unknown>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export declare class SlashCommandBuilder {
|
|
76
|
+
constructor(data?: Record<string, unknown>);
|
|
77
|
+
setName(name: string): this;
|
|
78
|
+
setDescription(description: string): this;
|
|
79
|
+
addStringOption(option: Record<string, unknown>): this;
|
|
80
|
+
addIntegerOption(option: Record<string, unknown>): this;
|
|
81
|
+
addBooleanOption(option: Record<string, unknown>): this;
|
|
82
|
+
toJSON(): Record<string, unknown>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export declare class REST {
|
|
86
|
+
constructor(options?: { token?: string; baseUrl?: string; authPrefix?: string });
|
|
87
|
+
setToken(token: string): this;
|
|
88
|
+
request(method: string, route: string, body?: unknown): Promise<unknown>;
|
|
89
|
+
get(route: string): Promise<unknown>;
|
|
90
|
+
post(route: string, body?: unknown): Promise<unknown>;
|
|
91
|
+
patch(route: string, body?: unknown): Promise<unknown>;
|
|
92
|
+
delete(route: string, body?: unknown): Promise<unknown>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export declare class TextChannel {
|
|
96
|
+
id: string;
|
|
97
|
+
constructor(client: Client, data?: Record<string, unknown>);
|
|
98
|
+
send(payload: string | Record<string, unknown>): Promise<unknown>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export declare class Message {
|
|
102
|
+
client: Client;
|
|
103
|
+
channel: TextChannel | null;
|
|
104
|
+
content?: string;
|
|
105
|
+
constructor(client: Client, data?: Record<string, unknown>);
|
|
106
|
+
reply(payload: string | Record<string, unknown>): Promise<unknown>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export declare class Client extends EventEmitter {
|
|
110
|
+
token: string;
|
|
111
|
+
rest: REST;
|
|
112
|
+
user: unknown;
|
|
113
|
+
intents: IntentBitField;
|
|
114
|
+
constructor(options?: { token?: string; intents?: Array<number | bigint | string> | number | bigint | string; restBaseUrl?: string });
|
|
115
|
+
login(token?: string): Promise<string>;
|
|
116
|
+
setPresence(presence?: Record<string, unknown>): Promise<unknown>;
|
|
117
|
+
setStatus(status: string): Promise<unknown>;
|
|
118
|
+
channel(id: string): TextChannel;
|
|
119
|
+
emitGatewayEvent(eventName: string, payload: unknown): boolean;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export declare function CreateEmbed(data?: Record<string, unknown>): EmbedBuilder;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { EventEmitter } = require('events');
|
|
4
|
+
|
|
5
|
+
const DefaultRestBase = 'https://qaut.gg/api';
|
|
6
|
+
|
|
7
|
+
const Events = Object.freeze({
|
|
8
|
+
ClientReady: 'clientReady',
|
|
9
|
+
Debug: 'debug',
|
|
10
|
+
Error: 'error',
|
|
11
|
+
MessageCreate: 'messageCreate',
|
|
12
|
+
MessageUpdate: 'messageUpdate',
|
|
13
|
+
MessageDelete: 'messageDelete',
|
|
14
|
+
InteractionCreate: 'interactionCreate',
|
|
15
|
+
GuildCreate: 'guildCreate',
|
|
16
|
+
GuildUpdate: 'guildUpdate',
|
|
17
|
+
GuildDelete: 'guildDelete',
|
|
18
|
+
GuildMemberAdd: 'guildMemberAdd',
|
|
19
|
+
GuildMemberRemove: 'guildMemberRemove',
|
|
20
|
+
PresenceUpdate: 'presenceUpdate',
|
|
21
|
+
WebhookEvent: 'webhookEvent'
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const GatewayIntentBits = Object.freeze({
|
|
25
|
+
Guilds: 1 << 0,
|
|
26
|
+
GuildMembers: 1 << 1,
|
|
27
|
+
GuildMessages: 1 << 2,
|
|
28
|
+
MessageContent: 1 << 3,
|
|
29
|
+
GuildPresences: 1 << 4,
|
|
30
|
+
DirectMessages: 1 << 5,
|
|
31
|
+
Webhooks: 1 << 6
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const Status = Object.freeze({
|
|
35
|
+
Online: 'online',
|
|
36
|
+
Idle: 'idle',
|
|
37
|
+
DoNotDisturb: 'dnd',
|
|
38
|
+
Invisible: 'invisible',
|
|
39
|
+
Offline: 'offline'
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const ActivityType = Object.freeze({
|
|
43
|
+
Playing: 0,
|
|
44
|
+
Streaming: 1,
|
|
45
|
+
Listening: 2,
|
|
46
|
+
Watching: 3,
|
|
47
|
+
Custom: 4,
|
|
48
|
+
Competing: 5
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const PermissionFlagsBits = Object.freeze({
|
|
52
|
+
Administrator: 1n << 0n,
|
|
53
|
+
ViewAuditLog: 1n << 1n,
|
|
54
|
+
ManageServer: 1n << 2n,
|
|
55
|
+
ManageRoles: 1n << 3n,
|
|
56
|
+
ManageChannels: 1n << 4n,
|
|
57
|
+
KickMembers: 1n << 5n,
|
|
58
|
+
BanMembers: 1n << 6n,
|
|
59
|
+
CreateInstantInvite: 1n << 7n,
|
|
60
|
+
ManageWebhooks: 1n << 11n,
|
|
61
|
+
ViewChannels: 1n << 12n,
|
|
62
|
+
SendMessages: 1n << 20n,
|
|
63
|
+
ManageMessages: 1n << 24n,
|
|
64
|
+
EmbedLinks: 1n << 25n,
|
|
65
|
+
AttachFiles: 1n << 26n,
|
|
66
|
+
ReadMessageHistory: 1n << 27n,
|
|
67
|
+
MentionEveryone: 1n << 28n,
|
|
68
|
+
UseSlashCommands: 1n << 31n,
|
|
69
|
+
Connect: 1n << 40n,
|
|
70
|
+
Speak: 1n << 41n,
|
|
71
|
+
Video: 1n << 42n,
|
|
72
|
+
MuteMembers: 1n << 43n,
|
|
73
|
+
DeafenMembers: 1n << 44n,
|
|
74
|
+
MoveMembers: 1n << 45n,
|
|
75
|
+
UseVoiceActivity: 1n << 46n
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
class QauTError extends Error {
|
|
79
|
+
constructor(message, details) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = 'QauTError';
|
|
82
|
+
this.details = details;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
class BitField {
|
|
87
|
+
constructor(bits = 0n) {
|
|
88
|
+
this.bitfield = Array.isArray(bits) ? bits.reduce((total, bit) => total | BigInt(bit), 0n) : BigInt(bits);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
has(bit) {
|
|
92
|
+
return (this.bitfield & BigInt(bit)) === BigInt(bit);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
add(...bits) {
|
|
96
|
+
bits.flat().forEach(bit => { this.bitfield |= BigInt(bit); });
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
remove(...bits) {
|
|
101
|
+
bits.flat().forEach(bit => { this.bitfield &= ~BigInt(bit); });
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
toString() {
|
|
106
|
+
return this.bitfield.toString();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
valueOf() {
|
|
110
|
+
return this.bitfield;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
class PermissionBitField extends BitField {
|
|
115
|
+
static Flags = PermissionFlagsBits;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class IntentBitField extends BitField {
|
|
119
|
+
static Flags = GatewayIntentBits;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
class EmbedBuilder {
|
|
123
|
+
constructor(data = {}) {
|
|
124
|
+
this.data = { ...data };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
setTitle(title) { this.data.title = String(title).slice(0, 256); return this; }
|
|
128
|
+
setDescription(description) { this.data.description = String(description).slice(0, 4096); return this; }
|
|
129
|
+
setColor(color) { this.data.color = normalizeColor(color); return this; }
|
|
130
|
+
setURL(url) { this.data.url = String(url); return this; }
|
|
131
|
+
setTimestamp(timestamp = new Date()) { this.data.timestamp = new Date(timestamp).toISOString(); return this; }
|
|
132
|
+
setAuthor(author) { this.data.author = typeof author === 'string' ? { name: author } : { ...author }; return this; }
|
|
133
|
+
setFooter(footer) { this.data.footer = typeof footer === 'string' ? { text: footer } : { ...footer }; return this; }
|
|
134
|
+
setImage(url) { this.data.image = { url: String(url) }; return this; }
|
|
135
|
+
setThumbnail(url) { this.data.thumbnail = { url: String(url) }; return this; }
|
|
136
|
+
|
|
137
|
+
addFields(...fields) {
|
|
138
|
+
this.data.fields ||= [];
|
|
139
|
+
this.data.fields.push(...fields.flat().map(field => ({
|
|
140
|
+
name: String(field.name).slice(0, 256),
|
|
141
|
+
value: String(field.value).slice(0, 1024),
|
|
142
|
+
inline: Boolean(field.inline)
|
|
143
|
+
})));
|
|
144
|
+
this.data.fields = this.data.fields.slice(0, 25);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
toJSON() {
|
|
149
|
+
return { ...this.data };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class ButtonBuilder {
|
|
154
|
+
constructor(data = {}) { this.data = { type: 'button', style: 'primary', ...data }; }
|
|
155
|
+
setCustomId(customId) { this.data.custom_id = String(customId); return this; }
|
|
156
|
+
setLabel(label) { this.data.label = String(label).slice(0, 80); return this; }
|
|
157
|
+
setStyle(style) { this.data.style = style; return this; }
|
|
158
|
+
setURL(url) { this.data.url = String(url); this.data.style = 'link'; return this; }
|
|
159
|
+
setDisabled(disabled = true) { this.data.disabled = Boolean(disabled); return this; }
|
|
160
|
+
toJSON() { return { ...this.data }; }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
class ActionRowBuilder {
|
|
164
|
+
constructor(data = {}) { this.components = data.components || []; }
|
|
165
|
+
addComponents(...components) { this.components.push(...components.flat()); return this; }
|
|
166
|
+
toJSON() { return { type: 'action_row', components: this.components.map(component => toJSON(component)) }; }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
class SlashCommandBuilder {
|
|
170
|
+
constructor(data = {}) {
|
|
171
|
+
this.data = { type: 'chat_input', options: [], ...data };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
setName(name) { this.data.name = String(name).toLowerCase().replace(/\s+/g, '-').slice(0, 32); return this; }
|
|
175
|
+
setDescription(description) { this.data.description = String(description).slice(0, 100); return this; }
|
|
176
|
+
addStringOption(option) { this.data.options.push({ type: 'string', ...option }); return this; }
|
|
177
|
+
addIntegerOption(option) { this.data.options.push({ type: 'integer', ...option }); return this; }
|
|
178
|
+
addBooleanOption(option) { this.data.options.push({ type: 'boolean', ...option }); return this; }
|
|
179
|
+
toJSON() { return { ...this.data, options: [...this.data.options] }; }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class REST {
|
|
183
|
+
constructor(options = {}) {
|
|
184
|
+
this.baseUrl = String(options.baseUrl || DefaultRestBase).replace(/\/+$/, '');
|
|
185
|
+
this.token = options.token || '';
|
|
186
|
+
this.authPrefix = options.authPrefix || 'Bot';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
setToken(token) {
|
|
190
|
+
this.token = token;
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async request(method, route, body) {
|
|
195
|
+
const response = await fetch(`${this.baseUrl}${route}`, {
|
|
196
|
+
method,
|
|
197
|
+
headers: {
|
|
198
|
+
Authorization: `${this.authPrefix} ${this.token}`,
|
|
199
|
+
'Content-Type': 'application/json'
|
|
200
|
+
},
|
|
201
|
+
body: body === undefined ? undefined : JSON.stringify(serialize(body))
|
|
202
|
+
});
|
|
203
|
+
const data = await response.json().catch(() => null);
|
|
204
|
+
if (!response.ok) throw new QauTError(data?.error || response.statusText, { status: response.status, data });
|
|
205
|
+
return data;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
get(route) { return this.request('GET', route); }
|
|
209
|
+
post(route, body) { return this.request('POST', route, body); }
|
|
210
|
+
patch(route, body) { return this.request('PATCH', route, body); }
|
|
211
|
+
delete(route, body) { return this.request('DELETE', route, body); }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
class TextChannel {
|
|
215
|
+
constructor(client, data = {}) {
|
|
216
|
+
this.client = client;
|
|
217
|
+
Object.assign(this, data);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
send(payload) {
|
|
221
|
+
return this.client.rest.post(`/bot/channels/${this.id}/messages`, normalizeMessagePayload(payload));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
class Message {
|
|
226
|
+
constructor(client, data = {}) {
|
|
227
|
+
this.client = client;
|
|
228
|
+
Object.assign(this, data);
|
|
229
|
+
this.channel = data.channel || (data.channelId ? new TextChannel(client, { id: data.channelId }) : null);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
reply(payload) {
|
|
233
|
+
if (!this.channel) throw new QauTError('Cannot reply without channel information');
|
|
234
|
+
return this.channel.send(payload);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
class Client extends EventEmitter {
|
|
239
|
+
constructor(options = {}) {
|
|
240
|
+
super();
|
|
241
|
+
this.options = options;
|
|
242
|
+
this.token = options.token || '';
|
|
243
|
+
this.intents = new IntentBitField(options.intents || 0);
|
|
244
|
+
this.rest = new REST({ token: this.token, baseUrl: options.restBaseUrl || DefaultRestBase });
|
|
245
|
+
this.user = null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async login(token = this.token) {
|
|
249
|
+
if (!token) throw new QauTError('Bot token is required');
|
|
250
|
+
this.token = token;
|
|
251
|
+
this.rest.setToken(token);
|
|
252
|
+
const me = await this.rest.get('/bot/me');
|
|
253
|
+
this.user = me;
|
|
254
|
+
this.emit(Events.ClientReady, me);
|
|
255
|
+
return token;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async setPresence(presence = {}) {
|
|
259
|
+
return this.rest.patch('/bot/presence', presence);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async setStatus(status) {
|
|
263
|
+
return this.setPresence({ status });
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
channel(id) {
|
|
267
|
+
return new TextChannel(this, { id });
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
emitGatewayEvent(eventName, payload) {
|
|
271
|
+
if (eventName === Events.MessageCreate) return this.emit(eventName, new Message(this, payload));
|
|
272
|
+
return this.emit(eventName, payload);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function CreateEmbed(data) {
|
|
277
|
+
return new EmbedBuilder(data);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function normalizeMessagePayload(payload) {
|
|
281
|
+
if (typeof payload === 'string') return { content: payload };
|
|
282
|
+
const output = { ...payload };
|
|
283
|
+
if (output.embeds) output.embeds = output.embeds.map(toJSON);
|
|
284
|
+
if (output.components) output.components = output.components.map(toJSON);
|
|
285
|
+
return output;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function normalizeColor(color) {
|
|
289
|
+
if (typeof color === 'number') return color;
|
|
290
|
+
const value = String(color).replace('#', '');
|
|
291
|
+
return Number.parseInt(value, 16);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function toJSON(value) {
|
|
295
|
+
return value && typeof value.toJSON === 'function' ? value.toJSON() : value;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function serialize(value) {
|
|
299
|
+
return JSON.parse(JSON.stringify(value, (_key, item) => typeof item === 'bigint' ? item.toString() : toJSON(item)));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
module.exports = {
|
|
303
|
+
ActionRowBuilder,
|
|
304
|
+
ActivityType,
|
|
305
|
+
BitField,
|
|
306
|
+
ButtonBuilder,
|
|
307
|
+
Client,
|
|
308
|
+
CreateEmbed,
|
|
309
|
+
DefaultRestBase,
|
|
310
|
+
EmbedBuilder,
|
|
311
|
+
Events,
|
|
312
|
+
GatewayIntentBits,
|
|
313
|
+
IntentBitField,
|
|
314
|
+
Message,
|
|
315
|
+
PermissionBitField,
|
|
316
|
+
PermissionFlagsBits,
|
|
317
|
+
QauTError,
|
|
318
|
+
REST,
|
|
319
|
+
SlashCommandBuilder,
|
|
320
|
+
Status,
|
|
321
|
+
TextChannel
|
|
322
|
+
};
|