-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathminecraft-version.js
243 lines (229 loc) · 8.15 KB
/
minecraft-version.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import request from 'request-promise-native'
import fetch from 'node-fetch'
import html from 'html-entities'
import {SlashCommandBuilder} from '@discordjs/builders'
import {replyNoMention} from './utils.js'
let client, config
export default async (_client, _config) => {
client = _client
config = _config['minecraft-version']
if (!config) return
const state = {}
await poll.call(state)
if (config.webhook || config.channels) {
setInterval(poll.bind(state), (config.interval || 10) * 1000)
}
async function handleVersionCommand(interaction, versionArg) {
let type = 'snapshot'
let id
if (versionArg) {
if (Object.keys(state.data.latest).includes(versionArg)) {
type = versionArg
} else {
id = versionArg
}
}
if (!id) id = state.data.latest[type]
const version = state.data.versions.find(v => v.id === id)
if (version) {
const embed = await getUpdateEmbed(version)
await replyNoMention(interaction, {embeds: [embed]})
} else {
await replyNoMention(interaction, `Unknown version '${id || type}'`)
}
}
client.on('messageCreate', async message => {
if (message.author.bot) return
try {
if (message.content.startsWith('!mcversion')) {
await handleVersionCommand(message, message.content.substr(10).trim())
}
} catch (e) {
console.error(e)
}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand() || interaction.commandName !== 'mcversion') return
await interaction.deferReply()
await handleVersionCommand(interaction, interaction.options.getString('version'))
})
return [
new SlashCommandBuilder()
.setName('mcversion')
.setDescription('Shows information about Minecraft versions')
.addStringOption(option => option.setName('version').setDescription('A specific version'))
]
}
async function poll () {
try {
const res = await fetch('https://launchermeta.mojang.com/mc/game/version_manifest.json')
const data = await res.json()
this.data = data
const latestDate = data.versions.map(v => Date.parse(v.time)).reduce((a, b) => a > b ? a : b)
if (this.latestDate === undefined) {
this.latestRelease = data.latest.release
this.latestSnapshot = data.latest.snapshot
this.latestDate = latestDate
return
}
if (latestDate < this.latestDate) return
this.latestDate = latestDate
if (this.latestRelease !== data.latest.release) {
this.latestRelease = data.latest.release
this.latestSnapshot = data.latest.snapshot
await update(data.versions.find(v => v.id === data.latest.release))
} else if (this.latestSnapshot !== data.latest.snapshot) {
this.latestSnapshot = data.latest.snapshot
await update(data.versions.find(v => v.id === data.latest.snapshot))
}
} catch (e) {
console.error(e)
}
}
const fancySize = size => {
const mbs = size / (1024 * 1204)
return mbs.toFixed(1) + 'MB'
}
async function update (version) {
const embed = await getUpdateEmbed(version)
if (config.webhook) await request.post(config.webhook, {json: {embeds: [embed]}})
if (config.channels) {
for (const id of config.channels) {
const channel = await client.channels.fetch(id)
await channel.send({embed})
}
}
}
async function getUpdateEmbed (version) {
const details = await (await fetch(version.url)).json()
const fields = {
Type: version.type.includes('_') ? version.type.replace(/_/g, '-') : version.type[0].toUpperCase() + version.type.slice(1),
Id: version.id,
'Version JSON': `[${version.id}.json](${version.url})`,
Assets: `[${details.assetIndex.id}](${details.assetIndex.url})`
}
let embedImage, embedThumbnail
let extraDescription
try {
const {url, image, subtitle, description} = await getArticle(version)
extraDescription = description
if (url) {
fields.Changelog = `[${subtitle || 'minecraft.net'}](${url})`
} else {
fields.Changelog = `[quiltmc.org](https://quiltmc.org/mc-patchnotes/#${version.id})`
}
if (image) {
if (image.endsWith('-header.jpg')) {
embedImage = {url: image}
} else {
embedThumbnail = {url: image}
}
}
} catch (e) {
console.error(e)
}
const jars = [
details.downloads.server && `[Server JAR](${details.downloads.server.url}) (${fancySize(details.downloads.server.size)})`,
details.downloads.client && `[Client JAR](${details.downloads.client.url}) (${fancySize(details.downloads.client.size)})`
].filter(Boolean).join(' - ')
const description = [
Object.keys(fields).map(k => `**${k}**: ${fields[k]}`).join('\n'),
extraDescription,
jars
].filter(Boolean).join('\n\n')
const embed = {
title: `Minecraft ${getFullVersionName(version)}`,
url: version.url,
description,
timestamp: version.releaseTime,
image: embedImage,
thumbnail: embedThumbnail
}
return embed
}
function getFullVersionName(version) {
const match = version.id.match(/^(\d+\.\d+(?:\.\d+)?)(-(rc|pre)(\d+)$)?/)
if (match) {
switch (match[3]) {
case 'rc': return match[1] + ' Release Candidate ' + match[4]
case 'pre': return match[1] + ' Pre-Release ' + match[4]
}
}
return version.type.split('_').map(w => w[0].toUpperCase() + w.slice(1)).join(' ') + ' ' + version.id
}
const USER_AGENT = 'Mozilla/5.0 (Linux) Gecko'
async function getPatchNotes() {
try {
return await (await fetch('https://launchercontent.mojang.com/javaPatchNotes.json', {timeout: 2000})).json()
} catch (e) {
console.error(e)
return {version: 1, entries: []}
}
}
async function getPatchNotesInfo(version) {
const allPatchNotes = await getPatchNotes()
const patchNotes = allPatchNotes.entries.find(e => e.version === version.id)
if (!patchNotes) return {}
const info = {}
const image = 'https://launchercontent.mojang.com' + patchNotes.image.url
if (await checkImage(image)) {
info.image = image
}
const match = patchNotes.body.match(/^<p>(.*?)<\/p>/)
if (match) {
info.description = html.decode(match[1])
}
return info
}
async function getArticleGrid() {
try {
return await (await fetch('https://www.minecraft.net/content/minecraft-net/_jcr_content.articles.grid', {timeout: 2000, headers: {'User-Agent': USER_AGENT}})).json()
} catch (e) {
console.error(e)
return {article_grid: [], article_count: 0}
}
}
async function getArticleInfo(version) {
const articles = await getArticleGrid()
const candidates = articles.article_grid.filter(article => {
const title = article.default_tile.title
if (!title.startsWith('Minecraft ') || title.startsWith('Minecraft Dungeons') || article.default_tile.sub_header.includes('Bedrock Beta')) return false
if (title.includes(version.id)) return true
if (version.type !== 'snapshot') return false
const snapshot = version.id.match(/^(\d{2}w\d{2})([a-z])$/)
if (snapshot) return title.includes(snapshot[1])
const match = version.id.match(/^(\d+\.\d+(?:\.\d+)?)(-(rc|pre)(\d+)$)?/)
if (!match) return false
switch (match[3]) {
case 'rc': return title.includes(match[1] + ' Release Candidate')
case 'pre': return title.includes(match[1] + ' Pre-Release')
default: return title.includes(version.id)
}
})
const article = candidates[0]
if (!article) return {}
const tile = article.default_tile
let imageURL = 'https://minecraft.net' + tile.image.imageURL
const headerImageURL = imageURL.replace('1x1', 'header')
if (headerImageURL !== imageURL && await checkImage(headerImageURL)) {
imageURL = headerImageURL
}
return {url: 'https://minecraft.net' + article.article_url, title: tile.title, subtitle: tile.sub_header, image: imageURL}
}
async function getArticle (version) {
const infos = (await Promise.allSettled([
getArticleInfo(version),
getPatchNotesInfo(version)
])).map(r => r.value || {})
console.log(infos)
return {...infos[0], ...infos[1]}
}
async function checkImage(url) {
try {
// See if this would work as an embed
await (await fetch(url, {timeout: 1000, method: 'HEAD', headers: {'User-Agent': 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)'}})).buffer()
return true
} catch (e) {
return false
}
}