BetterDiscordAddons/PluginsV2/BetterFriendCount/index.js

101 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-10-11 10:21:26 +02:00
module.exports = (Plugin, Api, Vendor) => {
2019-02-04 09:40:44 +01:00
if (!global.BDFDB || typeof BDFDB != "object") global.BDFDB = {myPlugins:{}, BDv2Api: Api};
2018-10-11 10:21:26 +02:00
return class extends Plugin {
initConstructor () {
2019-01-26 23:54:46 +01:00
this.patchModules = {
"TabBar":"componentDidMount",
"NameTag":["componentWillMount","componentWillUnmount"]
};
2018-10-11 10:21:26 +02:00
this.css = `
2019-01-26 23:54:46 +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-26 23:54:46 +01:00
${BDFDB.dotCNS.friends + BDFDB.dotCNS.settingstabbar + BDFDB.dotCN.badgewrapper}.betterfriendcount-badge {
margin-left: 5px !important;
}
2018-10-11 10:21:26 +02:00
`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.relationshipTypes = {};
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
onStart () {
2019-02-04 09:40:44 +01:00
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.name] = this;
2019-01-26 22:45:19 +01:00
var libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (!libraryScript || performance.now() - libraryScript.getAttribute("date") > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
2019-01-26 22:45:19 +01:00
libraryScript.setAttribute("date", performance.now());
2019-01-30 21:23:49 +01:00
libraryScript.addEventListener("load", () => {if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2019-01-26 22:45:19 +01: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-26 22:45:19 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-26 23:54:46 +01:00
if (this.started) return true;
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");
2019-01-26 23:54:46 +01:00
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
return true;
}
else {
2019-01-24 13:37:08 +01:00
console.error(`%c[${this.name}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!');
2018-10-11 10:21:26 +02:00
return false;
}
}
onStop () {
2019-01-26 22:45:19 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-26 23:54:46 +01:00
BDFDB.removeEles(".betterfriendcount-badge");
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
BDFDB.unloadMessage(this);
return true;
}
else {
return false;
}
}
2019-01-26 23:54:46 +01: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-01-26 23:54:46 +01:00
processTabBar (instance, wrapper) {
if (instance.props && instance.props.children && instance.props.children[0].key == "ADD_FRIEND") this.addCountNumbers(wrapper);
}
2019-01-26 22:45:19 +01:00
2019-01-26 23:54:46 +01:00
processNameTag (instance, wrapper) {
if (wrapper.parentElement && BDFDB.containsClass(wrapper.parentElement, BDFDB.disCN.friendscolumn)) this.addCountNumbers();
}
2019-01-26 22:45:19 +01:00
2019-01-26 23:54:46 +01:00
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");
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
}
}
};