hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/bot/app.js
blob: d25b3b67e6d47b0ce00d90068b33cba3a62c4468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { Client, Events, GatewayIntentBits } = require("discord.js")
const axios = require("axios")
const fs = require("fs-extra")
const path = require("path")
const config = require("../config/config.json")
const api = require("../config/api.json")

const { channel_id, minecraft_version, modrinth_mods } = config
const { token } = api

const cacheFile = path.join(__dirname, "../cache/mod_versions.json")
const client = new Client({ intents: [GatewayIntentBits.Guilds] })

const LoadCache = async () =>
{
  try
  {
    if (await fs.pathExists(cacheFile))
      return JSON.parse(await fs.readFile(cacheFile, "utf8"))
  } catch (err)
  {
    console.error("[Cache] Failed to read cache:", err)
  }
  return {}
}

const SaveCache = async (cache) =>
{
  try
  {
    await fs.ensureDir(path.dirname(cacheFile))
    await fs.writeFile(cacheFile, JSON.stringify(cache, null, 2))
  } catch (err)
  {
    console.error("[Cache] Failed to save cache:", err)
  }
}

const GetCompatibleModVersion = async (slug, targetMinecraftVersion) =>
{
  try
  {
    const res = await axios.get(`https://api.modrinth.com/v2/project/${slug}/version`)
    const fabricVersions = res.data.filter(v => v.loaders.includes("fabric"))
    if (fabricVersions.length === 0) 
      return null

    const compatible = fabricVersions.find(v => v.game_versions.includes(targetMinecraftVersion))
    const latest = fabricVersions.sort((a, b) =>
      new Date(b.date_published) - new Date(a.date_published)
    )[0]

    return { latest, compatible: compatible || null }
  } catch (err)
  {
    console.error(`[Modrinth] Error fetching ${slug}:`, err.message)
    return null
  }
}

const CheckModUpdates = async () =>
{
  console.log("[Check] Checking for mod compatibility...")
  const cache = await LoadCache()
  const newCache = { ...cache }
  const compatibleReleased = []
  const waitingUpdate = []
  const newlyCompatible = []

  for (const slug of modrinth_mods)
  {
    const modData = await GetCompatibleModVersion(slug, minecraft_version)
    if (!modData) continue

    const { latest, compatible } = modData
    const latestId = latest.id
    const cachedId = cache[slug]

    if (compatible)
    {
      compatibleReleased.push({ slug })
      newCache[slug] = latestId

      if (!cachedId || cachedId !== latestId)
      {
        newlyCompatible.push({ slug })
        console.log(`[Update] ${slug} is now compatible with Minecraft ${minecraft_version}`)
      }
    } else
    {
      waitingUpdate.push({ slug })
      console.log(`[Check] ${slug} is waiting for a compatible version`)
    }
  }

  await SaveCache(newCache)
  return { compatibleReleased, waitingUpdate, newlyCompatible }
}

const SendUpdateMessages = async (channel, compatibleReleased, waitingUpdate, newlyCompatible) =>
{
  if (!compatibleReleased.length && !waitingUpdate.length)
  {
    console.log("[Update]: No mods to report.")
    return
  }

  let message = `**Mod Compatibility Check — Minecraft ${minecraft_version}**\n\n`

  if (newlyCompatible.length > 0)
  {
    message += `**🎉 Mods Recently Updated:**\n`
    for (const { slug } of newlyCompatible)
      message += `✅ ${slug}\n`
    message += `\n`
  }

  const stillCompatible = compatibleReleased.filter(
    m => !newlyCompatible.find(n => n.slug === m.slug)
  )
  if (stillCompatible.length > 0)
  { 
    message += `**🟢 Mods Compatible:**\n`
    for (const { slug } of stillCompatible)
      message += `✅ ${slug}\n`
    message += `\n`
  }

  if (waitingUpdate.length > 0)
  {
    message += `**⏳ Mods Waiting on Update:**\n`
    for (const { slug } of waitingUpdate)
      message += `❌ ${slug}\n`
    message += `\n`
  }

  await channel.send({ content: message, allowedMentions: { parse: [] }, flags: 4 })
  console.log(`[Discord] Sent update summary (${newlyCompatible.length} updated, ${stillCompatible.length} compatible, ${waitingUpdate.length} waiting).`)
}

client.once(Events.ClientReady, async readyClient =>
{
  console.log(`[OK] Logged in as ${readyClient.user.tag}`)

  const channel = await client.channels.fetch(channel_id).catch(() => null)
  if (!channel) {
    console.error("❌ Could not find channel ID:", channel_id)
    return
  }

  const { compatibleReleased, waitingUpdate, newlyCompatible } = await CheckModUpdates()
  await SendUpdateMessages(channel, compatibleReleased, waitingUpdate, newlyCompatible)

  setInterval(async () =>
  {
    const { compatibleReleased, waitingUpdate, newlyCompatible } = await CheckModUpdates()
    await SendUpdateMessages(channel, compatibleReleased, waitingUpdate, newlyCompatible)
  }, 60 * 60 * 1000)
})

client.login(token)