forked from yofukashino/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBDGithubDownloader.plugin.js
More file actions
392 lines (392 loc) · 19.4 KB
/
Copy pathBDGithubDownloader.plugin.js
File metadata and controls
392 lines (392 loc) · 19.4 KB
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
/**
* @name BDGithubDownloader
* @author Ahlawat
* @authorId 887483349369765930
* @version 2.0.3
* @invite SgKSKyh9gY
* @description Download BetterDiscord Plugin/Theme by right clicking on message containing github link.
* @website https://tharki-god.github.io/
* @source https://github.com/Tharki-God/BetterDiscordPlugins
* @updateUrl https://raw.githubusercontent.com/Tharki-God/BetterDiscordPlugins/master/BDGithubDownloader.plugin.js
*/
/*@cc_on
@if (@_jscript)
// Offer to self-install for clueless users that try to run this directly.
var shell = WScript.CreateObject("WScript.Shell");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var pathPlugins = shell.ExpandEnvironmentStrings("%APPDATA%\BetterDiscord\plugins");
var pathSelf = WScript.ScriptFullName;
// Put the user at ease by addressing them in the first person
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 copy myself to BetterDiscord's plugins folder for you?", 0, "Do you need some help?", 0x34) === 6) {
fs.CopyFile(pathSelf, fs.BuildPath(pathPlugins, fs.GetFileName(pathSelf)), true);
// Show the user where to put plugins in the future
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: "887483349369765930",
github_username: "Tharki-God",
}, {
name: "Kirai",
discord_id: "872383230328832031",
github_username: "HiddenKirai",
},
],
version: "2.0.3",
description:
"Download BetterDiscord Plugin/Theme by right clicking on message containing github link.",
github: "https://github.com/Tharki-God/BetterDiscordPlugins",
github_raw:
"https://raw.githubusercontent.com/Tharki-God/BetterDiscordPlugins/master/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"
]
},
],
main: "BDPluginDownloader.plugin.js",
};
return !global.ZeresPluginLibrary
? class {
constructor() {
this._config = config;
}
getName() {
return config.info.name;
}
getAuthor() {
return config.info.authors.map((a) => a.name).join(", ");
}
getDescription() {
return config.info.description;
}
getVersion() {
return config.info.version;
}
load() {
try {
global.ZeresPluginLibrary.PluginUpdater.checkForUpdate(config.info.name, config.info.version, config.info.github_raw);
} catch (err) {
console.error(this.getName(), "Plugin Updater could not be reached.", err);
}
BdApi.showConfirmationModal(
"Library Missing",
`The library plugin needed for ${config.info.name} is missing. Please click Download Now to install it.`, {
confirmText: "Download Now",
cancelText: "Cancel",
onConfirm: () => {
require("request").get(
"https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js",
async(error, response, body) => {
if (error) {
return BdApi.showConfirmationModal("Error Downloading",
[
"Library plugin download failed. Manually install plugin library from the link below.",
BdApi.React.createElement("a", {
href: "https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js",
target: "_blank"
}, "Plugin Link")
], );
}
await new Promise((r) =>
require("fs").writeFile(
require("path").join(
BdApi.Plugins.folder,
"0PluginLibrary.plugin.js"),
body,
r));
});
},
});
}
start() {}
stop() {}
}
: (([Plugin, Library]) => {
const {
Patcher,
ContextMenu,
Settings,
Toasts,
DiscordModules: {
React
}
} = Library;
const Download = (width, height) => React.createElement('svg', {
viewBox: '0 0 24 24',
width,
height
}, React.createElement('path', {
style: {
fill: 'currentColor'
},
d: 'M5.25 20.5h13.498a.75.75 0 0 1 .101 1.493l-.101.007H5.25a.75.75 0 0 1-.102-1.494l.102-.006h13.498H5.25Zm6.633-18.498L12 1.995a1 1 0 0 1 .993.883l.007.117v12.59l3.294-3.293a1 1 0 0 1 1.32-.083l.094.084a1 1 0 0 1 .083 1.32l-.083.094-4.997 4.996a1 1 0 0 1-1.32.084l-.094-.083-5.004-4.997a1 1 0 0 1 1.32-1.498l.094.083L11 15.58V2.995a1 1 0 0 1 .883-.993L12 1.995l-.117.007Z'
}));
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*["'](.+?)["']'/g;
const fs = require("fs").promises;
return class BDGithubDownloader extends Plugin {
getLinks(message) {
var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
return message.match(urlRegex)
}
async loadSetting() {
this.showToast = BdApi.loadData(config.info.name, "showToast") ?? true;
this.autoEnablePlugin = BdApi.loadData(config.info.name, "autoEnablePlugin") ?? true;
this.showPluginDownload = BdApi.loadData(config.info.name, "showPluginDownload") ?? true;
this.autoEnableTheme = BdApi.loadData(config.info.name, "autoEnableTheme") ?? true;
this.showThemeDownload = BdApi.loadData(config.info.name, "showThemeDownload") ?? true;
this.menu = await ContextMenu.getDiscordMenu("MessageContextMenu");
}
async onStart() {
await this.loadSetting();
if (this.showPluginDownload)
this.addPluginDownload();
if (this.showThemeDownload)
this.addThemeDownload();
}
addPluginDownload() {
Patcher.after(
this.menu,
"default",
(_, [props], ret) => {
const message = props.message;
let links = this.getLinks(message.content);
links = links?.filter((c, index) => {
if ((c.endsWith(".plugin.js") && links.indexOf(c) === index) && (isGithubUrl.test(c) || isGithubRawUrl.test(c)))
return true;
});
if (links?.length) {
ret.props.children.splice(3, 0, ContextMenu.buildMenuItem({
name: 'Download Plugin',
separate: true,
id: 'download-plugin',
label: 'Download Plugin',
icon: () => Download('20', '20'),
action: async() => {
for (let plugin of links) {
if (isGithubUrl.test(plugin)) {
plugin = `https://raw.githubusercontent.com/${plugin.split("github.com/")[1]}`.replace("/blob/", "/");
let fileName = plugin.split("/")[plugin.split("/").length - 1];
this.downloadPlugin(plugin, fileName);
} else if (isGithubRawUrl.test(plugin)) {
let fileName = plugin.split("/")[plugin.split("/").length - 1];
this.downloadPlugin(plugin, fileName);
} else {
if (this.showToast)
Toasts.show(`Link Type Not Supported`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
}
}
}, true));
}
});
}
async addThemeDownload() {
Patcher.after(
this.menu,
"default",
(_, [props], ret) => {
const message = props.message;
let links = this.getLinks(message.content);
links = links?.filter((c, index) => {
if ((c.endsWith(".theme.css") && links.indexOf(c) === index) && (isGithubUrl.test(c) || isGithubRawUrl.test(c)))
return true;
});
if (links?.length) {
ret.props.children.splice(3, 0, ContextMenu.buildMenuItem({
name: 'Download Theme',
separate: true,
id: 'download-theme',
label: 'Download Theme',
icon: () => Download('20', '20'),
action: async() => {
for (let theme of links) {
if (isGithubUrl.test(theme)) {
theme = `https://raw.githubusercontent.com/${theme.split("github.com/")[1]}`.replace("/blob/", "/");
let fileName = theme.split("/")[theme.split("/").length - 1];
this.downloadTheme(theme, fileName);
} else if (isGithubRawUrl.test(theme)) {
let fileName = theme.split("/")[theme.split("/").length]
this.downloadTheme(theme, fileName);
} else {
if (this.showToast)
Toasts.show(`Link Type Not Supported`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
}
}
}, true));
}
});
}
async downloadPlugin(plugin, fileName) {
await fetch(plugin).then((response) => {
return response.text();
}).then(async(data) => {
let name = (/@name\s+([^\t^\r^\n]+)|\/\/\**META.*["']name["']\s*:\s*["'](.+?)["']/i.exec(data) || []).filter(n => n)[1];
if (this.showToast)
Toasts.show(`Downloading Plugin: ${name}`, {
icon: 'https://cdn.discordapp.com/attachments/991059796759310386/991736504273621062/ic_fluent_arrow_download_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
await fs.writeFile(require("path").join(
BdApi.Plugins.folder,
fileName),
data,
(err) => {
if (err) {
if (this.showToast) {
Toasts.show(` Error: ${err}.`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
console.log(err);
}
}).then(() => {
if (this.autoEnablePlugin) {
setTimeout(() => {
BdApi.Plugins.enable(name)
}, 2000);
}
})
}).catch((err) => {
if (this.showToast) {
Toasts.show(` Error: ${err}.`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
console.warn('Something went wrong.', err);
});
}
async downloadTheme(theme, fileName) {
await fetch(theme).then((response) => {
return response.text();
}).then(async(data) => {
let name = (/@name\s+([^\t^\r^\n]+)|\/\/\**META.*["']name["']\s*:\s*["'](.+?)["']/i.exec(data) || []).filter(n => n)[1];
if (this.showToast)
Toasts.show(`Downloading Theme: ${name}`, {
icon: 'https://cdn.discordapp.com/attachments/991059796759310386/991736504273621062/ic_fluent_arrow_download_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
await fs.writeFile(require("path").join(
BdApi.Themes.folder,
fileName),
data,
(err) => {
if (err) {
if (this.showToast) {
Toasts.show(` Error: ${err}.`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
console.log(err);
}
}).then(() => {
if (this.autoEnableTheme) {
setTimeout(() => {
BdApi.Themes.enable(name)
}, 2000);
}
})
}).catch((err) => {
if (this.showToast) {
Toasts.show(` Error: ${err}.`, {
icon: 'https://cdn.discordapp.com/attachments/887750789781676092/990999807415955486/ic_fluent_error_circle_24_filled.png?size=4096',
timeout: 5000,
type: 'error'
});
}
console.warn('Something went wrong.', err);
});
}
onStop() {
Patcher.unpatchAll();
}
getSettingsPanel() {
return Settings.SettingPanel.build(this.saveSettings.bind(this),
new Settings.Switch("Popup/Toast", "Display error/success popup", this.showToast, (e) => {
this.showToast = e;
}),
new Settings.SettingGroup("Plugins", {
collapsible: true,
shown: false
}).append(
new Settings.Switch("Show Option", "Weather to option to download plugins.", this.showPluginDownload, (e) => {
this.showPluginDownload = e;
}),
new Settings.Switch("Auto Enable", "Weather to Automatically Enable the plugin after download.", this.autoEnablePlugin, (e) => {
this.autoEnablePlugin = e;
})),
new Settings.SettingGroup("Themes", {
collapsible: true,
shown: false
}).append(
new Settings.Switch("Show Option", "Weather to option to download themes.", this.showThemeDownload, (e) => {
this.showThemeDownload = e;
}),
new Settings.Switch("Auto Enable", "Weather to Automatically Enable the themes after download.", this.autoEnableTheme, (e) => {
this.autoEnableTheme = e;
})))
}
saveSettings() {
BdApi.saveData(config.info.name, "showToast", this.showToast);
BdApi.saveData(config.info.name, "autoEnablePlugin", this.autoEnablePlugin);
BdApi.saveData(config.info.name, "showPluginDownload", this.showPluginDownload);
BdApi.saveData(config.info.name, "autoEnableTheme", this.autoEnableTheme);
BdApi.saveData(config.info.name, "showThemeDownload", this.showThemeDownload);
}
};
return plugin(Plugin, Library);
})(global.ZeresPluginLibrary.buildPlugin(config));
})();
/*@end@*/