BetterDiscordAddons/Plugins/OwnerTag/OwnerTag.plugin.js

240 lines
11 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"OwnerTag","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/OwnerTag","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/OwnerTag/OwnerTag.plugin.js"}*//
2019-01-09 12:56:24 +01:00
class OwnerTag {
getName () {return "OwnerTag";}
2020-01-06 22:16:17 +01:00
getVersion () {return "1.2.4";}
2019-01-09 12:56:24 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Adds a Tag like Bottags to the Serverowner.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-01-28 13:42:22 +01:00
this.changelog = {
2020-01-06 22:16:17 +01:00
"fixed":[["User Profile","Switching users in the user profile modal via mutual friends now properly removes the owner tag"]],
2019-11-05 11:33:13 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-01-28 13:42:22 +01:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
MemberListItem: "render",
MessageUsername: "render",
2019-11-21 22:32:44 +01:00
UserPopout: ["componentDidMount", "componentDidUpdate"],
UserProfile: ["componentDidMount", "componentDidUpdate"]
2019-11-14 17:56:26 +01:00
}
2019-01-09 12:56:24 +01:00
};
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 () {
2019-11-05 11:33:13 +01:00
this.css = `
${BDFDB.dotCN.memberownericon}.admin-crown {
color: #b3b3b3;
}
`;
2019-11-21 20:55:37 +01:00
2019-01-09 12:56:24 +01:00
this.defaults = {
settings: {
addInChatWindow: {value:true, inner:true, description:"Messages"},
addInMemberList: {value:true, inner:true, description:"Member List"},
addInUserPopout: {value:true, inner:true, description:"User Popouts"},
2019-01-09 13:05:51 +01:00
addInUserProfil: {value:true, inner:true, description:"User Profile Modal"},
useRoleColor: {value:true, inner:false, description:"Use the Rolecolor instead of the default blue."},
2019-01-28 13:42:22 +01:00
useBlackFont: {value:false, inner:false, description:"Instead of darkening the Rolecolor on bright colors use black font."},
2019-05-13 22:31:55 +02:00
useCrown: {value:false, inner:false, description:"Use the Crown Icon instead of the OwnerTag."},
hideNativeCrown: {value:true, inner:false, description:"Hide the native Crown Icon (not the Plugin one)."},
2019-02-13 13:29:23 +01:00
addForAdmins: {value:false, inner:false, description:"Also add the Tag for any user with Admin rights."}
2019-01-09 12:56:24 +01:00
},
inputs: {
2019-02-13 13:29:23 +01:00
ownTagName: {value:"Owner", description:"Owner Tag Text for Owners"},
ownAdminTagName: {value:"Admin", description:"Owner Tag Text for Admins"}
2019-01-09 12:56:24 +01:00
}
};
}
2019-01-26 22:45:19 +01:00
2019-01-09 12:56:24 +01:00
getSettingsPanel () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 23:04:35 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-11-05 11:33:13 +01:00
let inputs = BDFDB.DataUtils.get(this, "inputs");
2019-12-18 16:45:08 +01:00
let settingspanel, settingsitems = [], inneritems = [];
2019-11-05 11:33:13 +01:00
for (let key in inputs) settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
plugin: this,
keys: ["inputs", key],
label: this.defaults.inputs[key].description,
basis: "50%",
value: inputs[key]
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
2019-11-05 21:16:39 +01:00
for (let key in settings) (!this.defaults.settings[key].inner ? settingsitems : inneritems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2019-11-05 11:33:13 +01:00
className: BDFDB.disCN.marginbottom8,
2019-11-05 21:16:39 +01:00
type: "Switch",
2019-11-05 11:33:13 +01:00
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelInner, {
title: "Add Owner Tag in:",
first: settingsitems.length == 0,
last: true,
children: inneritems
}));
2019-12-18 16:45:08 +01:00
return settingspanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2019-01-09 12:56:24 +01:00
}
//legacy
load () {}
start () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
2020-01-21 12:56:26 +01:00
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
2019-05-26 13:55:26 +02:00
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2019-01-09 12:56:24 +01:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2019-01-09 12:56:24 +01: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();});
2019-01-09 12:56:24 +01:00
document.head.appendChild(libraryScript);
}
2020-01-17 19:50:31 +01:00
else if (window.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);
2019-01-09 12:56:24 +01:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +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);
2019-01-09 12:56:24 +01: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!");
2019-01-09 12:56:24 +01:00
}
stop () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-11-05 11:33:13 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-09-04 12:34:02 +02:00
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2019-01-09 12:56:24 +01:00
}
}
2019-01-26 22:45:19 +01:00
2019-01-09 12:56:24 +01:00
// begin of own functions
2019-11-05 20:35:35 +01:00
onSettingsClosed () {
2019-11-05 11:33:13 +01:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-01-09 12:56:24 +01:00
}
2019-07-18 12:18:54 +02:00
}
2019-09-04 12:34:02 +02:00
2019-11-05 11:33:13 +01:00
processMemberListItem (e) {
let usertype = this.getUserType(e.instance.props.user);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInMemberList")) {
this.injectOwnerTag(BDFDB.ReactUtils.getValue(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, usertype, 1, BDFDB.disCN.bottagmember);
}
2019-07-18 12:18:54 +02:00
}
2019-09-04 12:34:02 +02:00
2019-11-05 11:33:13 +01:00
processMessageUsername (e) {
let user = BDFDB.ReactUtils.getValue(e.instance, "props.message.author");
let usertype = this.getUserType(user);
2019-11-17 12:21:34 +01:00
if (usertype && user && typeof e.returnvalue.props.children == "function" && BDFDB.DataUtils.get(this, "settings", "addInChatWindow")) {
let renderChildren = e.returnvalue.props.children;
2019-11-17 12:34:38 +01:00
e.returnvalue.props.children = (...args) => {
let renderedChildren = renderChildren(...args);
2019-11-17 12:23:19 +01:00
this.injectOwnerTag(renderedChildren.props.children, user, usertype, 2, e.instance.props.isCompact ? BDFDB.disCN.bottagmessagecompact : BDFDB.disCN.bottagmessagecozy);
2019-11-17 12:21:34 +01:00
return renderedChildren;
};
2019-11-05 11:33:13 +01:00
}
2019-01-09 12:56:24 +01:00
}
2019-01-26 22:45:19 +01:00
2019-11-05 11:33:13 +01:00
processUserPopout (e) {
2020-01-06 22:16:17 +01:00
BDFDB.DOMUtils.remove(e.node.querySelectorAll(".owner-tag"));
2019-11-05 11:33:13 +01:00
let usertype = this.getUserType(e.instance.props.user);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInUserPopout")) {
let nameTag = e.node.querySelector(BDFDB.dotCN.nametag);
2019-11-21 20:55:37 +01:00
let tagProps = BDFDB.ReactUtils.findProps(nameTag, {name:["DiscordTag", "ColoredFluxTag"], up:true});
2019-11-05 11:33:13 +01:00
if (nameTag && tagProps) this.appendOwnerTag(nameTag, e.instance.props.user, usertype, ((tagProps.botClass || "") + " " + BDFDB.disCNS.bottagnametag).trim(), tagProps.invertBotTagColor);
2019-01-09 12:56:24 +01:00
}
}
2019-01-26 22:45:19 +01:00
2019-11-05 11:33:13 +01:00
processUserProfile (e) {
2020-01-06 22:16:17 +01:00
BDFDB.DOMUtils.remove(e.node.querySelectorAll(".owner-tag"));
2019-11-05 11:33:13 +01:00
let usertype = this.getUserType(e.instance.props.user);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInUserProfil")) {
let nameTag = e.node.querySelector(BDFDB.dotCN.nametag);
2019-11-21 20:55:37 +01:00
let tagProps = BDFDB.ReactUtils.findProps(nameTag, {name:["DiscordTag", "ColoredFluxTag"], up:true});
2019-11-05 11:33:13 +01:00
if (nameTag && tagProps) this.appendOwnerTag(nameTag, e.instance.props.user, usertype, ((tagProps.botClass || "") + " " + BDFDB.disCNS.bottagnametag).trim(), tagProps.invertBotTagColor);
2019-01-09 12:56:24 +01:00
}
}
2019-01-26 22:45:19 +01:00
2019-11-05 11:33:13 +01:00
injectOwnerTag (children, user, usertype, insertindex, tagclass = "", inverted = false) {
if (!BDFDB.ArrayUtils.is(children) || !user) return;
2019-10-22 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-11-05 11:33:13 +01:00
let [_, index] = BDFDB.ReactUtils.findChildren(settings.useCrown || settings.hideNativeCrown ? children : null, {props:[["text",[BDFDB.LanguageUtils.LanguageStrings.GROUP_OWNER, BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER]]]});
if (index > -1) children[index] = null;
children.splice(insertindex, 0, this.createOwnerTag(user, usertype, tagclass, inverted, settings));
}
appendOwnerTag (parent, user, usertype, tagclass = "", inverted = false) {
2019-11-21 22:32:44 +01:00
BDFDB.DOMUtils.remove(parent.querySelectorAll(".owner-tag"));
let renderWrapper = BDFDB.DOMUtils.create(`<span class="owner-tag"></span>`);
2019-11-05 11:33:13 +01:00
BDFDB.ReactUtils.render(this.createOwnerTag(user, usertype, tagclass, inverted), renderWrapper);
parent.appendChild(renderWrapper);
}
createOwnerTag (user, usertype, tagclass, inverted, settings = BDFDB.DataUtils.get(this, "settings")) {
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(BDFDB.LibraryModules.LastChannelStore.getChannelId());
let member = settings.useRoleColor ? (BDFDB.LibraryModules.MemberStore.getMember(channel.guild_id, user.id) || {}) : {};
let isowner = usertype == 2;
if (settings.useCrown) {
let label = isowner ? (channel.type == 3 ? BDFDB.LanguageUtils.LanguageStrings.GROUP_OWNER : BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER) : BDFDB.LanguageUtils.LanguageStrings.ADMINISTRATOR;
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: label,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCNS.memberownericon + (isowner ? "owner-crown" : "admin-crown"),
name: BDFDB.LibraryComponents.SvgIcon.Names.CROWN,
"aria-label": label
})
});
2019-01-28 13:42:22 +01:00
}
2019-05-11 18:04:29 +02:00
else {
2019-11-05 11:33:13 +01:00
let tagcolor = BDFDB.ColorUtils.convert(member.colorString, "RGBA");
let isbright = BDFDB.ColorUtils.isBright(tagcolor);
tagcolor = isbright ? (settings.useBlackFont ? tagcolor : BDFDB.ColorUtils.change(tagcolor, -0.3)) : tagcolor;
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BotTag, {
className: tagclass,
tag: BDFDB.DataUtils.get(this, "inputs", isowner ? "ownTagName" : "ownAdminTagName"),
invertColor: inverted,
style: {
backgroundColor: inverted ? (isbright && settings.useBlackFont ? "black" : null) : tagcolor,
color: !inverted ? (isbright && settings.useBlackFont ? "black" : null) : tagcolor
}
2019-01-28 13:42:22 +01:00
});
}
2019-01-09 12:56:24 +01:00
}
2019-11-05 11:33:13 +01:00
getUserType (user) {
if (!user) return 0;
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(BDFDB.LibraryModules.LastChannelStore.getChannelId());
if (!channel) return 0;
let guild = BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id);
let isowner = channel.ownerId == user.id || guild && guild.ownerId == user.id;
if (!(isowner || (BDFDB.DataUtils.get(this, "settings", "addForAdmins") && BDFDB.UserUtils.can("ADMINISTRATOR", user.id)))) return 0;
return isowner ? 2 : 1;
2019-05-13 22:31:55 +02:00
}
2019-01-09 15:17:35 +01:00
}