forked from Jisagi/Discord-guild-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.js
142 lines (123 loc) · 5.51 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
135
136
137
138
139
140
141
142
const fs = require('fs');
const path = require('path');
const Discord = require('discord.js');
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();
let isBackup = false;
let isRestore = false;
let backupFile = 'guildData.json';
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 {
// npm v5 check (included since node v8)
if (!fs.existsSync('package-lock.json')) throw new Error(Translator.disp('errorNPM1'));
// Check discord.js version
if (settings.djsVersionCheck) {
let djs = require('./package-lock.json').dependencies['discord.js'].version;
let localVersion = djs.split('#')[1];
let latestVersion = await VersionControl.checkLibraryVersion(Translator);
if (localVersion !== latestVersion.sha) throw new Error(Translator.disp('errorNPM2'));
Logger.logMessage(Translator.disp('messageDjsVersionCheckSuccess'));
}
// 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) data = Validator.validateSettings(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 {
if (!client.guilds.has(originalGuildId)) {
throw new Error(Translator.disp('errorSerializationOriginalNotExistent'));
}
let banCollection = new Discord.Collection();
try {
if (settings.copyBans) banCollection = await client.guilds.get(originalGuildId).fetchBans();
} catch (banError) {
throw new Error(Translator.disp('errorSerializationNoBanPermissions'));
}
guildData = Serializer.serializeOldGuild(client, originalGuildId, banCollection, guildData, backupFile, Translator);
}
// Stop on backup only
if (isBackup) {
Logger.logMessage(Translator.disp('messageBackupDone', [guildData.step]));
await client.destroy();
return process.exit(0);
}
// Cleanup new guild
guildData = await Cleaner.cleanNewGuild(client, newGuildId, newGuildAdminRoleId, guildData, Translator);
// Create new guild
await Creator.setData(client, guildData, newGuildId, newGuildAdminRoleId, Translator);
} catch (err) {
Logger.logError(err);
}
await 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';
isRestore = args[0] === 'restore';
client.login(settings.token);
}
main();