beniocord.js 2.0.5 → 2.0.7

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/Client.js CHANGED
@@ -83,6 +83,13 @@ class Client extends EventEmitter {
83
83
  response => response,
84
84
  error => this._handleAxiosError(error)
85
85
  );
86
+
87
+ setInterval(() => {
88
+ if (this._sentMessages.size > 1000) {
89
+ this._sentMessages.clear();
90
+ }
91
+ }, 30 * 60 * 1000);
92
+
86
93
  }
87
94
 
88
95
  _handleAxiosError(error) {
@@ -157,6 +164,8 @@ class Client extends EventEmitter {
157
164
  );
158
165
  }
159
166
 
167
+ await this._joinAllChannelRooms();
168
+
160
169
  this.isReady = true;
161
170
  this.emit("ready", this.user);
162
171
 
@@ -194,7 +203,7 @@ class Client extends EventEmitter {
194
203
  this.isConnected = true;
195
204
  this.retryCount = 0;
196
205
  this._setupSocketHandlers();
197
- this._startHeartbeat(); // Inicia o heartbeat
206
+ this._startHeartbeat();
198
207
  resolve();
199
208
  });
200
209
 
@@ -241,7 +250,7 @@ class Client extends EventEmitter {
241
250
 
242
251
  this.socket.on("reconnect", (attemptNumber) => {
243
252
  this.isConnected = true;
244
- this._startHeartbeat(); // Reinicia o heartbeat após reconexão
253
+ this._startHeartbeat();
245
254
  this.emit("reconnect", attemptNumber);
246
255
  });
247
256
 
@@ -259,6 +268,20 @@ class Client extends EventEmitter {
259
268
  });
260
269
  }
261
270
 
271
+ async _joinAllChannelRooms() {
272
+ try {
273
+ const channels = await this.fetchChannels();
274
+
275
+ for (const channel of channels) {
276
+ if (this.socket && this.socket.connected) {
277
+ this.socket.emit('channel:join', { channelId: channel.id });
278
+ }
279
+ }
280
+ } catch (error) {
281
+ console.error('Erro ao entrar nas rooms dos canais:', error);
282
+ }
283
+ }
284
+
262
285
  /**
263
286
  * Inicia o sistema de heartbeat
264
287
  * @private
@@ -301,22 +324,6 @@ class Client extends EventEmitter {
301
324
  }
302
325
  }
303
326
 
304
- /**
305
- * Define o status do bot (se o servidor suportar)
306
- * @param {string} status - Status: "online", "away", "dnd", "offline"
307
- */
308
- setStatus(status) {
309
- if (!["online", "away", "dnd", "offline"].includes(status)) {
310
- throw new ClientError("Invalid status", "INVALID_STATUS");
311
- }
312
-
313
- this.status = status;
314
-
315
- if (this.socket && this.isConnected) {
316
- this.socket.emit("status:update", { status });
317
- }
318
- }
319
-
320
327
  /**
321
328
  * Desconecta o cliente e limpa recursos
322
329
  */
@@ -334,15 +341,28 @@ class Client extends EventEmitter {
334
341
  });
335
342
  }
336
343
 
344
+ this._removeSocketHandlers();
345
+
346
+ this.socket.off("connect");
347
+ this.socket.off("disconnect");
348
+ this.socket.off("connect_error");
349
+ this.socket.off("reconnect");
350
+ this.socket.off("reconnect_error");
351
+ this.socket.off("reconnect_failed");
352
+
337
353
  this.socket.disconnect();
338
354
  this.socket = null;
339
355
  }
340
356
 
341
357
  this.isConnected = false;
358
+ this.isReady = false;
342
359
  }
343
360
 
