From 6fd1101f1af48636c2bb276bc77e567f92653b8f Mon Sep 17 00:00:00 2001 From: jmhayes3 <22490346+jmhayes3@users.noreply.github.com> Date: Sun, 25 Aug 2024 10:25:26 -0500 Subject: [PATCH] fetch latest version from github repo --- commands/core/version.js | 28 +++++++++++----------------- utils.js | 28 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/commands/core/version.js b/commands/core/version.js index 53b448c..d66c4fe 100644 --- a/commands/core/version.js +++ b/commands/core/version.js @@ -1,19 +1,13 @@ -export async function getLatestVersion() { - try { - const latestVersion = "0.1.0"; - console.log(latestVersion); - return latestVersion; - } catch (error) { - console.error(`Error while retrieving the latest version. No release found.\n ${error}`); - } -} +import { SlashCommandBuilder } from 'discord.js'; +import { checkVersion } from '../../utils.js'; + +export const data = new SlashCommandBuilder().setName('version').setDescription('Display version info'); + +export async function execute(interaction) { + await interaction.deferReply({ ephemeral: true }); + + const currentVersion = '0.1.0'; + const version = await checkVersion(currentVersion); -export function checkVersion(currentVersion) { - getLatestVersion().then((latestVersion) => { - if (currentVersion < latestVersion) { - console.log(`A new update is available: ${latestVersion}`); - } else { - console.log(`You have the latest version of the code.`); - } - }); + await interaction.followUp({ content: version, ephemeral: true }); } diff --git a/utils.js b/utils.js index 8b1eecd..96834f0 100644 --- a/utils.js +++ b/utils.js @@ -12,6 +12,32 @@ export function splitString(str, length) { } export const sleep = (ms) => { - console.log("Sleeping " + ms + "ms"); + console.log('Sleeping ' + ms + 'ms'); return new Promise(resolve => setTimeout(resolve, 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}`); + } +} + +export async function checkVersion(currentVersion) { + let reply = `You already have the latest version.`; + + const latestVersion = await getLatestVersion(); + if (currentVersion < latestVersion) { + reply = `The latest version is ${latestVersion}. You are currently using version ${currentVersion}.`; + } + + return reply; +}