From 06649f244d23b36e82262b26008eeca3a5cb2c15 Mon Sep 17 00:00:00 2001 From: Jisagi Date: Mon, 24 Dec 2018 13:24:16 +0100 Subject: [PATCH] multiple additions & fixes v1.1.4 - discord.js version check added (optinal) - error logging fixed (again) - readme disclaimer adjusted - issue template addded --- .github/ISSUE_TEMPLATE.md | 8 ++++++++ .gitignore | 2 +- README.md | 15 ++++++--------- copy.js | 20 +++++++++++++------- objects/logger.js | 2 +- objects/versioncontrol.js | 34 +++++++++++++++++++++++++++++++--- package.json | 4 ++-- settings.json | 1 + translations/de.json | 7 ++++--- translations/en.json | 7 ++++--- 10 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..09b6534 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,8 @@ +**Please describe the problem you are having in detail:** + +Please describe the problem in as much detail as possible. Please include any thrown error codes as well. + +**Additional information:** + +- Node.js version: +- Operating system: diff --git a/.gitignore b/.gitignore index b945f74..78bef8d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,6 @@ node_modules/ package-lock.json # Miscellaneous -guildData.json +guildData*.json logs/ .vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 1a3a0e8..5c68f79 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ ## Information Did you ever want to create a copy of a guild? Now you can! There is no need to manually create roles, channels and permissions, which can take very long depending on the original guilds size. The script does everything for you. It backs up a guild into a single file and lets you create a new guild from it. You can share your file with others, too. -The following guide is also available in [french](https://github.com/Jisagi/Discord-guild-copy/blob/master/README.fr.md) thanks to TheLightSpirit. - ### What will be copied - Categories - Text & Voice channels @@ -29,9 +27,7 @@ The following guide is also available in [french](https://github.com/Jisagi/Disc - A preferably empty guild which can be overwritten ## Disclaimer -**_Use this script at your own risk!_** - -According to the recent [statement](https://support.discordapp.com/hc/en-us/articles/115002192352-Automated-user-accounts-self-bots000000) of the discord team, any kind of user accounts including so called SelfBots are forbidden. The script must be run with a bot user token. +**The script must be run with a bot user token.** According to discords [statement](https://support.discordapp.com/hc/en-us/articles/115002192352-Automated-user-accounts-self-bots000000), any kind of user account botting including so called SelfBots are forbidden. The script will therefore block the execution with a user account token. ## Installation & Usage 1. Download the repository from github @@ -49,11 +45,12 @@ To get the id of a guild open your client settings -> Appearance and then enable | originalGuildId | The id of the guild you want to clone. Can be left blank if a guildData.json already exists. | | newGuildId | The id of the new guild you want to clone to. | | newGuildAdminRoleId | The id of a role with administrator permissions. The bot needs to have this role on the new guild! You can manually create a new role called 'guildcopy' and the script will automatically use it. If you do so, just leave this field empty. | -| copyEmojis | default: false - set to true to copy emojis (see also [Common Behaviour](https://github.com/Jisagi/Discord-guild-copy#common-behaviour)) | +| copyEmojis | default: false - Set to true to copy emojis (see also [Common Behaviour](https://github.com/Jisagi/Discord-guild-copy#common-behaviour)) | | copyBans | default: false - set to true to copy banned users The bot needs to have the BAN_MEMBERS permission on the original guild if you enable this! | -| language | default: en - set this to any supported language from the `translations` folder | +| language | default: en - Set this to any supported language from the `translations` folder | | output | default: all - Possible values: `all` for everything, `error` for errors only, `none` for no output at all | -| debug | default: false - set to true for a more detailed general and error output e.g. when creating an issue | +| djsVersionCheck | default: true - Checks the local discord.js version by its commit hash. If you periodically run this script, you might want to disable this | +| debug | default: false - Set to true for a more detailed general and error output e.g. when creating an issue | | token | Your account token. The bot does not need any permissions on the original guild (only exception: copyBans=true). | ## Common Behaviour @@ -84,5 +81,5 @@ This software is licensed under the terms of the GPLv3. For more details see [LI ## Credits - [EmberVulpix](https://github.com/EmberVulpix) for the command line arguments -- [TheLightSpirit](https://github.com/TheLightSpirit) for the french README translation +- [TheLightSpirit](https://github.com/TheLightSpirit) for the french README translation (not always up-to-date) - Everybody who helps adding new features and finding bugs! \ No newline at end of file diff --git a/copy.js b/copy.js index d3046f7..5c2d206 100644 --- a/copy.js +++ b/copy.js @@ -41,9 +41,17 @@ client.on('ready', async () => { 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 - let djsVersion = require('./node_modules/discord.js/package.json').version; - if (djsVersion !== '12.0.0-dev') throw new Error(Translator.disp('errorNPM')); + 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'); @@ -52,7 +60,7 @@ client.on('ready', async () => { }); 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('messageVersionCheckSuccess')); + if (!result.error) Logger.logMessage(Translator.disp('messageScriptVersionCheckSuccess')); // Settings Validation only on restore or clone let data = { changed: false }; @@ -101,9 +109,7 @@ client.on('ready', async () => { client.on('rateLimit', rateLimitObj => { if (settings.debug) { - Logger.logError(`Rate limit reached!\nTimeout: ${rateLimitObj.timeout}\nLimit: ${rateLimitObj.limit}\n` + - `TimeDiff: ${rateLimitObj.timeDifference}\nMethod: ${rateLimitObj.method}\nPath: ${rateLimitObj.path}\n` + - `Route: ${rateLimitObj.route}`); + Logger.logError(`Rate limit reached! Method: ${rateLimitObj.method}, Path: ${rateLimitObj.path}`); } }); @@ -118,7 +124,7 @@ function printUsage() { } function main() { - const args = process.argv.slice(2) + 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])) { diff --git a/objects/logger.js b/objects/logger.js index 2593476..9ff9e10 100644 --- a/objects/logger.js +++ b/objects/logger.js @@ -16,7 +16,7 @@ class Logger { static logError(error) { try { - if (output === 'all' || output === 'error') console.error(debug ? error.stack : error.message || error); + if (output === 'all' || output === 'error') console.error(debug ? error.stack || error : error.message || error); if (!fs.existsSync(logFolder)) fs.mkdirSync(logFolder); fs.appendFileSync(`${logFolder}/errors.log`, `[${getDateString()}] ${error.stack || error.message || error}\n`); } catch (err) { diff --git a/objects/versioncontrol.js b/objects/versioncontrol.js index 523532a..8edcd5f 100644 --- a/objects/versioncontrol.js +++ b/objects/versioncontrol.js @@ -1,14 +1,42 @@ const request = require('request'); class VersionControl { + /** + * Discord.js version check. + * @param {Object} Translator Translator object + */ + static checkLibraryVersion(Translator) { + return new Promise(async (resolve, reject) => { + let link = 'https://api.github.com/repos/hydrabolt/discord.js/commits/master'; + let result = await this.requestVersion(Translator, link).catch(err => reject(err)); + resolve(result); + }); + } + /** * Online version check. - * @param {Object} translator Translator object + * @param {Object} Translator Translator object */ static checkVersion(Translator) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { let link = 'http://guildcopy.jisagi.net/version'; - request(link, (err, res, body) => { + let result = await this.requestVersion(Translator, link).catch(err => reject(err)); + resolve(result); + }); + } + + /** + * Utility method + * @param {Object} Translator Translator object + * @param {string} link request link + */ + static requestVersion(Translator, link) { + return new Promise((resolve, reject) => { + let opt = { + url: link, + //headers: { 'User-Agent': 'request' }, + }; + request(opt, (err, res, body) => { if (err) return reject(err); if (res.statusCode !== 200) return reject(Translator.disp('errorVersionCheckStatuscode', [res.statusCode])); try { diff --git a/package.json b/package.json index 8465797..d839ef7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "guildcopy", - "version": "1.1.3", + "version": "1.1.4", "description": "A tool to copy a discord guild", "main": "copy.js", "scripts": { @@ -10,6 +10,6 @@ "license": "GPL-3.0", "dependencies": { "discord.js": "git://github.com/hydrabolt/discord.js.git#master", - "request": "^2.87.0" + "request": "^2.88.0" } } diff --git a/settings.json b/settings.json index ce9a5bb..fcd8d52 100644 --- a/settings.json +++ b/settings.json @@ -6,6 +6,7 @@ "copyBans": false, "language": "en", "output": "all", + "djsVersionCheck": true, "debug": false, "token": "" } diff --git a/translations/de.json b/translations/de.json index 3990693..10a963f 100644 --- a/translations/de.json +++ b/translations/de.json @@ -5,7 +5,8 @@ "text": { "messsageLogin": "Erfolgreich eingeloggt als [@@1@@]. Starte Skript", "messageLanguageAuthor": "Sprache [@@1@@] ausgewählt. Übersetzt von [@@2@@]", - "messageVersionCheckSuccess": "Aktuellste Sriptversion installiert", + "messageScriptVersionCheckSuccess": "Aktuellste Skriptversion installiert", + "messageDjsVersionCheckSuccess": "Aktuellste discord.js Version installiert", "messageSerialized": "[@@1@@]. Serialisierte Daten wurden gefunden und werden genutzt", "messageSerializerGeneralData": "[@@1@@]. Serialisiere allgemeine Daten", "messageSerializerRoleData": "[@@1@@]. Serialisiere Rollen", @@ -42,10 +43,10 @@ "messageCreatorBanData": "[@@1@@]. Banne Nutzer", "messageCreatorBanDataDebug": "[@@1@@].[@@2@@] Nutzer [@@3@@] gebannt", "messageGuildCopyFinished": "Servererstellung beendet!\nDie `[@@1@@]` Rolle kann nun gelöscht werden", - "errorOutputParameter": "Ausgabeparameter nicht valide", "errorUserToken": "Das genutzte Token ist kein Bot Token", - "errorNPM": "Discord.js wurde nicht mittels 'npm install' installiert. Genauere Information gibt es in der README.de", + "errorNPM1": "Bitte update deine Nodejs version auf mindestens Version 8.0.0", + "errorNPM2": "Du hast nicht die aktuellste Version von discord.js. Lösche den 'node_modules' Ordner und die 'package-lock.json' and installiere danach mittels 'npm install' alles neu", "errorUnspecified": "fehlgeschlagen", "errorVersionCheckStatuscode": "Versionsüberprüfung fehlgeschlagen mit Statuscode: [@@1@@]", "errorVersionCheckParsing": "Versionsüberprüfung fehlgeschlagen: Anfrage konnte nicht analysiert werden", diff --git a/translations/en.json b/translations/en.json index 7838364..6c58591 100644 --- a/translations/en.json +++ b/translations/en.json @@ -5,7 +5,8 @@ "text": { "messsageLogin": "Successfully logged in as [@@1@@]. Starting script", "messageLanguageAuthor": "Language [@@1@@] selected. Translated by [@@2@@]", - "messageVersionCheckSuccess": "Latest script version installed", + "messageScriptVersionCheckSuccess": "Latest script version installed", + "messageDjsVersionCheckSuccess": "Latest discord.js version installed", "messageSerialized": "[@@1@@]. Serialized data was found and will be used", "messageSerializerGeneralData": "[@@1@@]. Serializing general data", "messageSerializerRoleData": "[@@1@@]. Serializing role data", @@ -42,10 +43,10 @@ "messageCreatorBanData": "[@@1@@]. Banning user(s)", "messageCreatorBanDataDebug": "[@@1@@].[@@2@@] Banned user [@@3@@]", "messageGuildCopyFinished": "Guild copy finished!\nThe last thing to do is to delete the `[@@1@@]` role", - "errorOutputParameter": "Output parameter not valid", "errorUserToken": "Specified user token is not a bot user token", - "errorNPM": "Please don't install discord.js with 'npm install discord.js'! Installation instructions are in the README", + "errorNPM1": "Please update your Nodejs version to 8.0.0 or higher", + "errorNPM2": "You don't have the latest version of discord.js installed. Please delete the 'node_modules' folder and the 'package-lock.json' and run 'npm install' again", "errorUnspecified": "failed", "errorVersionCheckStatuscode": "Version check failed with statuscode: [@@1@@]", "errorVersionCheckParsing": "Version check failed: Failed to parse request",