Skip to content

Commit e93677e

Browse files
committed
🔖 v3.3.1
1 parent 5cb05fc commit e93677e

11 files changed

+251
-125
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discord-backup",
3-
"version": "3.3.0",
3+
"version": "3.3.1",
44
"description": "A complete framework to facilitate server backup using discord.js v12",
55
"main": "lib/index.js",
66
"files": [

src/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type {
88
TextChannelData,
99
VoiceChannelData
1010
} from './types';
11-
import type { CategoryChannel, ChannelType, Collection, Guild, GuildChannel, Snowflake, TextChannel, ThreadChannel, VoiceChannel } from 'discord.js';
11+
import type { CategoryChannel, Collection, Guild, GuildChannel, Snowflake, TextChannel, ThreadChannel, VoiceChannel } from 'discord.js';
12+
import { ChannelType } from 'discord.js';
1213
import nodeFetch from 'node-fetch';
1314
import { fetchChannelPermissions, fetchTextChannelData, fetchVoiceChannelData } from './util';
1415
import { MemberData } from './types/MemberData';
@@ -122,7 +123,7 @@ export async function getChannels(guild: Guild, options: CreateOptions) {
122123
children: [] // The children channels of the category
123124
};
124125
// Gets the children channels of the category and sort them by position
125-
const children = category.children.sort((a, b) => a.position - b.position).toJSON();
126+
const children = category.children.cache.sort((a, b) => a.position - b.position).toJSON();
126127
for (const child of children) {
127128
// For each child channel
128129
if (child.type === ChannelType.GuildText || child.type === ChannelType.GuildNews) {
@@ -146,7 +147,7 @@ export async function getChannels(guild: Guild, options: CreateOptions) {
146147
.toJSON();
147148
for (const channel of others) {
148149
// For each channel
149-
if (channel.type === ChnanelType.GuildText || channel.type === ChannelType.GuildNews) {
150+
if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildNews) {
150151
const channelData: TextChannelData = await fetchTextChannelData(channel as TextChannel, options); // Gets the channel data
151152
channels.others.push(channelData); // Update channels object
152153
} else {

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ export const create = async (
9797
members: [],
9898
createdTimestamp: Date.now(),
9999
guildID: guild.id,
100-
id: options.backupID ?? SnowflakeUtil.generate(Date.now())
100+
id: options.backupID ?? SnowflakeUtil.generate().toString()
101101
};
102102
if (guild.iconURL()) {
103103
if (options && options.saveImages && options.saveImages === 'base64') {
104104
backupData.iconBase64 = (
105-
await nodeFetch(guild.iconURL({ dynamic: true })).then((res) => res.buffer())
105+
await nodeFetch(guild.iconURL()).then((res) => res.buffer())
106106
).toString('base64');
107107
}
108-
backupData.iconURL = guild.iconURL({ dynamic: true });
108+
backupData.iconURL = guild.iconURL();
109109
}
110110
if (guild.splashURL()) {
111111
if (options && options.saveImages && options.saveImages === 'base64') {

src/load.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BackupData, LoadOptions } from './types';
2-
import type { ChannelType, Emoji, Guild, GuildFeature, GuildChannel, Role, VoiceChannel } from 'discord.js';
2+
import type { NewsChannel, TextChannel, ForumChannel, VoiceBasedChannel } from 'discord.js';
3+
import { ChannelType, Emoji, Guild, GuildFeature, Role, VoiceChannel } from 'discord.js';
34
import { loadCategory, loadChannel } from './util';
45

56
/**
@@ -110,9 +111,15 @@ export const loadEmojis = (guild: Guild, backupData: BackupData): Promise<Emoji[
110111
const emojiPromises: Promise<Emoji>[] = [];
111112
backupData.emojis.forEach((emoji) => {
112113
if (emoji.url) {
113-
emojiPromises.push(guild.emojis.create(emoji.url, emoji.name));
114+
emojiPromises.push(guild.emojis.create({
115+
name: emoji.name,
116+
attachment: emoji.url
117+
}));
114118
} else if (emoji.base64) {
115-
emojiPromises.push(guild.emojis.create(Buffer.from(emoji.base64, 'base64'), emoji.name));
119+
emojiPromises.push(guild.emojis.create({
120+
name: emoji.name,
121+
attachment: Buffer.from(emoji.base64, 'base64')
122+
}));
116123
}
117124
});
118125
return Promise.all(emojiPromises);
@@ -142,7 +149,7 @@ export const loadEmbedChannel = (guild: Guild, backupData: BackupData): Promise<
142149
embedChannelPromises.push(
143150
guild.setWidgetSettings({
144151
enabled: backupData.widget.enabled,
145-
channel: guild.channels.cache.find((ch) => ch.name === backupData.widget.channel)
152+
channel: guild.channels.cache.find((ch) => ch.name === backupData.widget.channel) as NewsChannel | TextChannel | ForumChannel | VoiceBasedChannel
146153
})
147154
);
148155
}

src/types/BackupData.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { DefaultMessageNotificationLevel, ExplicitContentFilterLevel, Snowflake, VerificationLevel } from 'discord.js';
1+
import { GuildDefaultMessageNotifications, GuildExplicitContentFilter, Snowflake, GuildVerificationLevel } from 'discord.js';
22
import { AfkData, BanData, ChannelsData, EmojiData, RoleData, WidgetData } from './';
33
import { MemberData } from './MemberData';
44

55
export interface BackupData {
66
name: string;
77
iconURL?: string;
88
iconBase64?: string;
9-
verificationLevel: VerificationLevel;
10-
explicitContentFilter: ExplicitContentFilterLevel;
11-
defaultMessageNotifications: DefaultMessageNotificationLevel | number;
9+
verificationLevel: GuildVerificationLevel;
10+
explicitContentFilter: GuildExplicitContentFilter;
11+
defaultMessageNotifications: GuildDefaultMessageNotifications | number;
1212
afk?: AfkData;
1313
widget: WidgetData;
1414
splashURL?: string;

src/types/BaseChannelData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { TextBasedChannelTypes, VoiceBasedChannelTypes, ThreadChannelTypes } from 'discord.js';
1+
import { TextBasedChannelTypes, VoiceBasedChannelTypes, ThreadChannelType } from 'discord.js';
22
import { ChannelPermissionsData } from './';
33

44
export interface BaseChannelData {
5-
type: TextBasedChannelTypes | VoiceBasedChannelTypes | ThreadChannelTypes;
5+
type: TextBasedChannelTypes | VoiceBasedChannelTypes | ThreadChannelType;
66
name: string;
77
parent?: string;
88
permissions: ChannelPermissionsData[];

src/types/MessageData.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { MessageEmbed, FileOptions } from 'discord.js';
1+
import { APIEmbed } from 'discord.js';
22

33
export interface MessageData {
44
username: string;
55
avatar?: string;
66
content?: string;
7-
embeds?: MessageEmbed[];
8-
files?: FileOptions[];
7+
embeds?: APIEmbed[];
8+
files?: {
9+
name: string;
10+
attachment: string;
11+
}[];
912
pinned?: boolean;
1013
sentAt: string;
1114
}

src/types/ThreadChannelData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Snowflake, ThreadAutoArchiveDuration, ThreadChannelTypes } from "discord.js";
1+
import { Snowflake, ThreadAutoArchiveDuration, ThreadChannelType } from "discord.js";
22
import { MessageData } from "./MessageData";
33

44
export interface ThreadChannelData {
5-
type: ThreadChannelTypes;
5+
type: ThreadChannelType;
66
name: string;
77
archived: boolean;
88
autoArchiveDuration: ThreadAutoArchiveDuration;

src/types/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { GuildFeatures } from 'discord.js';
2-
31
export * from './AfkData';
42
export * from './BackupData';
53
export * from './BackupInfos';

src/util.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import type {
88
ThreadChannelData,
99
VoiceChannelData
1010
} from './types';
11-
import type {
11+
import {
1212
CategoryChannel,
13-
ChannelLogsQueryOptions,
1413
ChannelType,
1514
Collection,
1615
Guild,
@@ -24,18 +23,22 @@ import type {
2423
TextChannel,
2524
VoiceChannel,
2625
NewsChannel,
27-
PremiumTier,
2826
ThreadChannel,
29-
GuildFeatures,
30-
Webhook
27+
Webhook,
28+
GuildPremiumTier,
29+
GuildExplicitContentFilter,
30+
GuildVerificationLevel,
31+
FetchMessagesOptions,
32+
OverwriteType,
33+
AttachmentBuilder
3134
} from 'discord.js';
3235
import nodeFetch from 'node-fetch';
3336

34-
const MaxBitratePerTier: Record<PremiumTier, number> = {
35-
None: 64000,
36-
Tier1: 128000,
37-
Tier2: 256000,
38-
Tier3: 384000
37+
const MaxBitratePerTier: Record<GuildPremiumTier, number> = {
38+
[GuildPremiumTier.None]: 64000,
39+
[GuildPremiumTier.Tier1]: 128000,
40+
[GuildPremiumTier.Tier2]: 256000,
41+
[GuildPremiumTier.Tier3]: 384000
3942
};
4043

4144
/**
@@ -44,7 +47,7 @@ const MaxBitratePerTier: Record<PremiumTier, number> = {
4447
export function fetchChannelPermissions(channel: TextChannel | VoiceChannel | CategoryChannel | NewsChannel) {
4548
const permissions: ChannelPermissionsData[] = [];
4649
channel.permissionOverwrites.cache
47-
.filter((p) => p.type === 'role')
50+
.filter((p) => p.type === OverwriteType.Role)
4851
.forEach((perm) => {
4952
// For each overwrites permission
5053
const role = channel.guild.roles.cache.get(perm.id);
@@ -80,7 +83,7 @@ export async function fetchVoiceChannelData(channel: VoiceChannel) {
8083
export async function fetchChannelMessages (channel: TextChannel | NewsChannel | ThreadChannel, options: CreateOptions): Promise<MessageData[]> {
8184
let messages: MessageData[] = [];
8285
const messageCount: number = isNaN(options.maxMessagesPerChannel) ? 10 : options.maxMessagesPerChannel;
83-
const fetchOptions: ChannelLogsQueryOptions = { limit: 100 };
86+
const fetchOptions: FetchMessagesOptions = { limit: 100 };
8487
let lastMessageId: Snowflake;
8588
let fetchComplete: boolean = false;
8689
while (!fetchComplete) {
@@ -179,7 +182,8 @@ export async function fetchTextChannelData(channel: TextChannel | NewsChannel, o
179182
*/
180183
export async function loadCategory(categoryData: CategoryData, guild: Guild) {
181184
return new Promise<CategoryChannel>((resolve) => {
182-
guild.channels.create(categoryData.name, {
185+
guild.channels.create({
186+
name: categoryData.name,
183187
type: ChannelType.GuildCategory
184188
}).then(async (category) => {
185189
// When the category is created
@@ -229,7 +233,9 @@ export async function loadChannel(
229233
username: msg.username,
230234
avatarURL: msg.avatar,
231235
embeds: msg.embeds,
232-
files: msg.files,
236+
files: msg.files.map((f) => new AttachmentBuilder(f.attachment, {
237+
name: f.name
238+
})),
233239
allowedMentions: options.allowedMentions,
234240
threadId: channel.isThread() ? channel.id : undefined
235241
})
@@ -258,7 +264,7 @@ export async function loadChannel(
258264
let bitrate = (channelData as VoiceChannelData).bitrate;
259265
const bitrates = Object.values(MaxBitratePerTier);
260266
while (bitrate > MaxBitratePerTier[guild.premiumTier]) {
261-
bitrate = bitrates[Object.keys(MaxBitratePerTier).indexOf(guild.premiumTier) - 1];
267+
bitrate = bitrates[guild.premiumTier];
262268
}
263269
createOptions.bitrate = bitrate;
264270
createOptions.userLimit = (channelData as VoiceChannelData).userLimit;

0 commit comments

Comments
 (0)