From 34ca7f9065fdfdff190d56ef5809fec2e70f8963 Mon Sep 17 00:00:00 2001 From: jmhayes3 <22490346+jmhayes3@users.noreply.github.com> Date: Sun, 25 Aug 2024 11:57:37 -0500 Subject: [PATCH] fetch latest release --- commands/core/version.js | 26 +++++++++++++++++++++----- utils.js | 26 ++++++++++---------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/commands/core/version.js b/commands/core/version.js index d66c4fe..c9594a0 100644 --- a/commands/core/version.js +++ b/commands/core/version.js @@ -1,13 +1,29 @@ import { SlashCommandBuilder } from 'discord.js'; -import { checkVersion } from '../../utils.js'; +import { getLatestVersion } from '../../utils.js'; -export const data = new SlashCommandBuilder().setName('version').setDescription('Display version info'); +const VERSION = '0.0.1'; + +export const data = new SlashCommandBuilder().setName('version').setDescription('Display version info').addBooleanOption((option) => option.setName('check').setDescription('Check for update').setRequired(false)); export async function execute(interaction) { await interaction.deferReply({ ephemeral: true }); - const currentVersion = '0.1.0'; - const version = await checkVersion(currentVersion); + const current = VERSION; + + let reply = `You already have the latest version (${current}).`; + + const { options } = interaction; - await interaction.followUp({ content: version, ephemeral: true }); + const check = options.getBoolean('check'); + if (check) { + console.log('Checking for updates...') + const latest = await getLatestVersion(); + if (current < latest) { + reply = `The latest version is ${latest}. You are using version ${current}.`; + } + } + else { + console.log('Not checking for updates') + } + await interaction.followUp({ content: reply, ephemeral: true }); } diff --git a/utils.js b/utils.js index 96834f0..e394c9a 100644 --- a/utils.js +++ b/utils.js @@ -18,26 +18,20 @@ export const sleep = (ms) => { export async function getLatestVersion() { try { - const latestVersion = '1.0.0'; - console.log(latestVersion); - const response = await fetch('https://github.com/jmhayes3/binksjs/releases/latest'); console.log(response); - console.log(response.data.tag_name); - return latestVersion; - } catch (error) { - console.error(`Error while retrieving the latest version. No release found.\n ${error}`); - } -} + const url = response.url; + console.log(url); -export async function checkVersion(currentVersion) { - let reply = `You already have the latest version.`; + const tag = response.url.split('/').at(-1); + console.log(tag); - const latestVersion = await getLatestVersion(); - if (currentVersion < latestVersion) { - reply = `The latest version is ${latestVersion}. You are currently using version ${currentVersion}.`; - } + const cleaned = tag.split('v').at(-1); + console.log(cleaned); - return reply; + return cleaned; + } catch (error) { + console.error(`Error while retrieving the latest version. No release found.\n ${error}`); + } }