From dc0ac88fa794ffc738235cf3bb50fc5f60aa131e Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 24 Sep 2021 19:39:58 +0100 Subject: [PATCH] feat: upgrade script --- .gitignore | 2 +- src/index.js | 3 ++- src/{updater.js => update/notifier.js} | 2 +- src/update/upgrade.js | 29 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) rename src/{updater.js => update/notifier.js} (94%) create mode 100644 src/update/upgrade.js diff --git a/.gitignore b/.gitignore index 5f5aa88..dcc5186 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ site/ # files .env -sskey.env +version user/config.js user/database.sqlite user/plugins/*/ \ No newline at end of file diff --git a/src/index.js b/src/index.js index db34d3c..ef3276b 100644 --- a/src/index.js +++ b/src/index.js @@ -160,7 +160,8 @@ class Bot extends Client { this.setMaxListeners(this.config.max_listeners); // set the max listeners for each event - require('./updater')(this); // check for updates + require('./update/notifier')(this); // check for updates + require('./update/upgrade')(this); // upgrade const listeners = new ListenerLoader(this); listeners.load(); // load listeners diff --git a/src/updater.js b/src/update/notifier.js similarity index 94% rename from src/updater.js rename to src/update/notifier.js index 09ffede..00e215f 100644 --- a/src/updater.js +++ b/src/update/notifier.js @@ -5,7 +5,7 @@ const link = require('terminal-link'); const semver = require('semver'); const { format } = require('leekslazylogger'); -const { version: current } = require('../package.json'); +const { version: current } = require('../../package.json'); module.exports = async client => { if (!client.config.update_notice) return; diff --git a/src/update/upgrade.js b/src/update/upgrade.js new file mode 100644 index 0000000..b821787 --- /dev/null +++ b/src/update/upgrade.js @@ -0,0 +1,29 @@ +const fs = require('fs'); +const { path } = require('../utils/fs'); +const FILE = path('./version'); +const { version: current } = require('../../package.json'); + +/** + * @param {import('../..').Bot} client + */ +module.exports = async client => { + if (!fs.existsSync(FILE)) { + return fs.writeFileSync(FILE, current); + } else { + const previous = fs.readFileSync(FILE, { encoding: 'utf8' }); + if (previous !== current) { + client.log.info('Running upgrade task'); + client.log.info('Writing version'); + fs.writeFileSync(FILE, current); + + const guilds = await client.db.models.Guild.findAll(); + guilds.forEach(async guild => { + guild.set('blacklist', { + members: [], + roles: [] + }); + await guild.save(); + }); + } + } +}; \ No newline at end of file