Skip to content

Commit

Permalink
🔖 v3.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Jan 5, 2023
1 parent 5cb05fc commit e93677e
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 125 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord-backup",
"version": "3.3.0",
"version": "3.3.1",
"description": "A complete framework to facilitate server backup using discord.js v12",
"main": "lib/index.js",
"files": [
Expand Down
7 changes: 4 additions & 3 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type {
TextChannelData,
VoiceChannelData
} from './types';
import type { CategoryChannel, ChannelType, Collection, Guild, GuildChannel, Snowflake, TextChannel, ThreadChannel, VoiceChannel } from 'discord.js';
import type { CategoryChannel, Collection, Guild, GuildChannel, Snowflake, TextChannel, ThreadChannel, VoiceChannel } from 'discord.js';
import { ChannelType } from 'discord.js';
import nodeFetch from 'node-fetch';
import { fetchChannelPermissions, fetchTextChannelData, fetchVoiceChannelData } from './util';
import { MemberData } from './types/MemberData';
Expand Down Expand Up @@ -122,7 +123,7 @@ export async function getChannels(guild: Guild, options: CreateOptions) {
children: [] // The children channels of the category
};
// Gets the children channels of the category and sort them by position
const children = category.children.sort((a, b) => a.position - b.position).toJSON();
const children = category.children.cache.sort((a, b) => a.position - b.position).toJSON();
for (const child of children) {
// For each child channel
if (child.type === ChannelType.GuildText || child.type === ChannelType.GuildNews) {
Expand All @@ -146,7 +147,7 @@ export async function getChannels(guild: Guild, options: CreateOptions) {
.toJSON();
for (const channel of others) {
// For each channel
if (channel.type === ChnanelType.GuildText || channel.type === ChannelType.GuildNews) {
if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildNews) {
const channelData: TextChannelData = await fetchTextChannelData(channel as TextChannel, options); // Gets the channel data
channels.others.push(channelData); // Update channels object
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export const create = async (
members: [],
createdTimestamp: Date.now(),
guildID: guild.id,
id: options.backupID ?? SnowflakeUtil.generate(Date.now())
id: options.backupID ?? SnowflakeUtil.generate().toString()
};
if (guild.iconURL()) {
if (options && options.saveImages && options.saveImages === 'base64') {
backupData.iconBase64 = (
await nodeFetch(guild.iconURL({ dynamic: true })).then((res) => res.buffer())
await nodeFetch(guild.iconURL()).then((res) => res.buffer())
).toString('base64');
}
backupData.iconURL = guild.iconURL({ dynamic: true });
backupData.iconURL = guild.iconURL();
}
if (guild.splashURL()) {
if (options && options.saveImages && options.saveImages === 'base64') {
Expand Down
15 changes: 11 additions & 4 deletions src/load.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BackupData, LoadOptions } from './types';
import type { ChannelType, Emoji, Guild, GuildFeature, GuildChannel, Role, VoiceChannel } from 'discord.js';
import type { NewsChannel, TextChannel, ForumChannel, VoiceBasedChannel } from 'discord.js';
import { ChannelType, Emoji, Guild, GuildFeature, Role, VoiceChannel } from 'discord.js';
import { loadCategory, loadChannel } from './util';

/**
Expand Down Expand Up @@ -110,9 +111,15 @@ export const loadEmojis = (guild: Guild, backupData: BackupData): Promise<Emoji[
const emojiPromises: Promise<Emoji>[] = [];
backupData.emojis.forEach((emoji) => {
if (emoji.url) {
emojiPromises.push(guild.emojis.create(emoji.url, emoji.name));
emojiPromises.push(guild.emojis.create({
name: emoji.name,
attachment: emoji.url
}));
} else if (emoji.base64) {
emojiPromises.push(guild.emojis.create(Buffer.from(emoji.base64, 'base64'), emoji.name));
emojiPromises.push(guild.emojis.create({
name: emoji.name,
attachment: Buffer.from(emoji.base64, 'base64')
}));
}
});
return Promise.all(emojiPromises);
Expand Down Expand Up @@ -142,7 +149,7 @@ export const loadEmbedChannel = (guild: Guild, backupData: BackupData): Promise<
embedChannelPromises.push(
guild.setWidgetSettings({
enabled: backupData.widget.enabled,
channel: guild.channels.cache.find((ch) => ch.name === backupData.widget.channel)
channel: guild.channels.cache.find((ch) => ch.name === backupData.widget.channel) as NewsChannel | TextChannel | ForumChannel | VoiceBasedChannel
})
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/BackupData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { DefaultMessageNotificationLevel, ExplicitContentFilterLevel, Snowflake, VerificationLevel } from 'discord.js';
import { GuildDefaultMessageNotifications, GuildExplicitContentFilter, Snowflake, GuildVerificationLevel } from 'discord.js';
import { AfkData, BanData, ChannelsData, EmojiData, RoleData, WidgetData } from './';
import { MemberData } from './MemberData';

export interface BackupData {
name: string;
iconURL?: string;
iconBase64?: string;
verificationLevel: VerificationLevel;
explicitContentFilter: ExplicitContentFilterLevel;
defaultMessageNotifications: DefaultMessageNotificationLevel | number;
verificationLevel: GuildVerificationLevel;
explicitContentFilter: GuildExplicitContentFilter;
defaultMessageNotifications: GuildDefaultMessageNotifications | number;
afk?: AfkData;
widget: WidgetData;
splashURL?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/types/BaseChannelData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TextBasedChannelTypes, VoiceBasedChannelTypes, ThreadChannelTypes } from 'discord.js';
import { TextBasedChannelTypes, VoiceBasedChannelTypes, ThreadChannelType } from 'discord.js';
import { ChannelPermissionsData } from './';

export interface BaseChannelData {
type: TextBasedChannelTypes | VoiceBasedChannelTypes | ThreadChannelTypes;
type: TextBasedChannelTypes | VoiceBasedChannelTypes | ThreadChannelType;
name: string;
parent?: string;
permissions: ChannelPermissionsData[];
Expand Down
9 changes: 6 additions & 3 deletions src/types/MessageData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { MessageEmbed, FileOptions } from 'discord.js';
import { APIEmbed } from 'discord.js';

export interface MessageData {
username: string;
avatar?: string;
content?: string;
embeds?: MessageEmbed[];
files?: FileOptions[];
embeds?: APIEmbed[];
files?: {
name: string;
attachment: string;
}[];
pinned?: boolean;
sentAt: string;
}
4 changes: 2 additions & 2 deletions src/types/ThreadChannelData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Snowflake, ThreadAutoArchiveDuration, ThreadChannelTypes } from "discord.js";
import { Snowflake, ThreadAutoArchiveDuration, ThreadChannelType } from "discord.js";
import { MessageData } from "./MessageData";

export interface ThreadChannelData {
type: ThreadChannelTypes;
type: ThreadChannelType;
name: string;
archived: boolean;
autoArchiveDuration: ThreadAutoArchiveDuration;
Expand Down
2 changes: 0 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { GuildFeatures } from 'discord.js';

export * from './AfkData';
export * from './BackupData';
export * from './BackupInfos';
Expand Down
36 changes: 21 additions & 15 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import type {
ThreadChannelData,
VoiceChannelData
} from './types';
import type {
import {
CategoryChannel,
ChannelLogsQueryOptions,
ChannelType,
Collection,
Guild,
Expand All @@ -24,18 +23,22 @@ import type {
TextChannel,
VoiceChannel,
NewsChannel,
PremiumTier,
ThreadChannel,
GuildFeatures,
Webhook
Webhook,
GuildPremiumTier,
GuildExplicitContentFilter,
GuildVerificationLevel,
FetchMessagesOptions,
OverwriteType,
AttachmentBuilder
} from 'discord.js';
import nodeFetch from 'node-fetch';

const MaxBitratePerTier: Record<PremiumTier, number> = {
None: 64000,
Tier1: 128000,
Tier2: 256000,
Tier3: 384000
const MaxBitratePerTier: Record<GuildPremiumTier, number> = {
[GuildPremiumTier.None]: 64000,
[GuildPremiumTier.Tier1]: 128000,
[GuildPremiumTier.Tier2]: 256000,
[GuildPremiumTier.Tier3]: 384000
};

/**
Expand All @@ -44,7 +47,7 @@ const MaxBitratePerTier: Record<PremiumTier, number> = {
export function fetchChannelPermissions(channel: TextChannel | VoiceChannel | CategoryChannel | NewsChannel) {
const permissions: ChannelPermissionsData[] = [];
channel.permissionOverwrites.cache
.filter((p) => p.type === 'role')
.filter((p) => p.type === OverwriteType.Role)
.forEach((perm) => {
// For each overwrites permission
const role = channel.guild.roles.cache.get(perm.id);
Expand Down Expand Up @@ -80,7 +83,7 @@ export async function fetchVoiceChannelData(channel: VoiceChannel) {
export async function fetchChannelMessages (channel: TextChannel | NewsChannel | ThreadChannel, options: CreateOptions): Promise<MessageData[]> {
let messages: MessageData[] = [];
const messageCount: number = isNaN(options.maxMessagesPerChannel) ? 10 : options.maxMessagesPerChannel;
const fetchOptions: ChannelLogsQueryOptions = { limit: 100 };
const fetchOptions: FetchMessagesOptions = { limit: 100 };
let lastMessageId: Snowflake;
let fetchComplete: boolean = false;
while (!fetchComplete) {
Expand Down Expand Up @@ -179,7 +182,8 @@ export async function fetchTextChannelData(channel: TextChannel | NewsChannel, o
*/
export async function loadCategory(categoryData: CategoryData, guild: Guild) {
return new Promise<CategoryChannel>((resolve) => {
guild.channels.create(categoryData.name, {
guild.channels.create({
name: categoryData.name,
type: ChannelType.GuildCategory
}).then(async (category) => {
// When the category is created
Expand Down Expand Up @@ -229,7 +233,9 @@ export async function loadChannel(
username: msg.username,
avatarURL: msg.avatar,
embeds: msg.embeds,
files: msg.files,
files: msg.files.map((f) => new AttachmentBuilder(f.attachment, {
name: f.name
})),
allowedMentions: options.allowedMentions,
threadId: channel.isThread() ? channel.id : undefined
})
Expand Down Expand Up @@ -258,7 +264,7 @@ export async function loadChannel(
let bitrate = (channelData as VoiceChannelData).bitrate;
const bitrates = Object.values(MaxBitratePerTier);
while (bitrate > MaxBitratePerTier[guild.premiumTier]) {
bitrate = bitrates[Object.keys(MaxBitratePerTier).indexOf(guild.premiumTier) - 1];
bitrate = bitrates[guild.premiumTier];
}
createOptions.bitrate = bitrate;
createOptions.userLimit = (channelData as VoiceChannelData).userLimit;
Expand Down
Loading

0 comments on commit e93677e

Please sign in to comment.