beniocord.js 2.0.5 → 2.0.6
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 +87 -27
- package/package.json +1 -1
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) {
|
|
@@ -194,7 +201,7 @@ class Client extends EventEmitter {
|
|
|
194
201
|
this.isConnected = true;
|
|
195
202
|
this.retryCount = 0;
|
|
196
203
|
this._setupSocketHandlers();
|
|
197
|
-
this._startHeartbeat();
|
|
204
|
+
this._startHeartbeat();
|
|
198
205
|
resolve();
|
|
199
206
|
});
|
|
200
207
|
|
|
@@ -241,7 +248,7 @@ class Client extends EventEmitter {
|
|
|
241
248
|
|
|
242
249
|
this.socket.on("reconnect", (attemptNumber) => {
|
|
243
250
|
this.isConnected = true;
|
|
244
|
-
this._startHeartbeat();
|
|
251
|
+
this._startHeartbeat();
|
|
245
252
|
this.emit("reconnect", attemptNumber);
|
|
246
253
|
});
|
|
247
254
|
|
|
@@ -301,22 +308,6 @@ class Client extends EventEmitter {
|
|
|
301
308
|
}
|
|
302
309
|
}
|
|
303
310
|
|
|
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
311
|
/**
|
|
321
312
|
* Desconecta o cliente e limpa recursos
|
|
322
313
|
*/
|
|
@@ -334,15 +325,28 @@ class Client extends EventEmitter {
|
|
|
334
325
|
});
|
|
335
326
|
}
|
|
336
327
|
|
|
328
|
+
this._removeSocketHandlers();
|
|
329
|
+
|
|
330
|
+
this.socket.off("connect");
|
|
331
|
+
this.socket.off("disconnect");
|
|
332
|
+
this.socket.off("connect_error");
|
|
333
|
+
this.socket.off("reconnect");
|
|
334
|
+
this.socket.off("reconnect_error");
|
|
335
|
+
this.socket.off("reconnect_failed");
|
|
336
|
+
|
|
337
337
|
this.socket.disconnect();
|
|
338
338
|
this.socket = null;
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
this.isConnected = false;
|
|
342
|
+
this.isReady = false;
|
|
342
343
|
}
|
|
343
344
|
|
|
345
|
+
|
|
344
346
|
_setupSocketHandlers() {
|
|
345
|
-
this.
|
|
347
|
+
this._removeSocketHandlers();
|
|
348
|
+
|
|
349
|
+
this.socket.on('message:new', async (data) => {
|
|
346
350
|
try {
|
|
347
351
|
if (this._sentMessages.has(data.id)) {
|
|
348
352
|
this._sentMessages.delete(data.id);
|
|
@@ -398,6 +402,26 @@ class Client extends EventEmitter {
|
|
|
398
402
|
data.member = member;
|
|
399
403
|
data.channel = channel;
|
|
400
404
|
|
|
405
|
+
// 🔥 Se EU fui adicionado ao canal
|
|
406
|
+
if (data.memberId === this.user?.id) {
|
|
407
|
+
// Adiciona o canal na cache se não existir
|
|
408
|
+
if (channel && !this.cache.channels.has(data.channelId)) {
|
|
409
|
+
this.cache.channels.set(data.channelId, channel);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Entra na room do canal no socket
|
|
413
|
+
this.socket.emit('channel:join', { channelId: data.channelId });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// 🔥 Se o canal já existe na cache, adiciona o membro nele
|
|
417
|
+
if (channel && member) {
|
|
418
|
+
// Assumindo que o Channel tem uma lista/Map de membros
|
|
419
|
+
if (!channel.members) {
|
|
420
|
+
channel.members = new Map();
|
|
421
|
+
}
|
|
422
|
+
channel.members.set(member.id, member);
|
|
423
|
+
}
|
|
424
|
+
|
|
401
425
|
this.emit('memberJoin', data);
|
|
402
426
|
console.log('join', data);
|
|
403
427
|
});
|
|
@@ -413,6 +437,20 @@ class Client extends EventEmitter {
|
|
|
413
437
|
data.member = member;
|
|
414
438
|
data.channel = channel;
|
|
415
439
|
|
|
440
|
+
// 🔥 Se EU fui removido do canal
|
|
441
|
+
if (data.memberId === this.user?.id) {
|
|
442
|
+
// Remove o canal da cache
|
|
443
|
+
this.cache.channels.delete(data.channelId);
|
|
444
|
+
|
|
445
|
+
// Sai da room do canal no socket
|
|
446
|
+
this.socket.emit('channel:leave', { channelId: data.channelId });
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// 🔥 Se o canal existe na cache, remove o membro dele
|
|
450
|
+
if (channel && member && channel.members) {
|
|
451
|
+
channel.members.delete(member.id);
|
|
452
|
+
}
|
|
453
|
+
|
|
416
454
|
this.emit('memberLeave', data);
|
|
417
455
|
console.log('leave', data);
|
|
418
456
|
});
|
|
@@ -432,6 +470,24 @@ class Client extends EventEmitter {
|
|
|
432
470
|
});
|
|
433
471
|
}
|
|
434
472
|
|
|
473
|
+
_removeSocketHandlers() {
|
|
474
|
+
if (!this.socket) return;
|
|
475
|
+
|
|
476
|
+
// Remove todos os listeners customizados
|
|
477
|
+
this.socket.off("message:new");
|
|
478
|
+
this.socket.off("message:deleted");
|
|
479
|
+
this.socket.off("message:edited");
|
|
480
|
+
this.socket.off("typing:user-start");
|
|
481
|
+
this.socket.off("typing:user-stop");
|
|
482
|
+
this.socket.off("user:status-update");
|
|
483
|
+
this.socket.off("presence:update");
|
|
484
|
+
this.socket.off("member:join");
|
|
485
|
+
this.socket.off("member:leave");
|
|
486
|
+
this.socket.off("channel:update");
|
|
487
|
+
this.socket.off("channel:delete");
|
|
488
|
+
this.socket.off("rate:limited");
|
|
489
|
+
}
|
|
490
|
+
|
|
435
491
|
async _processSocketMessage(data) {
|
|
436
492
|
const msg = new Message(data, this);
|
|
437
493
|
|
|
@@ -533,7 +589,8 @@ class Client extends EventEmitter {
|
|
|
533
589
|
}
|
|
534
590
|
|
|
535
591
|
/**
|
|
536
|
-
*
|
|
592
|
+
* Set the user status
|
|
593
|
+
* @param {string} status - Status: "online", "away", "dnd", "offline"
|
|
537
594
|
*/
|
|
538
595
|
async setStatus(status) {
|
|
539
596
|
const validStatuses = ["online", "offline", "away", "dnd"];
|
|
@@ -549,13 +606,7 @@ class Client extends EventEmitter {
|
|
|
549
606
|
this.socket.emit('status:update', { status });
|
|
550
607
|
}
|
|
551
608
|
|
|
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
|
-
*/
|
|
609
|
+
|
|
559
610
|
// async sendMessage(channelId, content, opts = {}) {
|
|
560
611
|
// return new Promise(async (resolve, reject) => {
|
|
561
612
|
// try {
|
|
@@ -628,6 +679,15 @@ class Client extends EventEmitter {
|
|
|
628
679
|
// }
|
|
629
680
|
// });
|
|
630
681
|
// }
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Send a message
|
|
686
|
+
* @param {string} channelId - Channel ID
|
|
687
|
+
* @param {string|MessageEmbed} content - Message content or MessageEmbed
|
|
688
|
+
* @param {Object|MessageAttachment} opts - Extra options
|
|
689
|
+
* @returns {Promise<Message>}
|
|
690
|
+
*/
|
|
631
691
|
async sendMessage(channelId, content, opts = {}) {
|
|
632
692
|
return new Promise(async (resolve, reject) => {
|
|
633
693
|
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.
|
|
7
|
+
"version": "2.0.6",
|
|
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": {},
|