Comparing sensitive data, confidential files or internal emails?

Most legal and privacy policies prohibit uploading sensitive data online. Diffchecker Desktop ensures your confidential information never leaves your computer. Work offline and compare documents securely.

RestoreUtils.ts

Created Diff never expires
149 removals
259 lines
29 additions
150 lines
import type { BackupData, RestoreServerOptions } from "./types";
import type { BackupData, LoadOptions } from './types';
import {
import type { ChannelType, Emoji, Guild, GuildFeature, GuildChannel, Role, VoiceChannel } from 'discord.js';
Collection,
import { loadCategory, loadChannel } from './util';
Emoji,
Guild,
GuildChannel,
Role,
RoleResolvable,
Snowflake,
VoiceChannel,
} from "discord.js";
import { loadCategory, loadChannel } from "./BackupUtils";
import { MemoryCache } from "../clients/MemoryCache";


/**
/**
* Restores the guild configuration
* Restores the guild configuration
*/
*/
export const loadConfig = (
export const loadConfig = (guild: Guild, backupData: BackupData): Promise<Guild[]> => {
guild: Guild,
backupData: BackupData
): Promise<Guild[]> => {
const configPromises: Promise<Guild>[] = [];
const configPromises: Promise<Guild>[] = [];
if (backupData.name) {
if (backupData.name) {
configPromises.push(guild.setName(backupData.name));
configPromises.push(guild.setName(backupData.name));
}
}
if (backupData.iconBase64) {
if (backupData.iconBase64) {
configPromises.push(
configPromises.push(guild.setIcon(Buffer.from(backupData.iconBase64, 'base64')));
guild.setIcon(Buffer.from(backupData.iconBase64, "base64"))
);
} else if (backupData.iconURL) {
} else if (backupData.iconURL) {
configPromises.push(guild.setIcon(backupData.iconURL));
configPromises.push(guild.setIcon(backupData.iconURL));
}
}
if (backupData.splashBase64) {
if (backupData.splashBase64) {
configPromises.push(
configPromises.push(guild.setSplash(Buffer.from(backupData.splashBase64, 'base64')));
guild.setSplash(Buffer.from(backupData.splashBase64, "base64"))
);
} else if (backupData.splashURL) {
} else if (backupData.splashURL) {
configPromises.push(guild.setSplash(backupData.splashURL));
configPromises.push(guild.setSplash(backupData.splashURL));
}
}
if (backupData.bannerBase64) {
if (backupData.bannerBase64) {
configPromises.push(
configPromises.push(guild.setBanner(Buffer.from(backupData.bannerBase64, 'base64')));
guild.setBanner(Buffer.from(backupData.bannerBase64, "base64"))
);
} else if (backupData.bannerURL) {
} else if (backupData.bannerURL) {
configPromises.push(guild.setBanner(backupData.bannerURL));
configPromises.push(guild.setBanner(backupData.bannerURL));
}
}
if (backupData.verificationLevel) {
if (backupData.verificationLevel) {
configPromises.push(
configPromises.push(guild.setVerificationLevel(backupData.verificationLevel));
guild.setVerificationLevel(backupData.verificationLevel)
);
}
}
if (backupData.defaultMessageNotifications) {
if (backupData.defaultMessageNotifications) {
configPromises.push(
configPromises.push(guild.setDefaultMessageNotifications(backupData.defaultMessageNotifications));
guild.setDefaultMessageNotifications(
backupData.defaultMessageNotifications
)
);
}
}
const changeableExplicitLevel = guild.features.includes("COMMUNITY");
const changeableExplicitLevel = guild.features.includes(GuildFeature.Community);
if (backupData.explicitContentFilter && changeableExplicitLevel) {
if (backupData.explicitContentFilter && changeableExplicitLevel) {
configPromises.push(
configPromises.push(guild.setExplicitContentFilter(backupData.explicitContentFilter));
guild.setExplicitContentFilter(backupData.explicitContentFilter)
);
}
}
return Promise.all(configPromises);
return Promise.all(configPromises);
};
};


