BetterDiscordAddons/Plugins/BetterFriendCount/BetterFriendCount.plugin.js

120 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"BetterFriendCount","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/BetterFriendCount","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/BetterFriendCount/BetterFriendCount.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class BetterFriendCount {
2019-01-09 23:20:20 +01:00
getName () {return "BetterFriendCount";}
2019-09-28 13:48:40 +02:00
getVersion () {return "1.1.8";}
2019-01-09 23:20:20 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Shows the amount of total and online friends and blocked users in the friends tab.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-04-23 08:42:15 +02:00
this.changelog = {
2019-09-11 12:14:43 +02:00
"fixed":[["Light Theme Update","Fixed bugs for the Light Theme Update, which broke 99% of my plugins"]]
2019-04-23 08:42:15 +02:00
};
2019-09-04 12:34:02 +02:00
this.patchModules = {
2019-10-17 20:32:58 +02:00
"TabBar":"render",
2019-07-18 11:39:57 +02:00
"FriendRow":["componentWillMount","componentWillUnmount"]
};
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2018-10-11 10:21:26 +02:00
this.relationshipTypes = {};
2019-09-11 12:14:43 +02:00
for (let type in BDFDB.DiscordConstants.RelationshipTypes) this.relationshipTypes[BDFDB.DiscordConstants.RelationshipTypes[type]] = type;
2018-10-11 10:21:26 +02:00
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
2019-05-26 13:55:26 +02:00
}
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2018-10-11 10:21:26 +02:00
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
}
initialize () {
2019-01-22 11:05:54 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-26 22:45:19 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this, 'TabBar');
2018-10-11 10:21:26 +02:00
}
2019-10-22 18:55:25 +02:00
else console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!');
2018-10-11 10:21:26 +02:00
}
stop () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
BDFDB.removeEles(".betterfriendcount-badge");
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processTabBar (instance, wrapper, returnvalue) {
2019-10-17 20:32:58 +02:00
if (returnvalue && returnvalue.props.children) for (let checkchild of returnvalue.props.children) if (checkchild && checkchild.props.id == "ADD_FRIEND") {
let relationships = BDFDB.LibraryModules.FriendUtils.getRelationships(), relationshipCount = {};
for (let type in this.relationshipTypes) relationshipCount[this.relationshipTypes[type]] = 0;
for (let id in relationships) relationshipCount[this.relationshipTypes[relationships[id]]]++;
for (let child of returnvalue.props.children) if (child && child.props.id != "ADD_FRIEND") {
let newchildren = [Array.isArray(child.props.children) ? child.props.children[0] : child.props.children];
switch (child.props.id) {
case "ALL":
2019-10-22 18:55:25 +02:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.FRIEND
}));
break;
case "ONLINE":
2019-10-22 18:55:25 +02:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: BDFDB.LibraryModules.StatusMetaUtils.getOnlineFriendCount()
}));
break;
case "PENDING":
2019-10-22 18:55:25 +02:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.PENDING_INCOMING
}));
2019-10-22 18:55:25 +02:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.PENDING_OUTGOING
}));
break;
case "BLOCKED":
2019-10-22 18:55:25 +02:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.BLOCKED
}));
break;
}
child.props.children = newchildren;
}
2019-04-24 10:13:57 +02:00
}
2018-10-11 10:21:26 +02:00
}
2019-09-04 12:34:02 +02:00
2019-10-17 20:32:58 +02:00
processFriendRow (instance, wrapper, returnvalue) {
clearTimeout(this.rerenderTimeout);
this.rerenderTimeout = setTimeout(() => {
delete this.rerenderTimeout;
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this, 'TabBar');
2019-10-17 20:32:58 +02:00
}, 1000);
2019-04-26 12:23:23 +02:00
}
2018-10-11 10:21:26 +02:00
}