beniocord.js 2.0.3 → 2.0.4

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
@@ -19,6 +19,10 @@ class ClientError extends Error {
19
19
  }
20
20
  }
21
21
 
22
+ let global = {
23
+ token: "",
24
+ apiUrl: "https://api-bots.beniocord.site"
25
+ };
22
26
  /**
23
27
  * @fires Client#ready
24
28
  * @fires Client#messageCreate
@@ -36,8 +40,9 @@ class Client extends EventEmitter {
36
40
  throw new ClientError("Valid token is required", "INVALID_TOKEN");
37
41
  }
38
42
 
39
- this.token = token.trim();
40
- this.apiUrl = "https://api-bots.beniocord.site";
43
+ // this.token = token.trim();
44
+ global.token = token.trim();
45
+ // this.apiUrl = "https://api-bots.beniocord.site";
41
46
  this.socket = null;
42
47
  this.user = null;
43
48
  this.isConnected = false;
@@ -64,12 +69,13 @@ class Client extends EventEmitter {
64
69
  this._sentMessages = new Set();
65
70
 
66
71
  this._axios = axios.create({
67
- baseURL: this.apiUrl,
72
+ baseURL: global.apiUrl,
68
73
  timeout: this.config.requestTimeout,
69
74
  headers: {
70
- 'Authorization': `Bearer ${this.token}`,
75
+ 'Authorization': `Bearer ${global.token}`,
76
+ 'x-bot-token': global.token,
71
77
  'Content-Type': 'application/json',
72
- 'Origin': this.apiUrl
78
+ 'Origin': global.apiUrl
73
79
  }
74
80
  });
75
81
 
@@ -173,9 +179,9 @@ class Client extends EventEmitter {
173
179
  ));
174
180
  }, this.config.connectionTimeout);
175
181
 
176
- this.socket = io(this.apiUrl, {
177
- auth: { token: this.token },
178
- extraHeaders: { 'Origin': this.apiUrl },
182
+ this.socket = io(global.apiUrl, {
183
+ auth: { token: global.token },
184
+ extraHeaders: { 'Origin': global.apiUrl },
179
185
  timeout: 5000,
180
186
  reconnection: true,
181
187
  reconnectionDelay: this.config.reconnectionDelay,
@@ -295,12 +301,34 @@ class Client extends EventEmitter {
295
301
  this.emit('presenceUpdate', data);
296
302
  });
297
303
 
298
- this.socket.on('member:join', (data) => {
304
+ this.socket.on('member:join', async (data) => {
305
+ const member = await this.fetchUser(data.memberId).catch(() => null);
306
+ const channel = await this.fetchChannel(data.channelId).catch(() => null);
307
+
308
+ if (data.addedBy) {
309
+ data.addedBy = new User(data.addedBy, this);
310
+ }
311
+
312
+ data.member = member;
313
+ data.channel = channel;
314
+
299
315
  this.emit('memberJoin', data);
316
+ console.log('join', data);
300
317
  });
301
318
 
302
- this.socket.on('member:leave', (data) => {
319
+ this.socket.on('member:leave', async (data) => {
320
+ const member = await this.fetchUser(data.memberId).catch(() => null);
321
+ const channel = await this.fetchChannel(data.channelId).catch(() => null);
322
+
323
+ if (data.removedBy) {
324
+ data.removedBy = new User(data.removedBy, this);
325
+ }
326
+
327
+ data.member = member;
328
+ data.channel = channel;
329
+
303
330
  this.emit('memberLeave', data);
331
+ console.log('leave', data);
304
332
  });
305
333
 
306
334
  this.socket.on('channel:update', (data) => {
@@ -823,8 +851,8 @@ class Client extends EventEmitter {
823
851
  /**
824
852
  * @param {string} id
825
853
  */
