feat: upgrade script

This commit is contained in:
Isaac 2021-09-24 19:39:58 +01:00
parent 95f76716ed
commit dc0ac88fa7
No known key found for this signature in database
GPG Key ID: 87B9B6BD04EB7534
4 changed files with 33 additions and 3 deletions

2
.gitignore vendored
View File

@ -6,7 +6,7 @@ site/
# files
.env
sskey.env
version
user/config.js
user/database.sqlite
user/plugins/*/

View File

@ -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

View File

@ -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;

29
src/update/upgrade.js Normal file
View File

@ -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();
});
}
}
};