BetterDiscordAddons/Plugins/OwnerTag/OwnerTag.plugin.js

227 lines
9.9 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";}
2019-11-05 11:33:13 +01:00
getVersion () {return "1.1.9";}
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 = {
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-01-09 12:56:24 +01:00
this.patchModules = {
2019-11-05 11:33:13 +01:00
MemberListItem: "render",
MessageUsername: "render",
UserPopout: "componentDidMount",
UserProfile: "componentDidMount"
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-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 () {
2019-01-22 11:28:32 +01:00
if (!global.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");
let settingsitems = [], inneritems = [];
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
}));
for (let key in settings) (!this.defaults.settings[key].inner ? settingsitems : inneritems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSwitch, {
className: BDFDB.disCN.marginbottom8,
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
}));
return BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2019-01-09 12:56:24 +01: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) {
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());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2019-01-09 12:56:24 +01:00
document.head.appendChild(libraryScript);
}
2019-01-17 23:48:29 +01:00
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
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 () {
2019-01-17 23:48:29 +01:00
if (global.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 () {
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-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 11:33:13 +01:00
onSettingsClosed (instance, wrapper, returnvalue) {
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);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInChatWindow")) {
this.injectOwnerTag(BDFDB.ReactUtils.getValue(e.returnvalue, "props.children.props.children"), user, usertype, 2, e.instance.props.isCompact ? BDFDB.disCN.bottagmessagecompact : BDFDB.disCN.bottagmessagecozy);
}
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) {
let usertype = this.getUserType(e.instance.props.user);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInUserPopout")) {
let nameTag = e.node.querySelector(BDFDB.dotCN.nametag);
let tagProps = BDFDB.ReactUtils.findProps(nameTag, {name:"DiscordTag", up:true});
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) {
let usertype = this.getUserType(e.instance.props.user);
if (usertype && BDFDB.DataUtils.get(this, "settings", "addInUserProfil")) {
let nameTag = e.node.querySelector(BDFDB.dotCN.nametag);
let tagProps = BDFDB.ReactUtils.findProps(nameTag, {name:"DiscordTag", up:true});
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) {
let renderWrapper = document.createElement("span");
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
}