This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
/
BDGitHubDownloader.plugin.js
418 lines (416 loc) · 14.8 KB
/
BDGitHubDownloader.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* @name BDGitHubDownloader
* @author Ahlawat
* @authorId 1025214794766221384
* @version 2.3.0
* @invite SgKSKyh9gY
* @description Download BetterDiscord plugins and themes by right clicking on a message containing a GitHub link.
* @website https://tharki-god.github.io/
* @source https://github.com/Tharki-God/BetterDiscordPlugins
* @updateUrl https://tharki-god.github.io/BetterDiscordPlugins/BDGitHubDownloader.plugin.js
*/
/*@cc_on
@if (@_jscript)
var shell = WScript.CreateObject("WScript.Shell");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var pathPlugins = shell.ExpandEnvironmentStrings("%APPDATA%\\BetterDiscord\\plugins");
var pathSelf = WScript.ScriptFullName;
shell.Popup("It looks like you've mistakenly tried to run me directly. \n(Don't do that!)", 0, "I'm a plugin for BetterDiscord", 0x30);
if (fs.GetParentFolderName(pathSelf) === fs.GetAbsolutePathName(pathPlugins)) {
shell.Popup("I'm in the correct folder already.", 0, "I'm already installed", 0x40);
} else if (!fs.FolderExists(pathPlugins)) {
shell.Popup("I can't find the BetterDiscord plugins folder.\nAre you sure it's even installed?", 0, "Can't install myself", 0x10);
} else if (shell.Popup("Should I move myself to BetterDiscord's plugins folder for you?", 0, "Do you need some help?", 0x34) === 6) {
fs.MoveFile(pathSelf, fs.BuildPath(pathPlugins, fs.GetFileName(pathSelf)));
shell.Exec("explorer " + pathPlugins);
shell.Popup("I'm installed!", 0, "Successfully installed", 0x40);
}
WScript.Quit();
@else@*/
module.exports = ((_) => {
const config = {
info: {
name: "BDGitHubDownloader",
authors: [
{
name: "Ahlawat",
discord_id: "1025214794766221384",
github_username: "Tharki-God",
}
],
version: "2.3.0",
description:
"Download BetterDiscord plugins and themes by right clicking on a message containing a GitHub link.",
github: "https://github.com/Tharki-God/BetterDiscordPlugins",
github_raw:
"https://tharki-god.github.io/BetterDiscordPlugins/BDGitHubDownloader.plugin.js",
},
changelog: [
{
title: "v0.0.1",
items: ["Idea in mind"],
},
{
title: "v0.0.5",
items: ["Base Model"],
},
{
title: "Initial Release v1.0.0",
items: [
"This is the initial release of the plugin :)",
"Couldn't Have been possible without my sis, Thank you Kirai",
"No Incest but you are great",
"btw guys get those illegal plugins easily (>'-'<)",
],
},
{
title: "v2.0.0",
items: ["Better Code", "Theme Support"],
},
{
title: "v2.2.2",
items: ["Corrected text."],
},
],
main: "BDGitHubDownloader.plugin.js",
};
const RequiredLibs = [{
window: "ZeresPluginLibrary",
filename: "0PluginLibrary.plugin.js",
external: "https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js",
downloadUrl: "https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js"
},
{
window: "BunnyLib",
filename: "1BunnyLib.plugin.js",
external: "https://github.com/Tharki-God/BetterDiscordPlugins",
downloadUrl: "https://tharki-god.github.io/BetterDiscordPlugins/1BunnyLib.plugin.js"
},
];
class handleMissingLibrarys {
load() {
for (const Lib of RequiredLibs.filter(lib => !window.hasOwnProperty(lib.window)))
BdApi.showConfirmationModal(
"Library Missing",
`The library plugin (${Lib.window}) needed for ${config.info.name} is missing. Please click Download Now to install it.`,
{
confirmText: "Download Now",
cancelText: "Cancel",
onConfirm: () => this.downloadLib(Lib),
}
);
}
async downloadLib(Lib) {
const fs = require("fs");
const path = require("path");
const { Plugins } = BdApi;
const LibFetch = await fetch(
Lib.downloadUrl
);
if (!LibFetch.ok) return this.errorDownloadLib(Lib);
const LibContent = await LibFetch.text();
try {
await fs.writeFile(
path.join(Plugins.folder, Lib.filename),
LibContent,
(err) => {
if (err) return this.errorDownloadLib(Lib);
}
);
} catch (err) {
return this.errorDownloadLib(Lib);
}
}
errorDownloadZLib(Lib) {
const { shell } = require("electron");
BdApi.showConfirmationModal(
"Error Downloading",
[
`${Lib.window} download failed. Manually install plugin library from the link below.`,
],
{
confirmText: "Download",
cancelText: "Cancel",
onConfirm: () => {
shell.openExternal(
Lib.external
);
},
}
);
}
start() { }
stop() { }
}
return RequiredLibs.some(m => !window.hasOwnProperty(m.window))
? handleMissingLibrarys
: (([Plugin, ZLibrary]) => {
const {
Toasts,
Utilities,
Logger,
PluginUpdater,
Settings: { SettingPanel, SettingGroup, Switch },
} = ZLibrary;
const { ContextMenu } = BdApi;
const {
LibraryUtils,
LibraryIcons,
LibraryRequires: { fs }
} = BunnyLib.build(config)
const isGithubUrl = new RegExp(
"(?:git|https?|git@)(?:\\:\\/\\/)?github.com[/|:][A-Za-z0-9-]+?"
);
const isGithubRawUrl = new RegExp(
"(?:git|https?|git@)(?:\\:\\/\\/)?raw.githubusercontent.com[/|:][A-Za-z0-9-]+?"
);
const nameRegex =
/@name\s+([^\t^\r^\n]+)|\/\/\**META.*["']name["']\s*:\s*["'](.+?)["']/i;
const fileNameRegex = /([^\\\/]+)$/i;
const defaultSettings = {
showToast: true,
autoEnablePlugin: true,
showPluginDownload: true,
autoEnableTheme: true,
showThemeDownload: true,
};
return class BDGitHubDownloader extends Plugin {
constructor() {
super();
this.settings = Utilities.loadData(
config.info.name,
"settings",
defaultSettings
);
this.pluginDownloadPatch = this.pluginDownloadPatch.bind(this);
this.themeDownloadPatch = this.themeDownloadPatch.bind(this);
}
checkForUpdates() {
try {
PluginUpdater.checkForUpdate(
config.info.name,
config.info.version,
config.info.github_raw
);
} catch (err) {
Logger.err("Plugin Updater could not be reached.", err);
}
}
onStart() {
this.checkForUpdates();
this.addMenu();
}
addMenu() {
if (this.settings["showPluginDownload"])
ContextMenu.patch("message", this.pluginDownloadPatch);
if (this.settings["showThemeDownload"])
ContextMenu.patch("message", this.themeDownloadPatch);
}
pluginDownloadPatch(menu, { message }) {
let links = LibraryUtils.getLinks(message.content);
links = links?.filter((link) => link.endsWith(".plugin.js"));
if (links?.length)
for (var link of links) {
if (isGithubUrl.test(link))
link = `https://raw.githubusercontent.com/${link.split("github.com/")[1]
}`.replace("/blob/", "/");
const [fileName] = fileNameRegex.exec(link)[0].split(".plugin.js");
menu.props.children.splice(
3,
0,
ContextMenu.buildItem(
{
name: `Download ${fileName}`,
separate: true,
id: `download-${fileName
.toLowerCase()
.replaceAll(" ", "-")}`,
label: `Download ${fileName}`,
icon: () => LibraryIcons.Download("20", "20"),
action: async () => {
if (isGithubRawUrl.test(link))
return this.download(
link,
`${fileName}.plugin.js`,
"Plugin"
);
else if (this.settings["showToast"])
Toasts.show(`That link type is not supported`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_error_circle_24_regular.png",
timeout: 5000,
type: "error",
});
},
},
true
)
);
}
}
themeDownloadPatch(menu, { message }) {
let links = LibraryUtils.getLinks(message.content);
links = links?.filter((link, index) => link.endsWith(".theme.css"));
if (links?.length)
for (var link of links) {
if (isGithubUrl.test(link))
link = `https://raw.githubusercontent.com/${link.split("github.com/")[1]
}`.replace("/blob/", "/");
const [fileName] = fileNameRegex.exec(link)[0].split(".theme.css");
menu.props.children.splice(
3,
0,
ContextMenu.buildItem(
{
name: `Download ${fileName}`,
separate: true,
id: `download-${fileName
.toLowerCase()
.replaceAll(" ", "-")}`,
label: `Download ${fileName}`,
icon: () => LibraryIcons.Download("20", "20"),
action: async () => {
if (isGithubRawUrl.test(link))
return this.download(
link,
`${fileName}.theme.css`,
"Theme"
);
else if (this.settings["showToast"])
Toasts.show(`That link type is not supported`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_error_circle_24_regular.png",
timeout: 5000,
type: "error",
});
},
},
true
)
);
}
}
async download(url, fileName, type) {
const response = await fetch(url);
if (!response.ok && this.settings["showToast"])
Toasts.show(`Downloading Error!`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_error_circle_24_regular.png",
timeout: 5000,
type: "error",
});
const data = await response.text();
let name = (nameRegex.exec(data) || []).filter((n) => n)[1];
if (this.settings["showToast"])
Toasts.show(`Downloading ${type}: ${name}`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_arrow_download_24_filled.png",
timeout: 5000,
type: "error",
});
try {
await fs
.writeFileSync(
require("path").join(
type === "Plugin"
? BdApi.Plugins.folder
: BdApi.Themes.folder,
fileName
),
data,
(err) => {
if (err) {
if (this.settings["showToast"])
Toasts.show(` Error: ${err}.`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_error_circle_24_regular.png",
timeout: 5000,
type: "error",
});
Logger.err(err);
}
}
)
if (this.settings["autoEnableTheme"] && type === "Theme") {
setTimeout(() => {
BdApi.Themes.enable(name);
}, 2000);
} else if (
this.settings["autoEnablePlugin"] &&
type === "Plugin"
) {
setTimeout(() => {
BdApi.Plugins.enable(name);
}, 2000);
}
} catch (err) {
if (this.settings["showToast"])
Toasts.show(` Error: ${err}.`, {
icon: "https://tharki-god.github.io/files-random-host/ic_fluent_error_circle_24_regular.png",
timeout: 5000,
type: "error",
});
Logger.warn("Something went wrong.", err);
}
}
onStop() {
ContextMenu.unpatch("message", this.pluginDownloadPatch);
ContextMenu.unpatch("message", this.themeDownloadPatch);
}
getSettingsPanel() {
return SettingPanel.build(
this.saveSettings.bind(this),
new Switch(
"Pop-up/Toast",
"Display error/success toast.",
this.settings["showToast"],
(e) => {
this.settings["showToast"] = e;
}
),
new SettingGroup("Plugins", {
collapsible: true,
shown: false,
}).append(
new Switch(
"Show option",
"Provide the option to download a plugin from a link.",
this.settings["showPluginDownload"],
(e) => {
this.settings["showPluginDownload"] = e;
}
),
new Switch(
"Auto enable",
"Automatically enable each plugin after downloading.",
this.settings["autoEnablePlugin"],
(e) => {
this.settings["autoEnablePlugin"] = e;
}
)
),
new SettingGroup("Themes", {
collapsible: true,
shown: false,
}).append(
new Switch(
"Show option",
"Provide the option to download a theme from a link.",
this.settings["showThemeDownload"],
(e) => {
this.settings["showThemeDownload"] = e;
}
),
new Switch(
"Auto enable",
"Automatically enable each theme after downloading.",
this.settings["autoEnableTheme"],
(e) => {
this.settings["autoEnableTheme"] = e;
}
)
)
);
}
saveSettings() {
Utilities.saveData(config.info.name, "settings", this.settings);
}
};
})(ZLibrary.buildPlugin(config));
})();
/*@end@*/