BetterDiscordAddons/Plugins/BetterFriendCount/BetterFriendCount.plugin.js

99 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-10-11 10:21:26 +02:00
//META{"name":"BetterFriendCount"}*//
class BetterFriendCount {
2019-01-09 23:20:20 +01:00
getName () {return "BetterFriendCount";}
getVersion () {return "1.1.0";}
getAuthor () {return "DevilBro";}
getDescription () {return "Shows the amount of total and online friends and blocked users in the friends tab.";}
2018-10-11 10:21:26 +02:00
initConstructor () {
this.patchModules = {
"TabBar":"componentDidMount",
"NameTag":["componentWillMount","componentWillUnmount"]
};
2018-10-11 10:21:26 +02:00
this.css = `
2019-01-09 23:20:20 +01:00
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.badge}:not(.betterfriendcount-badge),
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.badgewrapper}:not(.betterfriendcount-badge) {
2018-10-11 10:21:26 +02:00
display: none !important;
}
2019-01-09 23:20:20 +01:00
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.badgewrapper}.betterfriendcount-badge {
2018-10-11 10:21:26 +02:00
margin-left: 5px !important;
}
`;
this.relationshipTypes = {};
}
//legacy
load () {}
start () {
var libraryScript = null;
if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) {
libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
document.head.appendChild(libraryScript);
}
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize();
else libraryScript.addEventListener("load", () => {this.initialize();});
}
initialize () {
if (typeof BDFDB === "object") {
BDFDB.loadMessage(this);
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;
BDFDB.WebModules.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
}
}
stop () {
if (typeof BDFDB === "object") {
BDFDB.removeEles(".betterfriendcount-badge");
2018-10-11 10:21:26 +02:00
BDFDB.unloadMessage(this);
}
}
// begin of own functions
processTabBar (instance, wrapper) {
if (instance.props && instance.props.children && instance.props.children[0].key == "ADD_FRIEND") this.addCountNumbers(wrapper);
}
processNameTag (instance, wrapper) {
if (wrapper.parentElement && wrapper.parentElement.classList && wrapper.parentElement.classList.contains(BDFDB.disCN.friendscolumn)) this.addCountNumbers();
}
addCountNumbers (wrapper = document.querySelector(BDFDB.dotCNS.friends + BDFDB.dotCN.settingstabbar)) {
if (!wrapper) return;
let tabitems = wrapper.querySelectorAll(BDFDB.dotCN.settingsitem);
if (!tabitems || tabitems.length < 5) return;
BDFDB.removeEles(".betterfriendcount-badge");
2018-10-11 10:21:26 +02: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]]]++;
let badgeclass = BDFDB.disCN.badgewrapper;
tabitems[1].appendChild(BDFDB.htmlToElement(`<div class="${badgeclass} betterfriendcount-badge friendcount">${relationshipCount.FRIEND}</div>`));
tabitems[2].appendChild(BDFDB.htmlToElement(`<div class="${badgeclass} betterfriendcount-badge onlinefriendcount">${this.UserMetaStore.getOnlineFriendCount()}</div>`));
tabitems[3].appendChild(BDFDB.htmlToElement(`<div class="${badgeclass} betterfriendcount-badge requestincount">${relationshipCount.PENDING_INCOMING}</div>`));
tabitems[3].appendChild(BDFDB.htmlToElement(`<div class="${badgeclass} betterfriendcount-badge requestoutcount">${relationshipCount.PENDING_OUTGOING}</div>`));
tabitems[4].appendChild(BDFDB.htmlToElement(`<div class="${badgeclass} betterfriendcount-badge blockedcount">${relationshipCount.BLOCKED}</div>`));
2018-10-11 10:21:26 +02:00
}
}