826
- async fetchChannel(id) {
827
- if (this.cache.channels.has(id)) {
854
+ async fetchChannel(id, force = false) {
855
+ if (!force && this.cache.channels.has(id)) {
828
856
  return this.cache.channels.get(id);
829
857
  }
830
858
 
@@ -834,7 +862,9 @@ class Client extends EventEmitter {
834
862
  this.cache.channels.set(channel.id, channel);
835
863
  return channel;
836
864
  } catch (error) {
837
- throw error instanceof ClientError ? error : new ClientError(error.message, "FETCH_CHANNEL_ERROR");
865
+ throw error instanceof ClientError
866
+ ? error
867
+ : new ClientError(error.message, "FETCH_CHANNEL_ERROR");
838
868
  }
839
869
  }
840
870
 
@@ -965,18 +995,28 @@ class Client extends EventEmitter {
965
995
  /**
966
996
  * @param {string} id
967
997
  */
968
- async fetchUser(id) {
998
+ async fetchUser(id, force = false) {
999
+ if (!force && this.cache.users.has(id)) {
1000
+ return this.cache.users.get(id);
1001
+ }
1002
+
969
1003
  try {
970
1004
  const res = await this._axios.get(`/api/users/${id}`);
971
1005
  const user = new User(res.data, this);
972
1006
  this.cache.users.set(user.id, user);
973
1007
  return user;
974
1008
  } catch (error) {
975
- throw error instanceof ClientError ? error : new ClientError(error.message, "FETCH_USER_ERROR");
1009
+ throw error instanceof ClientError
1010
+ ? error
1011
+ : new ClientError(error.message, "FETCH_USER_ERROR");
976
1012
  }
977
1013
  }
978
1014
 
979
- async fetchMe() {
1015
+ async fetchMe(force = false) {
1016
+ if (!force && this.user) {
1017
+ return this.user;
1018
+ }
1019
+
980
1020
  try {
981
1021
  const res = await this._axios.get('/api/users/me');
982
1022
  const user = new User(res.data, this);
@@ -984,7 +1024,9 @@ class Client extends EventEmitter {
984
1024
  this.user = user;
985
1025
  return user;
986
1026
  } catch (error) {
987
- throw error instanceof ClientError ? error : new ClientError(error.message, "FETCH_ME_ERROR");
1027
+ throw error instanceof ClientError
1028
+ ? error
1029
+ : new ClientError(error.message, "FETCH_ME_ERROR");
988
1030
  }
989
1031
  }
990
1032
 
@@ -1004,14 +1046,20 @@ class Client extends EventEmitter {
1004
1046
  /**
1005
1047
  * @param {string} id
1006
1048
  */
1007
- async fetchEmoji(id) {
1049
+ async fetchEmoji(id, force = false) {
1050
+ if (!force && this.cache.emojis.has(id)) {
1051
+ return this.cache.emojis.get(id);
1052
+ }
1053
+
1008
1054
  try {
1009
1055
  const res = await this._axios.get(`/api/emojis/${id}`);
1010
- const emoji = new Emoji(res.data, this.apiUrl);
1056
+ const emoji = new Emoji(res.data);
1011
1057
  this.cache.emojis.set(emoji.id, emoji);
1012
1058
  return emoji;
1013
1059
  } catch (error) {
1014
- throw error instanceof ClientError ? error : new ClientError(error.message, "FETCH_EMOJI_ERROR");
1060
+ throw error instanceof ClientError
1061
+ ? error
1062
+ : new ClientError(error.message, "FETCH_EMOJI_ERROR");
1015
1063
  }
1016
1064
  }
1017
1065
 
@@ -1029,7 +1077,7 @@ class Client extends EventEmitter {
1029
1077
 
1030
1078
  const res = await this._axios.get(endpoint, { params });
1031
1079
  const emojis = res.data.map(e => {
1032
- const emoji = new Emoji(e, this.apiUrl);
1080
+ const emoji = new Emoji(e);
1033
1081
  this.cache.emojis.set(emoji.id, emoji);
1034
1082
  return emoji;
1035
1083
  });
@@ -1043,13 +1091,19 @@ class Client extends EventEmitter {
1043
1091
  /**
1044
1092
  * @param {string} id
1045
1093
  */
1046
- async fetchSticker(id) {
1094
+ async fetchSticker(id, force = false) {
1095
+ if (!force && this.cache.stickers.has(id)) {
1096
+ return this.cache.stickers.get(id);
1097
+ }
1098
+
1047
1099
  try {
1048
1100
  const res = await this._axios.get(`/api/stickers/${id}`);
1049
1101
  this.cache.stickers.set(res.data.id, res.data);
1050
1102
  return res.data;
1051
1103
  } catch (error) {
1052
- throw error instanceof ClientError ? error : new ClientError(error.message, "FETCH_STICKER_ERROR");
1104
+ throw error instanceof ClientError
1105
+ ? error
1106
+ : new ClientError(error.message, "FETCH_STICKER_ERROR");
1053
1107
  }
1054
1108
  }
1055
1109
 
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.3",
7
+ "version": "2.0.4",
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": {},
@@ -1,8 +1,9 @@
1
1
  const MessageCollector = require('./MessageCollector');
2
2
 
3
+ let client;
3
4
  class Channel {
4
- constructor(data, client) {
5
- this.client = client;
5
+ constructor(data, clientInstance) {
6
+ client = clientInstance;
6
7
 
7
8
  this.id = data.id;
8
9
  this.name = data.name;
@@ -10,26 +11,26 @@ class Channel {
10
11
  this.type = data.type || "text";
11
12
  this.isPrivate = data.is_private;
12
13
  this.isDm = data.is_dm;
13
- this.iconUrl = data.icon_url ? client.apiUrl + data.icon_url : undefined;
14
+ this.iconUrl = data.icon_url ? 'https://api.beniocord.site' + data.icon_url : undefined;
14
15
  this.createdBy = data.created_by;
15
16
  this.createdAt = data.created_at;
16
17
  this.updatedAt = data.updated_at;
17
18
  }
18
19
 
19
20
  async send(content, opts = {}) {
20
- return this.client.sendMessage(this.id, content, opts);
21
+ return client.sendMessage(this.id, content, opts);
21
22
  }
22
23
 
23
24
  startTyping() {
24
- return this.client.startTyping(this.id);
25
+ return client.startTyping(this.id);
25
26
  }
26
27
 
27
28
  stopTyping() {
28
- return this.client.stopTyping(this.id);
29
+ return client.stopTyping(this.id);
29
30
  }
30
31
 
31
32
  createMessageCollector(options = {}) {
32
- return new MessageCollector(this, options);
33
+ return new MessageCollector(this, options, client);
33
34
  }
34
35
 
35
36
  }
@@ -1,9 +1,9 @@
1
1
  class Emoji {
2
- constructor(data, api_url) {
2
+ constructor(data) {
3
3
  this.id = data.id;
4
4
  this.userId = data.user_id;
5
5
  this.name = data.name;
6
- this.url = data.url ? api_url + data.url : undefined;
6
+ this.url = data.url ? 'https://api.beniocord.site' + data.url : undefined;
7
7
  this.createdAt = data.created_at;
8
8
  this.updatedAt = data.updated_at;
9
9
  }
@@ -1,12 +1,13 @@
1
1
  const User = require("./User");
2
2
  const Channel = require("./Channel");
3
3
 
4
+ let client;
4
5
  class Message {
5
- constructor(data, client) {
6
+ constructor(data, clientInstance) {
6
7
  this.id = data.id;
7
8
  this.content = data.content;
8
9
  this.messageType = data.message_type || "text";
9
- this.fileUrl = data.file_url ? client.apiUrl + data.file_url : undefined;
10
+ this.fileUrl = data.file_url ? 'https://api.beniocord.site' + data.file_url : undefined;
10
11
  this.fileName = data.file_name;
11
12
  this.fileSize = data.file_size;
12
13
  this.replyTo = data.reply_to;
@@ -25,24 +26,25 @@ class Message {
25
26
  }
26
27
 
27
28
  // this.author = data.user ? new User(data.user, this) : null;
28
- this.author = data.user ? new User(data.user, client) : null;
29
- this.channel = data.channel ? new Channel(data.channel, client) : null;
29
+ this.author = data.user ? new User(data.user, clientInstance) : null;
30
+ this.channel = data.channel ? new Channel(data.channel, clientInstance) : null;
30
31
  // this.member = { user: this.author }
31
- this.client = client;
32
+ client = clientInstance;
32
33
  }
33
34
 
34
- async reply(content) {
35
- return this.client.sendMessage(this.channel.id, content, {
35
+ async reply(content, opts = {}) {
36
+ return client.sendMessage(this.channel.id, content, {
36
37
  replyTo: this.id,
38
+ ...opts
37
39
  });
38
40
  }
39
41
 
40
42
  async edit(content) {
41
- return this.client.editMessage(this.id, content);
43
+ return client.editMessage(this.id, content);
42
44
  }
43
45
 
44
46
  async delete() {
45
- return this.client.deleteMessage(this.id);
47
+ return client.deleteMessage(this.id);
46
48
  }
47
49
  }
48
50
 
@@ -1,11 +1,12 @@
1
1
  const EventEmitter = require('events');
2
2
 
3
3
  class MessageCollector extends EventEmitter {
4
- constructor(channel, options = {}) {
4
+ constructor(channel, options = {}, client) {
5
5
  super();
6
6
 
7
7
  this.channel = channel;
8
- this.client = channel.client;
8
+ this.client = client;
9
+ // this.client = channel.client;
9
10
  this.filter = options.filter || (() => true);
10
11
  this.time = options.time || 60000;
11
12
  this.max = options.max || Infinity;
@@ -3,7 +3,7 @@ class User {
3
3
  this.id = data.id;
4
4
  this.username = data.username;
5
5
  this.displayName = data.display_name;
6
- this.avatarUrl = data.avatar_url ? client.apiUrl + data.avatar_url : undefined;
6
+ this.avatarUrl = data.avatar_url ? 'https://api.beniocord.site' + data.avatar_url : undefined;
7
7
  this.status = data.status || "offline";
8
8
  this.emblems = data.emblems || [];
9
9
  this.lastSeen = data.last_seen;