361
+
344
362
  _setupSocketHandlers() {
345
- this.socket.on("message:new", async (data) => {
363
+ this._removeSocketHandlers();
364
+
365
+ this.socket.on('message:new', async (data) => {
346
366
  try {
347
367
  if (this._sentMessages.has(data.id)) {
348
368
  this._sentMessages.delete(data.id);
@@ -398,8 +418,24 @@ class Client extends EventEmitter {
398
418
  data.member = member;
399
419
  data.channel = channel;
400
420
 
421
+ if (data.memberId === this.user?.id) {
422
+ if (channel && !this.cache.channels.has(data.channelId)) {
423
+ this.cache.channels.set(data.channelId, channel);
424
+ }
425
+
426
+ if (this.socket && this.socket.connected) {
427
+ this.socket.emit('channel:join', { channelId: data.channelId });
428
+ }
429
+ }
430
+
431
+ if (channel && member) {
432
+ if (!channel.members) {
433
+ channel.members = new Map();
434
+ }
435
+ channel.members.set(member.id, member);
436
+ }
437
+
401
438
  this.emit('memberJoin', data);
402
- console.log('join', data);
403
439
  });
404
440
 
405
441
  this.socket.on('member:leave', async (data) => {
@@ -413,8 +449,18 @@ class Client extends EventEmitter {
413
449
  data.member = member;
414
450
  data.channel = channel;
415
451
 
452
+ if (data.memberId === this.user?.id) {
453
+ this.cache.channels.delete(data.channelId);
454
+ if (this.socket && this.socket.connected) {
455
+ this.socket.emit('channel:leave', { channelId: data.channelId });
456
+ }
457
+ }
458
+
459
+ if (channel && member && channel.members) {
460
+ channel.members.delete(member.id);
461
+ }
462
+
416
463
  this.emit('memberLeave', data);
417
- console.log('leave', data);
418
464
  });
419
465
 
420
466
  this.socket.on('channel:update', (data) => {
@@ -432,6 +478,24 @@ class Client extends EventEmitter {
432
478
  });
433
479
  }
434
480
 
481
+ _removeSocketHandlers() {
482
+ if (!this.socket) return;
483
+
484
+ // Remove todos os listeners customizados
485
+ this.socket.off("message:new");
486
+ this.socket.off("message:deleted");
487
+ this.socket.off("message:edited");
488
+ this.socket.off("typing:user-start");
489
+ this.socket.off("typing:user-stop");
490
+ this.socket.off("user:status-update");
491
+ this.socket.off("presence:update");
492
+ this.socket.off("member:join");
493
+ this.socket.off("member:leave");
494
+ this.socket.off("channel:update");
495
+ this.socket.off("channel:delete");
496
+ this.socket.off("rate:limited");
497
+ }
498
+
435
499
  async _processSocketMessage(data) {
436
500
  const msg = new Message(data, this);
437
501
 
@@ -533,7 +597,8 @@ class Client extends EventEmitter {
533
597
  }
534
598
 
535
599
  /**
536
- * @param {string} status - online, offline, away, dnd
600
+ * Set the user status
601
+ * @param {string} status - Status: "online", "away", "dnd", "offline"
537
602
  */
538
603
  async setStatus(status) {
539
604
  const validStatuses = ["online", "offline", "away", "dnd"];
@@ -549,13 +614,7 @@ class Client extends EventEmitter {
549
614
  this.socket.emit('status:update', { status });
550
615
  }
551
616
 
552
- /**
553
- * Envia uma mensagem para um canal
554
- * @param {string} channelId - ID do canal
555
- * @param {string|MessageEmbed} content - Conteúdo da mensagem ou MessageEmbed
556
- * @param {Object|MessageAttachment} opts - Opções adicionais
557
- * @returns {Promise<Message>}
558
- */
617
+
559
618
  // async sendMessage(channelId, content, opts = {}) {
560
619
  // return new Promise(async (resolve, reject) => {
561
620
  // try {
@@ -628,6 +687,15 @@ class Client extends EventEmitter {
628
687
  // }
629
688
  // });
630
689
  // }
690
+
691
+
692
+ /**
693
+ * Send a message
694
+ * @param {string} channelId - Channel ID
695
+ * @param {string|MessageEmbed} content - Message content or MessageEmbed
696
+ * @param {Object|MessageAttachment} opts - Extra options
697
+ * @returns {Promise<Message>}
698
+ */
631
699
  async sendMessage(channelId, content, opts = {}) {
632
700
  return new Promise(async (resolve, reject) => {
633
701
  try {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "socket.io-client": "^4.8.1"
5
5
  },
6
6
  "name": "beniocord.js",
7
- "version": "2.0.5",
7
+ "version": "2.0.7",
8
8
  "description": "Uma biblioteca leve e intuitiva para integração com APIs de bots em plataformas de mensagens, como Discord. Facilita o envio de mensagens, gerenciamento de canais e interação com usuários, proporcionando uma experiência de desenvolvimento ágil e eficiente.",
9
9
  "main": "Client.js",
10
10
  "devDependencies": {},
@@ -48,7 +48,7 @@ class MessageCollector extends EventEmitter {
48
48
  }
49
49
  }
50
50
 
51
- stop(reason = 'user') {
51
+ stop(reason = 'user', ) {
52
52
  if (this.ended) return;
53
53
 
54
54
  this.ended = true;