Skip to content

Commit

Permalink
Add download link to outdated message
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jul 24, 2024
1 parent 777be8e commit 36fdf55
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
34 changes: 23 additions & 11 deletions modules/checks/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Client, ColorResolvable, EmbedBuilder, Message} from 'discord.js'
import {Client, ColorResolvable, Colors, EmbedBuilder, Message} from 'discord.js'

import config, {Checks, MessagePredicate} from './checks.config'
import data from 'data.json'
import tesseract from 'tesseract.js'
import {getMetadata} from "../commands/metadata";
import semver from "semver/preload";

const imageTypes = ['image/png', 'image/jpeg', 'image/webp'];

Expand Down Expand Up @@ -139,17 +140,28 @@ async function respondToText(message: Message, text: string, footer: string) {
try {
const rawDump = JSON.parse(text)
const buildInfo = rawDump.buildInfo
const version = buildInfo.version
const latestVersion = getMetadata().name
const version = semver.coerce(buildInfo.version)
const metadata = getMetadata()
const latestVersion = semver.coerce(metadata.tag_name)

const messageEmbeds: EmbedBuilder[] = []
if (version !== null && latestVersion !== null && semver.lt(version, latestVersion)) {
messageEmbeds.push(new EmbedBuilder()
.setTitle('Outdated SkinsRestorer Version!')
.setColor(Colors.Red)
.addFields(
{
name: ' ',
value: `[Download ${latestVersion}](${metadata.assets.find(a => a.name === "SkinsRestorer.jar")?.browser_download_url})`
}
)
.setDescription(`The SkinsRestorer version you're using (\`${version}\`) is outdated! Please update to the latest version: \`${latestVersion}\``)

.setFooter({text: footer}))
}

if (version !== latestVersion) {
await message.reply({
embeds: [new EmbedBuilder()
.setTitle('Outdated SkinsRestorer Version!')
.setColor('#FF0000')
.setDescription(`The SkinsRestorer version you're using (\`${version}\`) is outdated! Please update to the latest version: \`${latestVersion}\``)
.setFooter({text: footer})]
})
if (messageEmbeds.length > 0) {
await message.reply({embeds: messageEmbeds})
}
} catch (e) {
// Can be ignored, as it's not a SkinsRestorer dump
Expand Down
25 changes: 14 additions & 11 deletions modules/commands/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
let metadata: { name?: string } = {}

const fetchData = async (): Promise<void> => {
try {
metadata = { ...await (await fetch('https://api.spiget.org/v2/resources/2124/versions/latest')).json() }
} catch (e) {
console.error(e)
}
type Metadata = {
tag_name: string
assets: {
name: string
browser_download_url: string
}[]
}

await fetchData()
setInterval(fetchData, 1_000 * 60) // 60 requests per hour
const fetchData = async (): Promise<Metadata> => {
return await (await fetch('https://api.github.com/repos/SkinsRestorer/SkinsRestorer/releases/latest')).json()
}
let metadata = await fetchData()
setInterval(() => {
void fetchData().catch(console.error)
}, 1_000 * 60) // 60 requests per hour

export function getMetadata () {
export function getMetadata() {
return metadata
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"js-yaml": "^4.1.0",
"mime-types": "^2.1.35",
"modelfusion": "^0.137.0",
"semver": "^7.6.3",
"tesseract.js": "^5.0.0"
},
"devDependencies": {
"@types/dateformat": "5.0.2",
"@types/js-yaml": "^4.0.5",
"@types/mime-types": "2.1.4",
"@types/semver": "^7.5.8",
"eslint": "8.57.0",
"ts-standard": "12.0.2",
"tsx": "^4.0.0",
Expand Down
45 changes: 18 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 36fdf55

Please sign in to comment.