BetterDiscordAddons/Plugins/BetterFriendCount/BetterFriendCount.plugin.js

133 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-01-31 17:06:48 +01: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-04-26 13:03:30 +02:00
getVersion () {return "1.1.5";}
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
2018-10-11 10:21:26 +02:00
initConstructor () {
2019-04-23 08:42:15 +02:00
this.changelog = {
2019-04-24 10:13:57 +02:00
"fixed":[["New Classes","Fixed the plugin for the new class update"]]
2019-04-23 08:42:15 +02:00
};
this.patchModules = {
"TabBar":"componentDidMount",
"NameTag":["componentWillMount","componentWillUnmount"]
};
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.css = `
2019-04-26 12:23:23 +02:00
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.settingstabbarbadge}:not(.betterfriendcount-badge) {
2018-10-11 10:21:26 +02:00
display: none !important;
}
2019-04-26 12:23:23 +02:00
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.settingstabbarbadge}.betterfriendcount-badge {
2018-10-11 10:21:26 +02:00
margin-left: 5px !important;
}
`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.relationshipTypes = {};
}
//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");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.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
this.libLoadTimeout = setTimeout(() => {
libraryScript.remove();
require("request")("https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js", (error, response, body) => {
if (body) {
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("date", performance.now());
libraryScript.innerText = body;
document.head.appendChild(libraryScript);
}
this.initialize();
});
}, 15000);
}
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;
2018-10-11 10:21:26 +02:00
BDFDB.loadMessage(this);
2019-01-26 22:45:19 +01:00
2018-12-20 22:54:42 +01:00
this.FriendUtils = BDFDB.WebModules.findByProperties("getFriendIDs", "getRelationships");
this.UserMetaStore = BDFDB.WebModules.findByProperties("getStatus", "getOnlineFriendCount");
let RelationshipTypes = BDFDB.WebModules.findByProperties("RelationshipTypes").RelationshipTypes;
for (let type in RelationshipTypes) this.relationshipTypes[RelationshipTypes[type]] = type;
2019-01-26 22:45:19 +01:00
BDFDB.WebModules.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
else {
2019-02-12 21:56:34 +01:00
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) {
BDFDB.removeEles(".betterfriendcount-badge");
2018-10-11 10:21:26 +02:00
BDFDB.unloadMessage(this);
}
}
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
processTabBar (instance, wrapper) {
2019-04-26 12:31:22 +02:00
if (instance.props && instance.props.children) for (let child of instance.props.children) if ((child.key || (child.props && child.props.id)) == "ADD_FRIEND") this.addCountNumbers(wrapper);
}
2019-01-26 22:45:19 +01:00
processNameTag (instance, wrapper) {
2019-01-17 23:48:29 +01:00
if (wrapper.parentElement && BDFDB.containsClass(wrapper.parentElement, BDFDB.disCN.friendscolumn)) this.addCountNumbers();
}
2019-01-26 22:45:19 +01:00
addCountNumbers (wrapper = document.querySelector(BDFDB.dotCNS.friends + BDFDB.dotCN.settingstabbar)) {
if (!wrapper) return;
2019-04-23 08:42:15 +02:00
let tabitems = wrapper.querySelectorAll(BDFDB.dotCN.settingsitem + BDFDB.notCN.settingstabbarprimary);
if (!tabitems || tabitems.length < 4) return;
BDFDB.removeEles(".betterfriendcount-badge");
2019-01-26 22:45:19 +01:00
let relationships = this.FriendUtils.getRelationships(), relationshipCount = {};
for (let type in this.relationshipTypes) relationshipCount[this.relationshipTypes[type]] = 0;
for (let id in relationships) relationshipCount[this.relationshipTypes[relationships[id]]]++;
2019-04-26 13:03:30 +02:00
for (let item of tabitems) switch (BDFDB.getReactValue(item, "return.memoizedProps.id") || BDFDB.getReactValue(item, "return.return.memoizedProps.id")) {
2019-04-24 10:13:57 +02:00
case "ALL":
2019-04-26 12:23:23 +02:00
item.appendChild(this.createBadge(relationshipCount.FRIEND, "friendcount"));
2019-04-24 10:13:57 +02:00
break;
case "ONLINE":
2019-04-26 12:23:23 +02:00
item.appendChild(this.createBadge(this.UserMetaStore.getOnlineFriendCount(), "onlinefriendcount"));
2019-04-24 10:13:57 +02:00
break;
case "PENDING":
2019-04-26 12:23:23 +02:00
item.appendChild(this.createBadge(relationshipCount.PENDING_INCOMING, "requestincount"));
item.appendChild(this.createBadge(relationshipCount.PENDING_OUTGOING, "requestoutcount"));
2019-04-24 10:13:57 +02:00
break;
case "BLOCKED":
2019-04-26 12:23:23 +02:00
item.appendChild(this.createBadge(relationshipCount.BLOCKED, "blockedcount"));
2019-04-24 10:13:57 +02:00
break;
}
2018-10-11 10:21:26 +02:00
}
2019-04-26 12:23:23 +02:00
createBadge (amount, type) {
return BDFDB.htmlToElement(`<div class="${BDFDB.disCNS.settingstabbarbadge + BDFDB.disCN.guildbadgenumberbadge} betterfriendcount-badge ${type}" style="background-color: rgb(240, 71, 71); width: ${amount > 99 ? 30 : (amount > 9 ? 22 : 16)}px; padding-right: ${amount > 99 ? 0 : (amount > 9 ? 0 : 1)}px;">${amount}</div>`)
}
2018-10-11 10:21:26 +02:00
}