-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathShowHiddenChannels.plugin.js
130 lines (108 loc) · 2.83 KB
/
ShowHiddenChannels.plugin.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
/**
* @name ShowHiddenChannels
* @displayName ShowHiddenChannels - Updater
* @version 1.0.0
* @author JustOptimize (Oggetto)
* @authorId 619203349954166804
* @source https://github.com/JustOptimize/ShowHiddenChannels
* @description An updater for ShowHiddenChannels to let older versions update to the latest version.
*/
const config = {
info: {
name: "ShowHiddenChannels",
},
main: "ShowHiddenChannels.plugin.js",
github_short: "JustOptimize/ShowHiddenChannels",
};
module.exports = class ShowHiddenChannels {
constructor(meta) {
this.api = new BdApi(meta.name);
}
start() {
this.checkForUpdates();
}
stop() {}
async checkForUpdates() {
console.log(
`Checking for updates`,
);
const releases_raw = await fetch(
`https://api.github.com/repos/${config.github_short}/releases`,
);
if (!releases_raw || !releases_raw.ok) {
return this.api.UI.showToast(
"(ShowHiddenChannels) Failed to check for updates.",
{
type: "error",
},
);
}
let releases = await releases_raw.json();
if (!releases || !releases.length) {
return this.api.UI.showToast(
"(ShowHiddenChannels) Failed to check for updates.",
{
type: "error",
},
);
}
// Remove releases that do not have in the assets a file named ShowHiddenChannels.plugin.js
releases = releases.filter((m) =>
m.assets.some((n) => n.name === config.main),
);
const latestRelease = releases.find((m) => !m.prerelease)?.tag_name?.replace("v", "");
console.log(
`Latest version: ${latestRelease}`,
);
if (!latestRelease) {
this.api.UI.alert(
config.info.name,
"Failed to check for updates, version not found.",
);
return console.err("Failed to check for updates, version not found.");
}
const SHCContent = await this.api.Net.fetch(
`https://github.com/JustOptimize/ShowHiddenChannels/releases/download/v${latestRelease}/${config.main}`,
)
.then((res) => res.text())
.catch(() => {
this.api.UI.showToast("Failed to fetch the latest version.", {
type: "error",
});
});
this.proceedWithUpdate(SHCContent, latestRelease);
}
async proceedWithUpdate(SHCContent, version) {
console.log(
`Update confirmed by the user, updating to version ${version}`,
);
function failed() {
this.api.UI.showToast("(ShowHiddenChannels) Failed to update.", {
type: "error",
});
}
if (!SHCContent) return failed();
if (!SHCContent.match(/(?<=version: ").*(?=")/)) {
return failed();
}
try {
const fs = require("fs");
const path = require("path");
await fs.writeFile(
path.join(this.api.Plugins.folder, config.main),
SHCContent,
(err) => {
if (err) return failed();
},
);
this.api.UI.showToast(
`ShowHiddenChannels updated to version ${version}`,
{
type: "success",
},
);
} catch (err) {
return failed();
}
}
}