This commit is contained in:
Mirco Wittrien 2021-08-10 19:12:51 +02:00
parent 70767d6458
commit cbab01ddab
2 changed files with 185 additions and 162 deletions

View File

@ -2,7 +2,7 @@
* @name BDFDB * @name BDFDB
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 1.7.13 * @version 1.7.14
* @description Required Library for DevilBro's Plugins * @description Required Library for DevilBro's Plugins
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -19,15 +19,10 @@ module.exports = (_ => {
"info": { "info": {
"name": "BDFDB", "name": "BDFDB",
"author": "DevilBro", "author": "DevilBro",
"version": "1.7.13", "version": "1.7.14",
"description": "Required Library for DevilBro's Plugins" "description": "Required Library for DevilBro's Plugins"
}, },
"rawUrl": `https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js`, "rawUrl": `https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js`
"changeLog": {
"fixed": {
"Date Formatter": "No longer shows future dates as 'today'"
}
}
}; };
const DiscordObjects = {}; const DiscordObjects = {};
@ -973,36 +968,161 @@ module.exports = (_ => {
const request = require("request"), fs = require("fs"), path = require("path"); const request = require("request"), fs = require("fs"), path = require("path");
const cssPath = path.join(BDFDB.BDUtils.getPluginsFolder(), "0BDFDB.raw.css");
const dataPath = path.join(BDFDB.BDUtils.getPluginsFolder(), "0BDFDB.data.json");
const loadBackup = _ => { InternalBDFDB.writeConfig = function (path, config) {
const backup = fs.existsSync(dataPath) && (fs.readFileSync(dataPath) || "").toString() || null; try {fs.writeFileSync(path, JSON.stringify(config, null, " "));}
if (!backup) BdApi.alert("Error", "Could not initiate BDFDB Library Plugin. Check your Internet Connection and make sure GitHub isn't blocked by your Network or try disabling your VPN/Proxy."); catch (err) {}
return backup; };
InternalBDFDB.readConfig = function (path) {
try {return JSON.parse(fs.readFileSync(path));}
catch (err) {return {};}
}; };
let InternalData, libraryCSS; BDFDB.DataUtils = {};
const requestLibraryData = tryAgain => { BDFDB.DataUtils.save = function (data, plugin, key, id) {
request.get(`https://mwittrien.github.io/BetterDiscordAddons/Library/_res/BDFDB.raw.css`, (e, r, b) => { plugin = plugin == BDFDB && InternalBDFDB || plugin;
if ((e || !b || r.statusCode != 200) && tryAgain) return BDFDB.TimeUtils.timeout(_ => requestLibraryData(), 10000); let pluginName = typeof plugin === "string" ? plugin : plugin.name;
libraryCSS = !e && b && r.statusCode == 200 ? b : fs.existsSync(cssPath) && (fs.readFileSync(cssPath) || "").toString(); let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
request.get(`https://mwittrien.github.io/BetterDiscordAddons/Library/_res/BDFDB.data.json`, BDFDB.TimeUtils.suppress((e2, r2, b2) => { let configPath = path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
if (e2 || !b2 || r2.statusCode != 200) {
if (tryAgain) return BDFDB.TimeUtils.timeout(_ => requestLibraryData(), 10000); let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
if (key === undefined) config = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
else { else {
BDFDB.LogUtils.error(["Failed to fetch JSON from GitHub. Could not load data.json!", e2 || ""]); if (id === undefined) config[key] = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
b2 = loadBackup(); else {
if (!BDFDB.ObjectUtils.is(config[key])) config[key] = {};
config[key][id] = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
} }
} }
try {InternalData = JSON.parse(b2);}
let configIsObject = BDFDB.ObjectUtils.is(config);
if (key !== undefined && configIsObject && BDFDB.ObjectUtils.is(config[key]) && BDFDB.ObjectUtils.isEmpty(config[key])) delete config[key];
if (BDFDB.ObjectUtils.isEmpty(config)) {
delete Cache.data[pluginName];
if (fs.existsSync(configPath)) fs.unlinkSync(configPath);
}
else {
if (configIsObject) config = BDFDB.ObjectUtils.sort(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
InternalBDFDB.writeConfig(configPath, config);
}
};
BDFDB.DataUtils.load = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
let pluginName = typeof plugin === "string" ? plugin : plugin.name;
let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
let configPath = path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
let configIsObject = BDFDB.ObjectUtils.is(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
if (key === undefined) return config;
else {
let keyData = configIsObject ? (BDFDB.ObjectUtils.is(config[key]) || config[key] === undefined ? BDFDB.ObjectUtils.deepAssign({}, config[key]) : config[key]) : null;
if (id === undefined) return keyData;
else return !BDFDB.ObjectUtils.is(keyData) || keyData[id] === undefined ? null : keyData[id];
}
};
BDFDB.DataUtils.remove = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
let pluginName = typeof plugin === "string" ? plugin : plugin.name;
let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
let configPath = path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
let configIsObject = BDFDB.ObjectUtils.is(config);
if (key === undefined || !configIsObject) config = {};
else {
if (id === undefined) delete config[key];
else if (BDFDB.ObjectUtils.is(config[key])) delete config[key][id];
}
if (BDFDB.ObjectUtils.is(config[key]) && BDFDB.ObjectUtils.isEmpty(config[key])) delete config[key];
if (BDFDB.ObjectUtils.isEmpty(config)) {
delete Cache.data[pluginName];
if (fs.existsSync(configPath)) fs.unlinkSync(configPath);
}
else {
if (configIsObject) config = BDFDB.ObjectUtils.sort(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
InternalBDFDB.writeConfig(configPath, config);
}
};
BDFDB.DataUtils.get = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
plugin = typeof plugin == "string" ? BDFDB.BDUtils.getPlugin(plugin) : plugin;
const defaults = plugin && plugin.defaults;
if (!BDFDB.ObjectUtils.is(defaults) || key && !BDFDB.ObjectUtils.is(defaults[key])) return id === undefined ? {} : null;
let oldC = BDFDB.DataUtils.load(plugin), newC = {}, update = false;
const checkLayer = (i, j) => {
let isObj = BDFDB.ObjectUtils.is(defaults[i][j].value);
if (!newC[i]) newC[i] = {};
if (oldC[i] == null || oldC[i][j] == null || isObj && (!BDFDB.ObjectUtils.is(oldC[i][j]) || Object.keys(defaults[i][j].value).some(n => defaults[i][j].value[n] != null && !BDFDB.sameProto(defaults[i][j].value[n], oldC[i][j][n])))) {
newC[i][j] = isObj ? BDFDB.ObjectUtils.deepAssign({}, defaults[i][j].value) : defaults[i][j].value;
update = true;
}
else newC[i][j] = oldC[i][j];
};
if (key) {for (let j in defaults[key]) checkLayer(key, j);}
else {for (let i in defaults) if (BDFDB.ObjectUtils.is(defaults[i])) for (let j in defaults[i]) checkLayer(i, j);}
if (update) BDFDB.DataUtils.save(Object.assign({}, oldC, newC), plugin);
if (key === undefined) return newC;
else if (id === undefined) return newC[key] === undefined ? {} : newC[key];
else return newC[key] === undefined || newC[key][id] === undefined ? null : newC[key][id];
};
const cssFileName = "0BDFDB.raw.css";
const dataFileName = "0BDFDB.data.json";
const cssFilePath = path.join(BDFDB.BDUtils.getPluginsFolder(), cssFileName);
const dataFilePath = path.join(BDFDB.BDUtils.getPluginsFolder(), dataFileName);
let InternalData, libHashes = {}, oldLibHashes = BDFDB.DataUtils.load(BDFDB, "hashes"), libraryCSS;
const getBackup = (fileName, path) => {
return libHashes[fileName] && oldLibHashes[fileName] && libHashes[fileName] == oldLibHashes[fileName] && fs.existsSync(path) && (fs.readFileSync(path) || "").toString();
};
const requestLibraryHashes = tryAgain => {
request("https://api.github.com/repos/mwittrien/BetterDiscordAddons/contents/Library/_res/", {headers: {"user-agent": "node.js"}}, (e, r, b) => {
if ((e || !b || r.statusCode != 200) && tryAgain) return BDFDB.TimeUtils.timeout(_ => requestLibraryHashes(), 10000);
try {
b = JSON.parse(b);
libHashes[cssFileName] = (b.find(n => n && n.name == cssFileName) || {}).sha;
libHashes[dataFileName] = (b.find(n => n && n.name == dataFileName) || {}).sha;
BDFDB.DataUtils.save(libHashes, BDFDB, "hashes")
requestLibraryData(true);
}
catch (err) {requestLibraryData(true);}
});
};
const requestLibraryData = tryAgain => {
const parseCSS = css => {
libraryCSS = css;
const backupData = getBackup(dataFileName, dataFilePath);
if (backupData) parseData(backupData);
else request.get(`https://mwittrien.github.io/BetterDiscordAddons/Library/_res/${dataFileName}`, (e, r, b) => {
if ((e || !b || r.statusCode != 200) && tryAgain) return BDFDB.TimeUtils.timeout(_ => requestLibraryData(), 10000);
if (!e && b && r.statusCode == 200) parseData(b, true);
else parseData(fs.existsSync(dataFilePath) && (fs.readFileSync(dataFilePath) || "").toString());
});
};
const parseData = (dataString, fetched) => {
try {InternalData = JSON.parse(dataString);}
catch (err) { catch (err) {
BDFDB.LogUtils.error(["Failed to parse fetched JSON. Could not load data.json!", err]); if (fetched) {
b2 = null; try {
InternalData = JSON.parse(loadBackup()); dataString = fs.existsSync(dataFilePath) && (fs.readFileSync(dataFilePath) || "").toString();
InternalData = JSON.parse(dataString);
} }
if (!e && b && r.statusCode == 200) fs.writeFile(cssPath, b, _ => {}); catch (err2) {BDFDB.LogUtils.error(["Failed to initiate Library!", "Failed Fetch!", dataString ? "Corrupt Backup." : "No Backup.", , err2]);}
if (!e2 && b2 && r2.statusCode == 200) fs.writeFile(dataPath, b2, _ => {}); }
else BDFDB.LogUtils.error(["Failed to initiate Library!", dataString ? "Corrupt Backup." : "No Backup.", err]);
}
if (fetched && dataString) fs.writeFile(dataFilePath, dataString, _ => {});
InternalBDFDB.getWebModuleReq = function () { InternalBDFDB.getWebModuleReq = function () {
if (!InternalBDFDB.getWebModuleReq.req) { if (!InternalBDFDB.getWebModuleReq.req) {
@ -1015,10 +1135,19 @@ module.exports = (_ => {
return InternalBDFDB.getWebModuleReq.req; return InternalBDFDB.getWebModuleReq.req;
}; };
// ADD async proxy check if (InternalData) loadLibrary();
else BdApi.alert("Error", "Could not initiate BDFDB Library Plugin. Check your Internet Connection and make sure GitHub isn't blocked by your Network or try disabling your VPN/Proxy.");
};
loadLibrary(); const backupCSS = getBackup(cssFileName, cssFilePath);
}, "Could not initiate Library!")); if (backupCSS) parseCSS(backupCSS);
else request.get(`https://mwittrien.github.io/BetterDiscordAddons/Library/_res/${cssFileName}`, (e, r, b) => {
if ((e || !b || r.statusCode != 200) && tryAgain) return BDFDB.TimeUtils.timeout(_ => requestLibraryData(), 10000);
if (!e && b && r.statusCode == 200) {
fs.writeFile(cssFilePath, b, _ => {});
parseCSS(b);
}
else parseCSS(fs.existsSync(cssFilePath) && (fs.readFileSync(cssFilePath) || "").toString());
}); });
}; };
const loadLibrary = _ => { const loadLibrary = _ => {
@ -2386,7 +2515,7 @@ module.exports = (_ => {
if (component && component.defaultProps) for (let key in component.defaultProps) if (props[key] == null) props[key] = component.defaultProps[key]; if (component && component.defaultProps) for (let key in component.defaultProps) if (props[key] == null) props[key] = component.defaultProps[key];
try { try {
let child = LibraryModules.React.createElement(component || "div", props) || null; let child = LibraryModules.React.createElement(component || "div", props) || null;
if (errorWrap) return LibraryModules.React.createElement(InternalComponents.ErrorBoundary, {}, child) || null; if (errorWrap) return LibraryModules.React.createElement(InternalComponents.ErrorBoundary, {key: child && child.key || ""}, child) || null;
else return child; else return child;
} }
catch (err) {BDFDB.LogUtils.error(["Could not create React Element!", err]);} catch (err) {BDFDB.LogUtils.error(["Could not create React Element!", err]);}
@ -3119,113 +3248,6 @@ module.exports = (_ => {
if (unreadDMs.length) for (let i in unreadDMs) BDFDB.TimeUtils.timeout(_ => LibraryModules.AckUtils.ack(unreadDMs[i]), i * 1000); if (unreadDMs.length) for (let i in unreadDMs) BDFDB.TimeUtils.timeout(_ => LibraryModules.AckUtils.ack(unreadDMs[i]), i * 1000);
}; };
InternalBDFDB.writeConfig = function (path, config) {
try {LibraryRequires.fs.writeFileSync(path, JSON.stringify(config, null, " "));}
catch (err) {}
};
InternalBDFDB.readConfig = function (path) {
try {return JSON.parse(LibraryRequires.fs.readFileSync(path));}
catch (err) {return {};}
};
BDFDB.DataUtils = {};
BDFDB.DataUtils.save = function (data, plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
let pluginName = typeof plugin === "string" ? plugin : plugin.name;
let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
let configPath = LibraryRequires.path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
if (key === undefined) config = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
else {
if (id === undefined) config[key] = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
else {
if (!BDFDB.ObjectUtils.is(config[key])) config[key] = {};
config[key][id] = BDFDB.ObjectUtils.is(data) ? BDFDB.ObjectUtils.sort(data) : data;
}
}
let configIsObject = BDFDB.ObjectUtils.is(config);
if (key !== undefined && configIsObject && BDFDB.ObjectUtils.is(config[key]) && BDFDB.ObjectUtils.isEmpty(config[key])) delete config[key];
if (BDFDB.ObjectUtils.isEmpty(config)) {
delete Cache.data[pluginName];
if (LibraryRequires.fs.existsSync(configPath)) LibraryRequires.fs.unlinkSync(configPath);
}
else {
if (configIsObject) config = BDFDB.ObjectUtils.sort(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
InternalBDFDB.writeConfig(configPath, config);
}
};
BDFDB.DataUtils.load = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
let pluginName = typeof plugin === "string" ? plugin : plugin.name;
let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
let configPath = LibraryRequires.path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
let configIsObject = BDFDB.ObjectUtils.is(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
if (key === undefined) return config;
else {
let keyData = configIsObject ? (BDFDB.ObjectUtils.is(config[key]) || config[key] === undefined ? BDFDB.ObjectUtils.deepAssign({}, config[key]) : config[key]) : null;
if (id === undefined) return keyData;
else return !BDFDB.ObjectUtils.is(keyData) || keyData[id] === undefined ? null : keyData[id];
}
};
BDFDB.DataUtils.remove = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
let pluginName = typeof plugin === "string" ? plugin : plugin.name;
let fileName = pluginName == "BDFDB" ? "0BDFDB" : pluginName;
let configPath = LibraryRequires.path.join(BDFDB.BDUtils.getPluginsFolder(), fileName + ".config.json");
let config = Cache.data[pluginName] !== undefined ? Cache.data[pluginName] : (InternalBDFDB.readConfig(configPath) || {});
let configIsObject = BDFDB.ObjectUtils.is(config);
if (key === undefined || !configIsObject) config = {};
else {
if (id === undefined) delete config[key];
else if (BDFDB.ObjectUtils.is(config[key])) delete config[key][id];
}
if (BDFDB.ObjectUtils.is(config[key]) && BDFDB.ObjectUtils.isEmpty(config[key])) delete config[key];
if (BDFDB.ObjectUtils.isEmpty(config)) {
delete Cache.data[pluginName];
if (LibraryRequires.fs.existsSync(configPath)) LibraryRequires.fs.unlinkSync(configPath);
}
else {
if (configIsObject) config = BDFDB.ObjectUtils.sort(config);
Cache.data[pluginName] = configIsObject ? BDFDB.ObjectUtils.deepAssign({}, config) : config;
InternalBDFDB.writeConfig(configPath, config);
}
};
BDFDB.DataUtils.get = function (plugin, key, id) {
plugin = plugin == BDFDB && InternalBDFDB || plugin;
plugin = typeof plugin == "string" ? BDFDB.BDUtils.getPlugin(plugin) : plugin;
const defaults = plugin && plugin.defaults;
if (!BDFDB.ObjectUtils.is(defaults) || key && !BDFDB.ObjectUtils.is(defaults[key])) return id === undefined ? {} : null;
let oldC = BDFDB.DataUtils.load(plugin), newC = {}, update = false;
const checkLayer = (i, j) => {
let isObj = BDFDB.ObjectUtils.is(defaults[i][j].value);
if (!newC[i]) newC[i] = {};
if (oldC[i] == null || oldC[i][j] == null || isObj && (!BDFDB.ObjectUtils.is(oldC[i][j]) || Object.keys(defaults[i][j].value).some(n => defaults[i][j].value[n] != null && !BDFDB.sameProto(defaults[i][j].value[n], oldC[i][j][n])))) {
newC[i][j] = isObj ? BDFDB.ObjectUtils.deepAssign({}, defaults[i][j].value) : defaults[i][j].value;
update = true;
}
else newC[i][j] = oldC[i][j];
};
if (key) {for (let j in defaults[key]) checkLayer(key, j);}
else {for (let i in defaults) if (BDFDB.ObjectUtils.is(defaults[i])) for (let j in defaults[i]) checkLayer(i, j);}
if (update) BDFDB.DataUtils.save(Object.assign({}, oldC, newC), plugin);
if (key === undefined) return newC;
else if (id === undefined) return newC[key] === undefined ? {} : newC[key];
else return newC[key] === undefined || newC[key][id] === undefined ? null : newC[key][id];
};
BDFDB.ColorUtils = {}; BDFDB.ColorUtils = {};
BDFDB.ColorUtils.convert = function (color, conv, type) { BDFDB.ColorUtils.convert = function (color, conv, type) {
if (BDFDB.ObjectUtils.is(color)) { if (BDFDB.ObjectUtils.is(color)) {
@ -8443,7 +8465,7 @@ module.exports = (_ => {
if (pluginName) BDFDB.TimeUtils.timeout(_ => BDFDB.BDUtils.reloadPlugin(pluginName)); if (pluginName) BDFDB.TimeUtils.timeout(_ => BDFDB.BDUtils.reloadPlugin(pluginName));
} }
}; };
requestLibraryData(true); requestLibraryHashes(true);
return class BDFDB_Frame { return class BDFDB_Frame {
getName () {return config.info.name;} getName () {return config.info.name;}

View File

@ -2,7 +2,7 @@
* @name SpotifyControls * @name SpotifyControls
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 1.1.8 * @version 1.1.9
* @description Adds a Control Panel while listening to Spotify on a connected Account * @description Adds a Control Panel while listening to Spotify on a connected Account
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -17,12 +17,12 @@ module.exports = (_ => {
"info": { "info": {
"name": "SpotifyControls", "name": "SpotifyControls",
"author": "DevilBro", "author": "DevilBro",
"version": "1.1.8", "version": "1.1.9",
"description": "Adds a Control Panel while listening to Spotify on a connected Account" "description": "Adds a Control Panel while listening to Spotify on a connected Account"
}, },
"changeLog": { "changeLog": {
"fixed": { "fixed": {
"Direct Messages List": "Fixed and Issue where the Plugin would cause the DM List to jump to the top if a DM was selected" "Duplicates": "No longer creates Duplicate Controls"
} }
} }
}; };
@ -792,12 +792,13 @@ module.exports = (_ => {
processAnalyticsContext (e) { processAnalyticsContext (e) {
if (e.instance.props.section == BDFDB.DiscordConstants.AnalyticsSections.ACCOUNT_PANEL) e.instance.props.children = [ if (e.instance.props.section == BDFDB.DiscordConstants.AnalyticsSections.ACCOUNT_PANEL) e.instance.props.children = [
BDFDB.ReactUtils.createElement(SpotifyControlsComponent, { BDFDB.ReactUtils.createElement(SpotifyControlsComponent, {
key: "SPOTIFY_CONTROLS",
song: BDFDB.LibraryModules.SpotifyTrackUtils.getActivity(false), song: BDFDB.LibraryModules.SpotifyTrackUtils.getActivity(false),
maximized: BDFDB.DataUtils.load(this, "playerState", "maximized"), maximized: BDFDB.DataUtils.load(this, "playerState", "maximized"),
timeline: this.settings.general.addTimeline, timeline: this.settings.general.addTimeline,
activityToggle: this.settings.general.addActivityButton activityToggle: this.settings.general.addActivityButton
}, true), }, true),
e.instance.props.children [e.instance.props.children].flat(10).filter(n => !n || n.key != "SPOTIFY_CONTROLS")
].flat(10); ].flat(10);
} }