/**
/**
* Restore the guild roles
* Restore the guild roles
*/
*/
export const loadRoles = (
export const loadRoles = (guild: Guild, backupData: BackupData): Promise<Role[]> => {
guild: Guild,
backupData: BackupData,
cache?: MemoryCache
): Promise<Role[]> => {
const rolePromises: Promise<Role>[] = [];
const rolePromises: Promise<Role>[] = [];
backupData.roles.forEach((roleData) => {
backupData.roles.forEach((roleData) => {
if (roleData.isEveryone) {
if (roleData.isEveryone) {
rolePromises.push(
rolePromises.push(
guild.roles.cache.get(guild.id)!.edit({
guild.roles.cache.get(guild.id).edit({
name: roleData.name,
name: roleData.name,
color: roleData.color,
color: roleData.color,
permissions: BigInt(roleData.permissions),
permissions: BigInt(roleData.permissions),
mentionable: roleData.mentionable,
mentionable: roleData.mentionable
})
})
);
);
} else {
} else {
rolePromises.push(
rolePromises.push(
guild.roles.create({
guild.roles.create({
name: roleData.name,
name: roleData.name,
color: roleData.color,
color: roleData.color,
hoist: roleData.hoist,
hoist: roleData.hoist,
permissions: BigInt(roleData.permissions),
permissions: BigInt(roleData.permissions),
mentionable: roleData.mentionable,
mentionable: roleData.mentionable
})
})
);
);
}
}
});
});
cache?.setItem(`${guild.id}_roles`, JSON.stringify({ finished: true }));
return Promise.all(rolePromises);
return Promise.all(rolePromises);
};
};


/**
/**
* Restore the guild channels
* Restore the guild channels
*/
*/
export const loadChannels = (
export const loadChannels = (guild: Guild, backupData: BackupData, options: LoadOptions): Promise<unknown[]> => {
guild: Guild,
backupData: BackupData,
options: RestoreServerOptions
): Promise<unknown[]> => {
const loadChannelPromises: Promise<void | unknown>[] = [];
const loadChannelPromises: Promise<void | unknown>[] = [];
backupData.channels.categories.forEach((categoryData) => {
backupData.channels.categories.forEach((categoryData) => {
loadChannelPromises.push(
loadChannelPromises.push(
new Promise((resolve) => {
new Promise((resolve) => {
loadCategory(categoryData, guild).then((createdCategory: any) => {
loadCategory(categoryData, guild).then((createdCategory) => {
categoryData.children.forEach((channelData) => {
categoryData.children.forEach((channelData) => {
loadChannel(channelData, guild, createdCategory, options);
loadChannel(channelData, guild, createdCategory, options);
resolve(true);
resolve(true);
});
});
});
});
})
})
);
);
});
});
backupData.channels.others.forEach((channelData) => {
backupData.channels.others.forEach((channelData) => {
loadChannelPromises.push(loadChannel(channelData, guild, null, options));
loadChannelPromises.push(loadChannel(channelData, guild, null, options));
});
});
return Promise.all(loadChannelPromises);
return Promise.all(loadChannelPromises);
};
};


/**
/**
* Restore member roles.
*/
export const loadMemberRoles = async (
guild: Guild,
backupData: BackupData,
cache?: MemoryCache
) => {
// add roles after 10 seconds to make sure they got restored.
setTimeout(async () => {
const loadMemberRolesPromises: Promise<void | unknown>[] = [];
const members = await guild.members.fetch();
await guild.roles.fetch(undefined, {
cache: true,
});
backupData.members?.forEach((member) => {
const valid = members.find((m) => m.id === member.id);
if (!valid) return;
else {
const roles: Collection<Snowflake, Role> = new Collection<
Snowflake,
Role
>();
member.roles?.forEach((r) => {
const role = guild.roles.cache.find((v) => v.name === r.roleName);
if (role) {
if (!roles.get(role.id)) {
roles.set(role.id, role);
}
}
});
loadMemberRolesPromises.push(
valid.roles.set(roles, "Member Role Restore").catch(() => {})
);
}
});
cache?.deleteItem(`${guild.id}_roles`);
return Promise.all(loadMemberRolesPromises);
}, 10000);
};

