forked from Jisagi/Discord-guild-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.js
134 lines (115 loc) · 5.25 KB
/
copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fs = require('fs');
const path = require('path');
const Discord = require('discord.js');
const lockfile = require('@yarnpkg/lockfile');
const VersionControl = require('./objects/versioncontrol');
const Validator = require('./objects/validator');
const Serializer = require('./objects/serializer');
const Cleaner = require('./objects/cleaner');
const Creator = require('./objects/creator');
const Logger = require('./objects/logger');
const Translator = require('./objects/translator');
const settings = require('./settings.json');
const client = new Discord.Client({ intents: [Discord.IntentsBitField.Flags.Guilds, Discord.IntentsBitField.Flags.GuildMembers] });
let isBackup = false;
let isRestore = false;
let isClone = false;
let backupFile = 'guildData.json';
console.log(`nodejs: ${process.version}`);
Validator.validateNodeVersion(Translator);
client.on('ready', async () => {
await Translator.loadTranslations().catch(langError => {
console.error(langError);
return process.exit(1);
});
if (!['all', 'error', 'none'].includes(settings.output)) {
console.error(Translator.disp('errorOutputParameter'));
return process.exit(1);
}
let lang = Translator.getLanguage();
Logger.logMessage(Translator.disp('messageLanguageAuthor', lang['language'], lang['author']));
if (!client.user.bot) {
Logger.logError(Translator.disp('errorUserToken'));
return process.exit(1);
}
Logger.logMessage(Translator.disp('messsageLogin', client.user.tag));
let originalGuildId = settings.originalGuildId;
let newGuildId = settings.newGuildId;
let newGuildAdminRoleId = settings.newGuildAdminRoleId;
let guildData = { step: 1 };
try {
// Check script version
let { version } = require('./package.json');
let result = await VersionControl.checkVersion(Translator).catch(err => {
return { error: err || new Error(Translator.disp('errorUnspecified')) };
});
if (result.error) Logger.logMessage(Translator.disp('errorVersionCheckOther', result.error))
else if (version !== result.version) throw new Error(Translator.disp('errorVersionCheckOutdated', version, result.version));
if (!result.error) Logger.logMessage(Translator.disp('messageScriptVersionCheckSuccess'));
// Settings Validation only on restore or clone
let data = { changed: false };
if (isBackup || isClone) Validator.validateSettingsBackup(client, originalGuildId, settings.copy.Bans, Translator);
if (isRestore || isClone) data = Validator.validateSettingsRestore(client, originalGuildId, newGuildId, newGuildAdminRoleId, Translator);
if (data.changed) newGuildAdminRoleId = data.newGuildAdminRoleId;
// Load/Create serialized guildData
if (fs.existsSync(backupFile) && isRestore) {
guildData = require(`./${backupFile}`);
guildData.step = 1;
Logger.logMessage(Translator.disp('messageSerialized', guildData.step++));
} else if (isRestore) {
throw new Error(Translator.disp('errorRestoreNotExistent', backupFile));
} else {
let banCollection = new Discord.Collection();
if (settings.copy.Bans) banCollection = await client.guilds.cache.get(originalGuildId).bans.fetch();
guildData = Serializer.serializeOldGuild(client, originalGuildId, banCollection, guildData, backupFile, Translator);
}
// Stop on backup only
if (isBackup) {
Logger.logMessage(Translator.disp('messageBackupDone', guildData.step));
client.destroy();
return process.exit(0);
}
// Cleanup new guild
if (settings.cleanup) guildData = await Cleaner.cleanNewGuild(client, newGuildId, newGuildAdminRoleId, guildData, Translator);
// Create new guild
guildData = await Creator.setData(client, guildData, newGuildId, newGuildAdminRoleId, Translator);
// Finalize
if (settings.copy.RCC) await Creator.finalize(client, originalGuildId, newGuildId, newGuildAdminRoleId, guildData, Translator);
else Logger.logMessage(Translator.disp('scriptFinished', guildData.step));
} catch (err) {
Logger.logError(err);
}
client.destroy();
return process.exit(0);
});
client.on('rateLimit', rateLimitObj => {
if (settings.debug) {
Logger.logError(`Rate limit reached! Method: ${rateLimitObj.method}, Path: ${rateLimitObj.path}`);
}
});
function printUsage() {
console.log(
`Usage:
* Backup guild to file: node copy.js backup <backupFile (optional)>
* Restore guild from file: node copy.js restore <backupFile (optional)>
* Clone guild to guild: node copy.js clone`
);
process.exit(1);
}
function main() {
const args = process.argv.slice(2);
if (args.length < 1 || !['backup', 'restore', 'clone'].includes(args[0])) {
printUsage();
} else if (args.length >= 2 && ['backup', 'restore'].includes(args[0])) {
if (path.extname(args[1]) === '.json') {
backupFile = args[1];
} else {
backupFile = args[1] + '.json';
}
}
isBackup = args[0] === 'backup';
isClone = args[0] === 'clone';
isRestore = args[0] === 'restore';
client.login(settings.token);
}
main();