DiscordTickets/src/updater.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-02-17 15:24:33 +02:00
const fetch = require('node-fetch');
const boxen = require('boxen');
const link = require('terminal-link');
const semver = require('semver');
2021-04-22 00:02:21 +03:00
const { format } = require('leekslazylogger-fastify');
2021-02-17 15:24:33 +02:00
2021-05-18 20:12:07 +03:00
const { version: current } = require('../package.json');
2021-02-17 15:24:33 +02:00
module.exports = async client => {
if (!client.config.update_notice) return;
2021-03-03 12:57:28 +02:00
const json = await (await fetch('https://api.github.com/repos/discord-tickets/bot/releases')).json();
2021-02-17 15:24:33 +02:00
const update = json[0];
2021-05-18 20:12:07 +03:00
const latest = semver.coerce(update.tag_name);
2021-02-17 15:24:33 +02:00
if (!semver.valid(latest)) return;
if (semver.lt(current, latest)) {
2021-05-12 23:14:02 +03:00
client.log.notice(`There is an update available for Discord Tickets (${current} -> ${update.tag_name})`);
2021-02-17 15:24:33 +02:00
2021-05-18 20:12:07 +03:00
const lines = [
2021-05-12 23:14:02 +03:00
`&k&6You are currently using &c${current}&6, the latest is &a${update.tag_name}&6.&r`,
`&k&6Download "&f${update.name}&6" from&r`,
link('&k&6the GitHub releases page.&r&6', 'https://github.com/discord-tickets/bot/releases/')
2021-04-22 00:02:21 +03:00
];
2021-02-17 15:24:33 +02:00
console.log(
2021-04-22 00:02:21 +03:00
boxen(format(lines.join('\n')), {
2021-02-17 15:24:33 +02:00
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round'
})
);
}
};