/**
* Restore the afk configuration
* Restore the afk configuration
*/
*/
export const loadAFK = (
export const loadAFK = (guild: Guild, backupData: BackupData): Promise<Guild[]> => {
guild: Guild,
backupData: BackupData
): Promise<Guild[]> => {
const afkPromises: Promise<Guild>[] = [];
const afkPromises: Promise<Guild>[] = [];
if (backupData.afk) {
if (backupData.afk) {
afkPromises.push(
afkPromises.push(guild.setAFKChannel(guild.channels.cache.find((ch) => ch.name === backupData.afk.name && ch.type === ChannelType.GuildVoice) as VoiceChannel));
guild.setAFKChannel(
guild.channels.cache.find(
(ch) =>
// @ts-ignore
ch.name === backupData.afk.name && ch.type === "GUILD_VOICE"
) as VoiceChannel
)
);
afkPromises.push(guild.setAFKTimeout(backupData.afk.timeout));
afkPromises.push(guild.setAFKTimeout(backupData.afk.timeout));
}
}
return Promise.all(afkPromises);
return Promise.all(afkPromises);
};
};


/**
/**
* Restore guild emojis
* Restore guild emojis
*/
*/
export const loadEmojis = (
export const loadEmojis = (guild: Guild, backupData: BackupData): Promise<Emoji[]> => {
guild: Guild,
backupData: BackupData
): Promise<Emoji[]> => {
const emojiPromises: Promise<Emoji>[] = [];
const emojiPromises: Promise<Emoji>[] = [];
backupData.emojis.forEach((emoji) => {
backupData.emojis.forEach((emoji) => {
if (emoji.url) {
if (emoji.url) {
// @ts-ignore
emojiPromises.push(guild.emojis.create(emoji.url, emoji.name));
emojiPromises.push(guild.emojis.create(emoji.url, emoji.name));
} else if (emoji.base64) {
} else if (emoji.base64) {
emojiPromises.push(
emojiPromises.push(guild.emojis.create(Buffer.from(emoji.base64, 'base64'), emoji.name));
guild.emojis.create(
Buffer.from(emoji.base64, "base64"),
// @ts-ignore
emoji.name
)
);
}
}
});
});
return Promise.all(emojiPromises);
return Promise.all(emojiPromises);
};
};


/**
/**
* Restore guild bans
* Restore guild bans
*/
*/
export const loadBans = (
export const loadBans = (guild: Guild, backupData: BackupData): Promise<string[]> => {
guild: Guild,
backupData: BackupData
): Promise<string[]> => {
const banPromises: Promise<string>[] = [];
const banPromises: Promise<string>[] = [];
backupData.bans.forEach((ban) => {
backupData.bans.forEach((ban) => {
banPromises.push(
banPromises.push(
guild.members.ban(ban.id, {
guild.members.ban(ban.id, {
reason: ban.reason ?? undefined,
reason: ban.reason
}) as Promise<string>
}) as Promise<string>
);
);
});
});
return Promise.all(banPromises);
return Promise.all(banPromises);
};
};


/**
/**
* Restore embedChannel configuration
* Restore embedChannel configuration
*/
*/
export const loadEmbedChannel = (
export const loadEmbedChannel = (guild: Guild, backupData: BackupData): Promise<Guild[]> => {
guild: Guild,
backupData: BackupData
): Promise<Guild[]> => {
const embedChannelPromises: Promise<Guild>[] = [];
const embedChannelPromises: Promise<Guild>[] = [];
if (backupData.widget.channel) {
if (backupData.widget.channel) {
embedChannelPromises.push(
embedChannelPromises.push(
guild.setWidgetSettings({
guild.setWidgetSettings({
// @ts-ignore
enabled: backupData.widget.enabled,
enabled: backupData.widget.enabled,
// @ts-ignore
channel: guild.channels.cache.find((ch) => ch.name === backupData.widget.channel)
channel: guild.channels.cache.find(
(ch) => ch.name === backupData.widget.channel
),
})
})
);
);
}
}
return Promise.all(embedChannelPromises);
return Promise.all(embedChannelPromises);
};
};