discord.js-backup-v13 13.1.1 → 13.1.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/lib/create.js CHANGED
@@ -89,6 +89,8 @@ async function getEmojis(guild, options) {
89
89
  return emojis;
90
90
  }
91
91
  exports.getEmojis = getEmojis;
92
+
93
+
92
94
  /**
93
95
  * Returns an array with the channels of the guild
94
96
  * @param {Guild} guild The discord guild
@@ -96,57 +98,51 @@ exports.getEmojis = getEmojis;
96
98
  * @returns {ChannelData[]} The channels of the guild
97
99
  */
98
100
  async function getChannels(guild, options) {
99
- return new Promise(async (resolve) => {
100
- const channels = {
101
- categories: [],
102
- others: []
101
+ const allChannels = guild.channels.cache;
102
+
103
+ const categories = allChannels
104
+ .filter(ch => ch.type === 'GUILD_CATEGORY')
105
+ .sort((a, b) => a.position - b.position);
106
+
107
+ const categoryDataPromises = categories.map(async (category) => {
108
+ const children = allChannels
109
+ .filter(c => c.parentId === category.id)
110
+ .sort((a, b) => a.position - b.position);
111
+
112
+ const childrenData = await Promise.all(children.map(child => fetchAnyChannelData(child, options)));
113
+
114
+ return {
115
+ name: category.name,
116
+ permissions: (0, util_1.fetchChannelPermissions)(category),
117
+ children: childrenData
103
118
  };
119
+ });
104
120
 
105
- const categories = guild.channels.cache
106
- .filter((ch) => ch.type === 'GUILD_CATEGORY')
107
- .sort((a, b) => a.position - b.position)
121
+ const others = allChannels
122
+ .filter(ch => !ch.parent && ch.type !== 'GUILD_CATEGORY' && !ch.isThread() && ch.type !== 'GUILD_STORE')
123
+ .sort((a, b) => a.position - b.position);
108
124
 
109
- for (const category of categories.values()) {
125
+ const othersDataPromises = others.map(ch => fetchAnyChannelData(ch, options));
110
126
 
111
- const categoryData = {
112
- name: category.name,
113
- permissions: (0, util_1.fetchChannelPermissions)(category),
114
- children: []
115
- };
116
- const children = guild.channels.cache.filter(c => c.parentId == category.id).sort((a, b) => a.position - b.position)
127
+ const [categoriesResult, othersResult] = await Promise.all([
128
+ Promise.all(categoryDataPromises),
129
+ Promise.all(othersDataPromises)
130
+ ]);
117
131
 
118
- for (const child of children.values()) {
132
+ return {
133
+ categories: categoriesResult,
134
+ others: othersResult
135
+ };
136
+ }
119
137
 
120
- if (child.type === 'GUILD_TEXT' || child.type === 'GUILD_NEWS') {
121
- const channelData = await (0, util_1.fetchTextChannelData)(child, options);
122
- categoryData.children.push(channelData);
123
- }
124
- else {
125
- const channelData = await (0, util_1.fetchVoiceChannelData)(child);
126
- categoryData.children.push(channelData);
127
- }
128
- }
129
- channels.categories.push(categoryData);
130
- }
131
- const others = guild.channels.cache
132
- .filter((ch) => {
133
- return !ch.parent && ch.type !== 'GUILD_CATEGORY'
134
- && ch.type !== 'GUILD_STORE'
135
- && ch.type !== 'GUILD_NEWS_THREAD' && ch.type !== 'GUILD_PRIVATE_THREAD' && ch.type !== 'GUILD_PUBLIC_THREAD';
136
- })
137
- .sort((a, b) => a.position - b.position)
138
- .toJSON();
139
- for (const channel of others.values()) {
140
- if (channel.type === 'GUILD_TEXT' || channel.type === 'GUILD_NEWS') {
141
- const channelData = await (0, util_1.fetchTextChannelData)(channel, options);
142
- channels.others.push(channelData);
143
- }
144
- else {
145
- const channelData = await (0, util_1.fetchVoiceChannelData)(channel);
146
- channels.others.push(channelData);
147
- }
148
- }
149
- resolve(channels);
150
- });
138
+ /**
139
+ * Fonction utilitaire pour éviter la répétition de la logique IF/ELSE
140
+ */
141
+ async function fetchAnyChannelData(channel, options) {
142
+ if (['GUILD_TEXT', 'GUILD_NEWS'].includes(channel.type)) {
143
+ return await (0, util_1.fetchTextChannelData)(channel, options);
144
+ } else {
145
+ return await (0, util_1.fetchVoiceChannelData)(channel);
146
+ }
151
147
  }
152
148
  exports.getChannels = getChannels;
package/lib/load.js CHANGED
@@ -50,7 +50,7 @@ const loadRoles = (guild, backupData) => {
50
50
  if (roleData.isEveryone) {
51
51
  rolePromises.push(guild.roles.cache.get(guild.id).edit({
52
52
  name: roleData.name,
53
- color: roleData.color,
53
+ colors: { primaryColor: roleData.color },
54
54
  permissions: BigInt(roleData.permissions),
55
55
  mentionable: roleData.mentionable
56
56
  }));
@@ -58,7 +58,7 @@ const loadRoles = (guild, backupData) => {
58
58
  else {
59
59
  rolePromises.push(guild.roles.create({
60
60
  name: roleData.name,
61
- color: roleData.color,
61
+ colors: { primaryColor: roleData.color },
62
62
  hoist: roleData.hoist,
63
63
  permissions: BigInt(roleData.permissions),
64
64
  mentionable: roleData.mentionable
@@ -1,6 +1,6 @@
1
1
  export interface RoleData {
2
2
  name: string;
3
- color: `#${string}`;
3
+ colors: { primaryColor: `#${string}` };
4
4
  hoist: boolean;
5
5
  permissions: string;
6
6
  mentionable: boolean;
package/lib/util.js CHANGED
@@ -47,37 +47,41 @@ async function fetchVoiceChannelData(channel) {
47
47
  }
48
48
  exports.fetchVoiceChannelData = fetchVoiceChannelData;
49
49
  async function fetchChannelMessages(channel, options) {
50
- let messages = [];
51
- const messageCount = isNaN(options.maxMessagesPerChannel) ? 10 : options.maxMessagesPerChannel;
50
+ const messages = [];
51
+ const maxMessages = isNaN(options.maxMessagesPerChannel) ? 10 : options.maxMessagesPerChannel;
52
52
  const fetchOptions = { limit: 100 };
53
53
  let lastMessageId;
54
- let fetchComplete = false;
55
- while (!fetchComplete) {
56
- if (lastMessageId) {
57
- fetchOptions.before = lastMessageId;
58
- }
54
+
55
+ const imageRegex = /\.(png|jpg|jpeg|jpe|jif|jfif|jfi)$/i;
56
+
57
+ while (messages.length < maxMessages) {
58
+ if (lastMessageId) fetchOptions.before = lastMessageId;
59
+
59
60
  const fetched = await channel.messages.fetch(fetchOptions);
60
- if (fetched.size === 0) {
61
- break;
62
- }
61
+ if (fetched.size === 0) break;
62
+
63
63
  lastMessageId = fetched.last().id;
64
- await Promise.all(fetched.map(async (msg) => {
65
- if (!msg.author || messages.length >= messageCount) {
66
- fetchComplete = true;
67
- return;
68
- }
64
+
65
+ for (const msg of fetched.values()) {
66
+ if (messages.length >= maxMessages) break;
67
+ if (!msg.author) continue;
68
+
69
69
  const files = await Promise.all(msg.attachments.map(async (a) => {
70
70
  let attach = a.url;
71
- if (a.url && ['png', 'jpg', 'jpeg', 'jpe', 'jif', 'jfif', 'jfi'].includes(a.url)) {
72
- if (options.saveImages && options.saveImages === 'base64') {
73
- attach = (await ((0, node_fetch_1.default)(a.url).then((res) => res.buffer()))).toString('base64');
71
+
72
+ if (options.saveImages === 'base64' && imageRegex.test(a.url)) {
73
+ try {
74
+ const response = await fetch(a.url);
75
+ const buffer = await response.arrayBuffer();
76
+ attach = Buffer.from(buffer).toString('base64');
77
+ } catch (e) {
78
+ console.error(`Erreur base64 sur ${a.url}:`, e);
74
79
  }
75
80
  }
76
- return {
77
- name: a.name,
78
- attachment: attach
79
- };
81
+
82
+ return { name: a.name, attachment: attach };
80
83
  }));
84
+
81
85
  messages.push({
82
86
  username: msg.author.username,
83
87
  avatar: msg.author.displayAvatarURL(),
@@ -87,8 +91,11 @@ async function fetchChannelMessages(channel, options) {
87
91
  pinned: msg.pinned,
88
92
  sentAt: msg.createdAt.toISOString(),
89
93
  });
90
- }));
94
+ }
95
+
96
+ if (fetched.size < 100) break;
91
97
  }
98
+
92
99
  return messages;
93
100
  }
94
101
  exports.fetchChannelMessages = fetchChannelMessages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discord.js-backup-v13",
3
- "version": "13.1.1",
3
+ "version": "13.1.2",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib/**/*"