UNPKG

discord-rebackup

Version:

An rewamped fork of the discord-backup module ! by the iHorizon Team

284 lines (283 loc) 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBans = getBans; exports.getMembers = getMembers; exports.getRoles = getRoles; exports.getEmojis = getEmojis; exports.getChannels = getChannels; const util_1 = require("./util"); // Support pour discord.js v14 et discord.js-selfbot-v13 let ChannelType; try { // discord.js v14 ChannelType = require('discord.js').ChannelType; } catch { // discord.js-selfbot-v13 (simulation des types de canaux) ChannelType = { GuildText: 'GUILD_TEXT', GuildVoice: 'GUILD_VOICE', GuildCategory: 'GUILD_CATEGORY', GuildAnnouncement: 'GUILD_NEWS', GuildStageVoice: 'GUILD_STAGE_VOICE', AnnouncementThread: 'ANNOUNCEMENT_THREAD', PublicThread: 'PUBLIC_THREAD', PrivateThread: 'PRIVATE_THREAD', GuildForum: 'GUILD_FORUM', GuildMedia: 'GUILD_MEDIA' }; } // Import des types nécessaires en fonction de la version disponible let discordjs; try { // Tenter d'importer discord.js v14 discordjs = require('discord.js'); } catch { // Si échec, on suppose que c'est discord.js-selfbot-v13 discordjs = require('discord.js-selfbot-v13'); } // Extraction des types/classes pour les utiliser dans le code const { CategoryChannel, Collection, Guild, GuildChannel, Snowflake, StageChannel, TextChannel, ThreadChannel, VoiceChannel } = discordjs; async function getBans(guild) { const bans = []; try { const cases = await guild.bans.fetch(); // Gets the list of the banned members cases.forEach((ban) => { bans.push({ id: ban.user.id, // Banned member ID reason: ban.reason // Ban reason }); }); } catch (error) { // If the bot doesn't have the permission to see the bans // It will throw an error, so we catch it and return an empty array return []; } return bans; } async function getMembers(guild) { const members = []; try { guild.members.cache.forEach((member) => { try { members.push({ userId: member.user.id, username: member.user.username, discriminator: member.user.discriminator, avatarUrl: member.user.avatarURL(), joinedTimestamp: member.joinedTimestamp, roles: member.roles?.cache?.map((role) => role.id) || [], bot: member.user.bot }); } catch (error) { // Si une erreur se produit pour un membre spécifique, on continue avec les autres console.error(`Erreur lors de la récupération du membre ${member?.user?.id || 'inconnu'}: ${error}`); } }); } catch (error) { // Si une erreur se produit lors de la récupération des membres, on retourne un tableau vide console.error(`Erreur lors de la récupération des membres: ${error}`); } return members; } async function getRoles(guild) { const roles = []; try { guild.roles.cache .filter((role) => !role.managed) .sort((a, b) => b.position - a.position) .forEach((role) => { try { const roleData = { name: role.name, color: role.hexColor, hoist: role.hoist, permissions: role.permissions?.bitfield?.toString() || '0', mentionable: role.mentionable, position: role.position, isEveryone: guild.id === role.id }; roles.push(roleData); } catch (error) { // Si une erreur se produit pour un rôle spécifique, on continue avec les autres console.error(`Erreur lors de la récupération du rôle ${role?.id || 'inconnu'}: ${error}`); } }); } catch (error) { // Si une erreur se produit lors de la récupération des rôles, on retourne un tableau vide console.error(`Erreur lors de la récupération des rôles: ${error}`); } return roles; } async function getEmojis(guild, options) { const emojis = []; try { const promises = []; guild.emojis.cache.forEach((emoji) => { const promise = (async () => { try { const eData = { name: emoji.name }; if (options.saveImages && options.saveImages === 'base64') { try { const response = await fetch(emoji.url); const buffer = await response.buffer(); eData.base64 = buffer.toString('base64'); } catch (fetchError) { // Si l'image ne peut pas être récupérée, on utilise l'URL à la place console.error(`Erreur lors de la récupération de l'image de l'emoji ${emoji.name}: ${fetchError}`); eData.url = emoji.url; } } else { eData.url = emoji.url; } emojis.push(eData); } catch (emojiError) { // Si une erreur se produit pour un emoji spécifique, on continue avec les autres console.error(`Erreur lors de la récupération de l'emoji ${emoji?.name || 'inconnu'}: ${emojiError}`); } })(); promises.push(promise); }); // Attendre que toutes les promesses soient résolues await Promise.all(promises); } catch (error) { // Si une erreur se produit lors de la récupération des emojis, on retourne un tableau vide console.error(`Erreur lors de la récupération des emojis: ${error}`); } return emojis; } async function getChannels(guild, options) { return new Promise(async (resolve) => { const channels = { categories: [], others: [] }; const categories = guild.channels.cache .filter((ch) => ch.type === ChannelType.GuildCategory || // Pour discord.js-selfbot-v13 ch.type === 'GUILD_CATEGORY') .sort((a, b) => ('position' in a && 'position' in b) ? a.position - b.position : 0) .map((category) => category); for (const category of categories) { // Typage explicite pour éviter les erreurs const typedCategory = category; const categoryData = { name: typedCategory.name, permissions: (0, util_1.fetchChannelPermissions)(typedCategory), children: [] }; // Récupérer les canaux enfants de manière compatible avec les deux versions let children = []; try { // discord.js v14 if (typedCategory.children && typedCategory.children.cache) { children = Array.from(typedCategory.children.cache.values()); } // discord.js-selfbot-v13 (structure différente) else if (typedCategory.children) { children = Array.from(typedCategory.children.values()); } // Autre structure possible dans discord.js-selfbot-v13 else { // Filtrer tous les canaux du serveur qui ont cette catégorie comme parent children = guild.channels.cache .filter((ch) => ch.parentId === typedCategory.id || ch.parent?.id === typedCategory.id) .map((ch) => ch); } // Trier les canaux par position children = children.sort((a, b) => { if (!a || !b || typeof a.position !== 'number' || typeof b.position !== 'number') return 0; return a.position - b.position; }); } catch (error) { children = []; } for (const child of children) { // Typage explicite pour éviter les erreurs const typedChild = child; if (typedChild.type === ChannelType.GuildText || typedChild.type === ChannelType.GuildAnnouncement || typedChild.type === ChannelType.GuildForum || typedChild.type === ChannelType.GuildMedia // Pour discord.js-selfbot-v13 || typedChild.type === 'GUILD_TEXT' || typedChild.type === 'GUILD_NEWS') { if (guild.rulesChannelId === typedChild.id || guild.safetyAlertsChannelId === typedChild.id || guild.widgetChannelId === typedChild.id || guild.publicUpdatesChannelId === typedChild.id) continue; const channelData = await (0, util_1.fetchTextChannelData)(typedChild, options); categoryData.children.push(channelData); } else if (typedChild.type === ChannelType.GuildStageVoice || typedChild.type === 'GUILD_STAGE_VOICE') { const channelData = await (0, util_1.fetchStageChannelData)(typedChild); channelData.userLimit = 0; categoryData.children.push(channelData); } else { const channelData = await (0, util_1.fetchVoiceChannelData)(typedChild); categoryData.children.push(channelData); } } channels.categories.push(categoryData); } const others = guild.channels.cache .filter((ch) => { return !ch.parent && (ch.type !== ChannelType.GuildCategory && ch.type !== 'GUILD_CATEGORY') && (ch.type !== ChannelType.AnnouncementThread && ch.type !== 'ANNOUNCEMENT_THREAD') && (ch.type !== ChannelType.PrivateThread && ch.type !== 'PRIVATE_THREAD') && (ch.type !== ChannelType.PublicThread && ch.type !== 'PUBLIC_THREAD'); }) .sort((a, b) => { if (!('position' in a) || !('position' in b)) return 0; return a.position - b.position; }) .map((channel) => channel); for (const channel of others) { // Typage explicite pour éviter les erreurs const typedChannel = channel; if (typedChannel.type === ChannelType.GuildText || typedChannel.type === ChannelType.GuildAnnouncement || typedChannel.type === ChannelType.GuildForum || typedChannel.type === ChannelType.GuildMedia // Pour discord.js-selfbot-v13 || typedChannel.type === 'GUILD_TEXT' || typedChannel.type === 'GUILD_NEWS') { if (guild.rulesChannelId === typedChannel.id || guild.safetyAlertsChannelId === typedChannel.id || guild.widgetChannelId === typedChannel.id || guild.publicUpdatesChannelId === typedChannel.id) continue; const channelData = await (0, util_1.fetchTextChannelData)(typedChannel, options); channels.others.push(channelData); } else if (typedChannel.type === ChannelType.GuildStageVoice || typedChannel.type === 'GUILD_STAGE_VOICE') { const channelData = await (0, util_1.fetchStageChannelData)(typedChannel); channelData.userLimit = 0; channels.others.push(channelData); } else { const channelData = await (0, util_1.fetchVoiceChannelData)(typedChannel); channels.others.push(channelData); } } resolve(channels); }); }