BetterDiscordAddons/Plugins/OwnerTag/OwnerTag.plugin.js

283 lines
12 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"OwnerTag","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","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
2020-02-04 08:20:40 +01:00
var OwnerTag = (_ => {
2020-04-09 09:29:38 +02:00
const userTypes = {
NONE: 0,
ADMIN: 1,
OWNER: 2
};
2020-06-09 10:11:25 +02:00
var settings = {}, inputs = {};
2020-05-25 17:16:19 +02:00
2020-02-04 08:20:40 +01:00
return class OwnerTag {
getName () {return "OwnerTag";}
2019-01-09 12:56:24 +01:00
2020-07-15 17:06:13 +02:00
getVersion () {return "1.3.0";}
2019-01-09 12:56:24 +01:00
2020-02-04 08:20:40 +01:00
getAuthor () {return "DevilBro";}
2019-01-09 12:56:24 +01:00
2020-02-04 08:20:40 +01:00
getDescription () {return "Adds a Tag like Bottags to the Serverowner.";}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor () {
this.changelog = {
2020-07-15 17:06:13 +02:00
"fixed":[["BotTag","BotTag style is now properly inverted on the userpopout if the user is playing a game etc."]]
2020-02-04 08:20:40 +01:00
};
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
this.patchedModules = {
after: {
MemberListItem: "render",
MessageHeader: "default",
NameTag: "default",
UserPopout: "render"
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
initConstructor () {
this.css = `
${BDFDB.dotCN.memberownericon}.admin-crown {
color: #b3b3b3;
}
${BDFDB.dotCNS.message + BDFDB.dotCN.memberownericon} {
top: 2px;
}
${BDFDB.dotCNS.userprofile + BDFDB.dotCN.memberownericon} {
top: 0px;
}
2020-02-27 11:58:52 +01:00
${BDFDB.dotCNS.messagecozy + BDFDB.dotCN.memberownericon} {
margin-right: .25rem;
}
2020-02-04 08:20:40 +01:00
${BDFDB.dotCNS.messagecozy + BDFDB.dotCN.messageusername} + ${BDFDB.dotCN.memberownericon} {
margin-left: 0;
}
`;
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"},
addInUserProfile: {value:true, inner:true, description:"User Profile Modal"},
useRoleColor: {value:true, inner:false, description:"Use the Rolecolor instead of the default blue."},
useBlackFont: {value:false, inner:false, description:"Instead of darkening the Rolecolor on bright colors use black font."},
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)."},
2020-05-25 17:16:19 +02:00
addForAdmins: {value:false, inner:false, description:"Also add an Admin Tag for any user with Admin rights."},
ignoreBotAdmins: {value:false, inner:false, description:"Do not add the Admin tag for bots with Admin rights."}
2020-02-04 08:20:40 +01:00
},
inputs: {
ownTagName: {value:"Owner", description:"Owner Tag Text for Owners"},
ownAdminTagName: {value:"Admin", description:"Owner Tag Text for Admins"}
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
getSettingsPanel () {
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
let settings = BDFDB.DataUtils.get(this, "settings");
let inputs = BDFDB.DataUtils.get(this, "inputs");
2020-03-28 07:55:39 +01:00
let settingsPanel, settingsItems = [], innerItems = [];
2020-02-04 08:20:40 +01:00
2020-03-28 07:55:39 +01:00
for (let key in inputs) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
plugin: this,
keys: ["inputs", key],
label: this.defaults.inputs[key].description,
basis: "50%",
value: inputs[key]
}));
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8
}));
2020-03-28 07:55:39 +01:00
for (let key in settings) (!this.defaults.settings[key].inner ? settingsItems : innerItems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelInner, {
2020-02-04 08:20:40 +01:00
title: "Add Owner Tag in:",
2020-03-28 07:55:39 +01:00
first: settingsItems.length == 0,
2020-02-04 08:20:40 +01:00
last: true,
2020-03-28 07:55:39 +01:00
children: innerItems
2020-02-04 08:20:40 +01:00
}));
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-04 08:20:40 +01:00
}
2019-01-09 12:56:24 +01:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-02-04 08:20:40 +01:00
load () {}
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
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
}
2020-02-04 08:20:40 +01:00
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
}
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
}
2020-02-04 08:20:40 +01:00
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
2019-10-22 11:37:23 +02:00
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
BDFDB.PluginUtils.clear(this);
}
2019-01-09 12:56:24 +01:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-09 12:56:24 +01:00
2020-02-04 08:20:40 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
2019-01-09 12:56:24 +01:00
}
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
processMemberListItem (e) {
2020-08-11 17:00:48 +02:00
let userType = this.getUserType(e.instance.props.user, e.instance.props.channel && e.instance.props.channel.id);
2020-05-25 17:16:19 +02:00
if (userType && settings.addInMemberList) {
2020-04-09 09:29:38 +02:00
this.injectOwnerTag(BDFDB.ReactUtils.getValue(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, userType, 1, {
tagClass: BDFDB.disCN.bottagmember
});
2020-02-04 08:20:40 +01:00
}
2019-11-05 11:33:13 +01:00
}
2019-09-04 12:34:02 +02:00
2020-02-04 08:20:40 +01:00
processMessageHeader (e) {
2020-05-25 17:16:19 +02:00
if (e.instance.props.message && settings.addInChatWindow) {
2020-08-11 00:09:19 +02:00
let userType = this.getUserType(e.instance.props.message.author, e.instance.props.message.channel_id);
2020-04-09 09:29:38 +02:00
if (userType) {
2020-06-16 17:07:08 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue.props.children.slice(1), {name: "Popout", props: [["className", BDFDB.disCN.messageusername]]});
2020-04-09 09:29:38 +02:00
if (index > -1) this.injectOwnerTag(children, e.instance.props.message.author, userType, e.instance.props.compact ? 0 : 2, {
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
2020-02-27 11:53:07 +01:00
}
2020-02-04 08:20:40 +01:00
}
2019-11-05 11:33:13 +01:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
processNameTag (e) {
if (e.instance.props.user && e.instance.props.className) {
2020-04-09 09:29:38 +02:00
let userType = this.getUserType(e.instance.props.user);
if (userType) {
let inject = false, tagClass = "";
2020-02-04 08:20:40 +01:00
switch (e.instance.props.className) {
case BDFDB.disCN.userpopoutheadertagnonickname:
2020-05-25 17:16:19 +02:00
inject = settings.addInUserPopout;
2020-04-09 09:29:38 +02:00
tagClass = BDFDB.disCN.bottagnametag;
2020-02-04 08:20:40 +01:00
break;
case BDFDB.disCN.userprofilenametag:
2020-05-25 17:16:19 +02:00
inject = settings.addInUserProfile;
2020-04-09 09:29:38 +02:00
tagClass = BDFDB.disCNS.userprofilebottag + BDFDB.disCN.bottagnametag;
2020-02-04 08:20:40 +01:00
break;
}
2020-04-09 09:29:38 +02:00
if (inject) this.injectOwnerTag(e.returnvalue.props.children, e.instance.props.user, userType, 2, {
tagClass: tagClass,
useRem: e.instance.props.useRemSizes,
inverted: e.instance.props.invertBotTagColor
});
2020-02-04 08:20:40 +01:00
}
}
2019-01-09 12:56:24 +01:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
processUserPopout (e) {
2020-05-25 17:16:19 +02:00
if (e.instance.props.user && settings.addInUserPopout) {
2020-08-11 00:09:19 +02:00
let userType = this.getUserType(e.instance.props.user, e.instance.props.channel && e.instance.props.channel.id);
2020-04-09 09:29:38 +02:00
if (userType) {
2020-06-16 17:07:08 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.userpopoutheadertagwithnickname]]});
2020-04-09 09:29:38 +02:00
if (index > -1) this.injectOwnerTag(children, e.instance.props.user, userType, 2, {
2020-07-15 17:06:13 +02:00
tagClass: BDFDB.disCNS.userpopoutheaderbottagwithnickname + BDFDB.disCN.bottagnametag,
inverted: typeof e.instance.getMode == "function" && e.instance.getMode() !== "Normal"
2020-04-09 09:29:38 +02:00
});
2020-02-04 08:20:40 +01:00
}
}
2019-01-09 12:56:24 +01:00
}
2019-01-26 22:45:19 +01:00
2020-04-09 09:29:38 +02:00
injectOwnerTag (children, user, userType, insertIndex, config = {}) {
2020-02-04 08:20:40 +01:00
if (!BDFDB.ArrayUtils.is(children) || !user) return;
if (settings.useCrown || settings.hideNativeCrown) {
2020-06-16 17:07:08 +02:00
let [_, index] = BDFDB.ReactUtils.findParent(children, {props: [["text",[BDFDB.LanguageUtils.LanguageStrings.GROUP_OWNER, BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER]]]});
2020-02-04 08:20:40 +01:00
if (index > -1) children[index] = null;
}
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(BDFDB.LibraryModules.LastChannelStore.getChannelId());
let member = settings.useRoleColor ? (BDFDB.LibraryModules.MemberStore.getMember(channel.guild_id, user.id) || {}) : {};
2020-04-09 09:29:38 +02:00
let isOwner = userType == userTypes.OWNER;
2020-02-04 08:20:40 +01:00
let tag = null;
if (settings.useCrown) {
2020-04-09 09:29:38 +02:00
let label = isOwner ? (channel.type == BDFDB.DiscordConstants.ChannelTypes.GROUP_DM ? BDFDB.LanguageUtils.LanguageStrings.GROUP_OWNER : BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER) : BDFDB.LanguageUtils.LanguageStrings.ADMINISTRATOR;
2020-02-04 08:20:40 +01:00
tag = 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
})
});
}
else {
2020-04-09 09:29:38 +02: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;
2020-02-04 08:20:40 +01:00
tag = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BotTag, {
2020-04-09 09:29:38 +02:00
className: config.tagClass,
useRemSizes: config.useRem,
invertColor: config.inverted,
2020-02-04 08:20:40 +01:00
style: {
2020-04-09 09:29:38 +02:00
backgroundColor: config.inverted ? (isBright && settings.useBlackFont ? "black" : null) : tagColor,
color: !config.inverted ? (isBright && settings.useBlackFont ? "black" : null) : tagColor
},
2020-06-09 10:11:25 +02:00
tag: inputs[isOwner ? "ownTagName" : "ownAdminTagName"]
2020-02-04 08:20:40 +01:00
});
}
2020-04-09 09:29:38 +02:00
children.splice(insertIndex, 0, tag);
2019-01-28 13:42:22 +01:00
}
2020-02-04 08:20:40 +01:00
2020-08-11 00:09:19 +02:00
getUserType (user, channelId) {
2020-04-09 09:29:38 +02:00
if (!user) return userTypes.NONE;
2020-08-11 00:09:19 +02:00
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(channelId || BDFDB.LibraryModules.LastChannelStore.getChannelId());
2020-04-09 09:29:38 +02:00
if (!channel) return userTypes.NONE;
2020-02-04 08:20:40 +01:00
let guild = BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id);
2020-04-09 09:29:38 +02:00
let isOwner = channel.ownerId == user.id || guild && guild.ownerId == user.id;
2020-05-25 17:16:19 +02:00
if (!(isOwner || (settings.addForAdmins && BDFDB.UserUtils.can("ADMINISTRATOR", user.id) && !(settings.ignoreBotAdmins && user.bot)))) return userTypes.NONE;
2020-04-09 09:29:38 +02:00
return isOwner ? userTypes.OWNER : userTypes.ADMIN;
2019-01-28 13:42:22 +01:00
}
2019-11-05 11:33:13 +01:00
2020-02-04 08:20:40 +01:00
forceUpdateAll () {
2020-05-25 17:16:19 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
2020-06-09 10:11:25 +02:00
inputs = BDFDB.DataUtils.get(this, "inputs");
2020-05-25 17:16:19 +02:00
2020-02-04 08:20:40 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
}
2019-05-13 22:31:55 +02:00
}
2020-07-26 16:39:51 +02:00
})();
module.exports = OwnerTag;