BetterDiscordAddons/Plugins/BetterFriendCount/BetterFriendCount.plugin.js

125 lines
5.0 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-11-04 22:07:00 +01:00
getVersion () {return "1.2.0";}
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-10-30 10:30:46 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-04-23 08:42:15 +02:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
TabBar: "render",
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());
2020-01-14 00:06:07 +01: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();
2020-01-14 00:06:07 +01:00
this.startTimeout = setTimeout(_ => {
2019-11-01 10:27:07 +01:00
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
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-30 10:49:36 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this, "TabBar");
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01: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;
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".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-10-29 18:55:11 +01:00
processTabBar (e) {
if (e.returnvalue.props.children) for (let checkchild of e.returnvalue.props.children) if (checkchild && checkchild.props.id == "ADD_FRIEND") {
2019-10-17 20:32:58 +02:00
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]]]++;
2019-10-29 18:55:11 +01:00
for (let child of e.returnvalue.props.children) if (child && child.props.id != "ADD_FRIEND") {
2019-10-17 20:32:58 +02:00
let newchildren = [Array.isArray(child.props.children) ? child.props.children[0] : child.props.children];
switch (child.props.id) {
case "ALL":
2019-11-04 22:07:00 +01:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BadgeComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.FRIEND
}));
break;
case "ONLINE":
2019-11-04 22:07:00 +01:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BadgeComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: BDFDB.LibraryModules.StatusMetaUtils.getOnlineFriendCount()
}));
break;
case "PENDING":
2019-11-04 22:07:00 +01:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BadgeComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.PENDING_INCOMING
}));
2019-11-04 22:07:00 +01:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BadgeComponents.NumberBadge, {
2019-10-17 20:32:58 +02:00
className: BDFDB.disCN.settingstabbarbadge,
count: relationshipCount.PENDING_OUTGOING
}));
break;
case "BLOCKED":
2019-11-04 22:07:00 +01:00
newchildren.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BadgeComponents.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-29 18:55:11 +01:00
processFriendRow () {
2019-11-01 11:09:32 +01:00
BDFDB.TimeUtils.clear(this.rerenderTimeout);
2020-01-14 00:06:07 +01:00
this.rerenderTimeout = BDFDB.TimeUtils.timeout(_ => {
2019-10-17 20:32:58 +02:00
delete this.rerenderTimeout;
2019-10-30 10:49:36 +01: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
}