qaut.js 1.0.0 → 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/README.md CHANGED
@@ -53,6 +53,12 @@ client.on(Events.MessageCreate, async message => {
53
53
  client.login();
54
54
  ```
55
55
 
56
+ `Client` keeps the Node.js process alive after login by default. For one-shot scripts, pass:
57
+
58
+ ```js
59
+ const client = new Client({ token: process.env.QAUT_TOKEN, keepAlive: false });
60
+ ```
61
+
56
62
  ## Builders
57
63
 
58
64
  - `EmbedBuilder`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qaut.js",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Official QauT bot framework for building bots and integrations.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/index.d.ts CHANGED
@@ -98,9 +98,16 @@ export declare class TextChannel {
98
98
  send(payload: string | Record<string, unknown>): Promise<unknown>;
99
99
  }
100
100
 
101
+ export declare class User {
102
+ id: string;
103
+ constructor(client: Client, data?: Record<string, unknown>);
104
+ send(payload: string | Record<string, unknown>): Promise<unknown>;
105
+ }
106
+
101
107
  export declare class Message {
102
108
  client: Client;
103
109
  channel: TextChannel | null;
110
+ author: User | null;
104
111
  content?: string;
105
112
  constructor(client: Client, data?: Record<string, unknown>);
106
113
  reply(payload: string | Record<string, unknown>): Promise<unknown>;
@@ -111,11 +118,18 @@ export declare class Client extends EventEmitter {
111
118
  rest: REST;
112
119
  user: unknown;
113
120
  intents: IntentBitField;
114
- constructor(options?: { token?: string; intents?: Array<number | bigint | string> | number | bigint | string; restBaseUrl?: string });
121
+ polling: boolean;
122
+ pollIntervalMs: number;
123
+ keepAlive: boolean;
124
+ constructor(options?: { token?: string; intents?: Array<number | bigint | string> | number | bigint | string; restBaseUrl?: string; keepAlive?: boolean; polling?: boolean; pollIntervalMs?: number });
115
125
  login(token?: string): Promise<string>;
126
+ startKeepAlive(): this;
127
+ startPolling(): this;
128
+ destroy(): this;
116
129
  setPresence(presence?: Record<string, unknown>): Promise<unknown>;
117
130
  setStatus(status: string): Promise<unknown>;
118
131
  channel(id: string): TextChannel;
132
+ user(id: string): User;
119
133
  emitGatewayEvent(eventName: string, payload: unknown): boolean;
120
134
  }
121
135
 
package/src/index.js CHANGED
@@ -222,11 +222,23 @@ class TextChannel {
222
222
  }
223
223
  }
224
224
 
225
+ class User {
226
+ constructor(client, data = {}) {
227
+ this.client = client;
228
+ Object.assign(this, data);
229
+ }
230
+
231
+ send(payload) {
232
+ return this.client.rest.post(`/bot/users/${this.id}/messages`, normalizeMessagePayload(payload));
233
+ }
234
+ }
235
+
225
236
  class Message {
226
237
  constructor(client, data = {}) {
227
238
  this.client = client;
228
239
  Object.assign(this, data);
229
240
  this.channel = data.channel || (data.channelId ? new TextChannel(client, { id: data.channelId }) : null);
241
+ this.author = data.author ? new User(client, data.author) : null;
230
242
  }
231
243
 
232
244
  reply(payload) {
@@ -243,6 +255,12 @@ class Client extends EventEmitter {
243
255
  this.intents = new IntentBitField(options.intents || 0);
244
256
  this.rest = new REST({ token: this.token, baseUrl: options.restBaseUrl || DefaultRestBase });
245
257
  this.user = null;
258
+ this.keepAlive = options.keepAlive !== false;
259
+ this.keepAliveInterval = null;
260
+ this.polling = options.polling !== false;
261
+ this.pollIntervalMs = Number(options.pollIntervalMs || 1500);
262
+ this.pollInterval = null;
263
+ this.gatewayCursor = null;
246
264
  }
247
265
 
248
266
  async login(token = this.token) {
@@ -251,10 +269,48 @@ class Client extends EventEmitter {
251
269
  this.rest.setToken(token);
252
270
  const me = await this.rest.get('/bot/me');
253
271
  this.user = me;
272
+ if (this.keepAlive) this.startKeepAlive();
273
+ if (this.polling) this.startPolling();
254
274
  this.emit(Events.ClientReady, me);
255
275
  return token;
256
276
  }
257
277
 
278
+ startKeepAlive() {
279
+ if (this.keepAliveInterval) return this;
280
+ this.keepAliveInterval = setInterval(() => {
281
+ this.emit(Events.Debug, 'QauT.js keep-alive heartbeat');
282
+ }, 30_000);
283
+ return this;
284
+ }
285
+
286
+ destroy() {
287
+ if (this.keepAliveInterval) clearInterval(this.keepAliveInterval);
288
+ if (this.pollInterval) clearInterval(this.pollInterval);
289
+ this.keepAliveInterval = null;
290
+ this.pollInterval = null;
291
+ this.user = null;
292
+ return this;
293
+ }
294
+
295
+ startPolling() {
296
+ if (this.pollInterval) return this;
297
+ this.gatewayCursor = new Date().toISOString();
298
+ const tick = async () => {
299
+ try {
300
+ const query = this.gatewayCursor ? `?after=${encodeURIComponent(this.gatewayCursor)}` : '';
301
+ const data = await this.rest.get(`/bot/gateway/messages${query}`);
302
+ this.gatewayCursor = data.cursor || new Date().toISOString();
303
+ for (const payload of data.messages || []) {
304
+ this.emitGatewayEvent(Events.MessageCreate, payload);
305
+ }
306
+ } catch (err) {
307
+ this.emit(Events.Error, err);
308
+ }
309
+ };
310
+ this.pollInterval = setInterval(tick, this.pollIntervalMs);
311
+ return this;
312
+ }
313
+
258
314
  async setPresence(presence = {}) {
259
315
  return this.rest.patch('/bot/presence', presence);
260
316
  }
@@ -267,6 +323,10 @@ class Client extends EventEmitter {
267
323
  return new TextChannel(this, { id });
268
324
  }
269
325
 
326
+ user(id) {
327
+ return new User(this, { id });
328
+ }
329
+
270
330
  emitGatewayEvent(eventName, payload) {
271
331
  if (eventName === Events.MessageCreate) return this.emit(eventName, new Message(this, payload));
272
332
  return this.emit(eventName, payload);
@@ -319,4 +379,5 @@ module.exports = {
319
379
  SlashCommandBuilder,
320
380
  Status,
321
381
  TextChannel
382
+ , User
322
383
  };