-
Notifications
You must be signed in to change notification settings - Fork 6
/
update.js
65 lines (55 loc) · 1.95 KB
/
update.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
#!/usr/bin/env node
const isProd = true // This should be set to true when not a local development version.
const core = require('@actions/core');
const axios = require('axios');
const { readFileSync, writeFileSync } = require('fs');
const id = isProd ? core.getInput("id") : "893762371770802227"
const file = isProd ? core.getInput("file") : "README.md"
const deriveReplacement = (status) => {
const symbols = {
offline: "⚪",
online: "🟢",
dnd: "🔴",
idle: "🟡",
streaming: "🟣"
}
const statusSymbol = symbols[status]
const matcher = /🔴|🟣|🟡|⚪|🟢/g
try {
var fileBuf = readFileSync(file)
console.log("[deriveReplacement] :: Successfully read target file.")
} catch (err) {
if (core.isDebug()) {
console.error("[deriveReplacement] :: Failed to read target file.\nCaptured Trace:\n", err)
} else {
console.error("[deriveReplacement] :: Failed to read target file.")
}
}
const replacedContents = fileBuf.toString().replace(matcher, statusSymbol)
try {
writeFileSync(file, replacedContents)
console.log("[deriveReplacement] :: Successfully wrote updated contents to target file.")
} catch (err) {
if (core.isDebug()) {
console.error("[deriveReplacement] :: Failed to write to target file.\nCaptured Trace:\n", err)
} else {
console.error("[deriveReplacement] :: Failed to write to target file.")
}
}
}
axios.get(`https://api.lanyard.rest/v1/users/${id}`)
.then((response) => {
const fetchedRaw = response.data.data
const status = fetchedRaw.discord_status
console.log("[self] :: Successfully fetched user status -> ", status)
if (!status) throw new Error("[self] :: Invalid status type. Refusing to continue.")
try { deriveReplacement(status) } catch (err) {
core.setFailed(err.message)
}
})
.catch((err) => {
isProd ? core.setFailed(err.message) : (() => {
console.error(err.message)
process.exit(1)
})()
})