qaut.js 1.0.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qaut.js",
3
- "version": "1.0.1",
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,14 +118,18 @@ export declare class Client extends EventEmitter {
111
118
  rest: REST;
112
119
  user: unknown;
113
120
  intents: IntentBitField;
121
+ polling: boolean;
122
+ pollIntervalMs: number;
114
123
  keepAlive: boolean;
115
- constructor(options?: { token?: string; intents?: Array<number | bigint | string> | number | bigint | string; restBaseUrl?: string; keepAlive?: boolean });
124
+ constructor(options?: { token?: string; intents?: Array<number | bigint | string> | number | bigint | string; restBaseUrl?: string; keepAlive?: boolean; polling?: boolean; pollIntervalMs?: number });
116
125
  login(token?: string): Promise<string>;
117
126
  startKeepAlive(): this;
127
+ startPolling(): this;
118
128
  destroy(): this;
119
129
  setPresence(presence?: Record<string, unknown>): Promise<unknown>;
120
130
  setStatus(status: string): Promise<unknown>;
121
131
  channel(id: string): TextChannel;
132
+ user(id: string): User;
122
133
  emitGatewayEvent(eventName: string, payload: unknown): boolean;
123
134
  }
124
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) {
@@ -245,6 +257,10 @@ class Client extends EventEmitter {
245
257
  this.user = null;
246
258
  this.keepAlive = options.keepAlive !== false;
247
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;
248
264
  }
249
265
 
250
266
  async login(token = this.token) {
@@ -254,6 +270,7 @@ class Client extends EventEmitter {
254
270
  const me = await this.rest.get('/bot/me');
255
271
  this.user = me;
256
272
  if (this.keepAlive) this.startKeepAlive();
273
+ if (this.polling) this.startPolling();
257
274
  this.emit(Events.ClientReady, me);
258
275
  return token;
259
276
  }
@@ -268,11 +285,32 @@ class Client extends EventEmitter {
268
285
 
269
286
  destroy() {
270
287
  if (this.keepAliveInterval) clearInterval(this.keepAliveInterval);
288
+ if (this.pollInterval) clearInterval(this.pollInterval);
271
289
  this.keepAliveInterval = null;
290
+ this.pollInterval = null;
272
291
  this.user = null;
273
292
  return this;
274
293
  }
275
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
+
276
314
  async setPresence(presence = {}) {
277
315
  return this.rest.patch('/bot/presence', presence);
278
316
  }
@@ -285,6 +323,10 @@ class Client extends EventEmitter {
285
323
  return new TextChannel(this, { id });
286
324
  }
287
325
 
326
+ user(id) {
327
+ return new User(this, { id });
328
+ }
329
+
288
330
  emitGatewayEvent(eventName, payload) {
289
331
  if (eventName === Events.MessageCreate) return this.emit(eventName, new Message(this, payload));
290
332
  return this.emit(eventName, payload);
@@ -337,4 +379,5 @@ module.exports = {
337
379
  SlashCommandBuilder,
338
380
  Status,
339
381
  TextChannel
382
+ , User
340
383
  };