BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

246 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"TopRoleEverywhere","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/TopRoleEverywhere","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
var TopRoleEverywhere = (_ => {
2020-06-06 10:49:00 +02:00
var settings = {};
2020-02-04 08:20:40 +01:00
return class TopRoleEverywhere {
getName () {return "TopRoleEverywhere";}
2020-06-06 10:49:00 +02:00
getVersion () {return "3.0.3";}
2020-02-04 08:20:40 +01:00
getAuthor () {return "DevilBro";}
2020-02-04 08:20:40 +01:00
getDescription () {return "Adds the highest role of a user as a tag.";}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor () {
this.changelog = {
2020-06-06 10:49:00 +02:00
"improved":[["Chat Role Tag & Id Tag","The tags for the role and id in the message history now use their own settings separately, so you can disable the role tag in the chat but still show the id tag"]]
2020-02-04 08:20:40 +01:00
};
2020-04-09 09:29:38 +02:00
2020-02-04 08:20:40 +01:00
this.patchedModules = {
after: {
MemberListItem: "render",
MessageHeader: "default"
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
initConstructor () {
this.css = `
${BDFDB.dotCNS.member + BDFDB.dotCN.namecontainercontent} {
overflow: visible;
}
${BDFDB.dotCN._toproleseverywheretag} {
cursor: pointer;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
${BDFDB.dotCN._toproleseverywheremembertag} {
max-width: 50%;
}
${BDFDB.dotCN._toproleseverywherebadgestyle} {
margin-left: 0.3rem;
}
2020-03-25 17:39:00 +01:00
${BDFDB.dotCNS.themelight + BDFDB.dotCN._toproleseverywhererolestyle} {
color: rgba(79, 84, 92, 0.8);
}
${BDFDB.dotCNS.themedark + BDFDB.dotCN._toproleseverywhererolestyle} {
color: hsla(0, 0%, 100%, 0.8);
}
${BDFDB.dotCNS.messagecompact + BDFDB.dotCNS.messageheader + BDFDB.dotCN._toproleseverywherebadgestyle} {
2020-02-04 08:20:40 +01:00
margin-left: 0.1rem;
margin-right: 0.2rem;
}
2020-03-25 17:39:00 +01:00
${BDFDB.dotCNS.messagecompact + BDFDB.dotCNS.messageheader + BDFDB.dotCN._toproleseverywhererolestyle} {
margin-right: 0.3rem;
}
2020-02-04 08:20:40 +01:00
${BDFDB.dotCN._toproleseverywhererolestyle} {
display: inline-flex;
margin: 0 0 0 0.3rem;
}
${BDFDB.dotCNS._toproleseverywhererolestyle + BDFDB.dotCN.userpopoutrolecircle} {
flex: 0 0 auto;
}
2020-03-25 17:39:00 +01:00
${BDFDB.dotCNS.messagecompact + BDFDB.dotCNS.messageheader + BDFDB.dotCN._toproleseverywhererolestyle} {
2020-02-04 08:20:40 +01:00
text-indent: 0;
}`;
this.defaults = {
settings: {
showInChat: {value:true, inner:true, description:"Chat Window"},
showInMemberList: {value:true, inner:true, description:"Member List"},
useOtherStyle: {value:false, inner:false, description:"Use BotTag Style instead of the Role Style."},
useBlackFont: {value:false, inner:false, description:"Instead of darkening the color for BotTag Style on bright colors use black font."},
includeColorless: {value:false, inner:false, description:"Include colorless roles."},
showOwnerRole: {value:false, inner:false, description:`Display Role Tag of Serverowner as "${BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER}".`},
disableForBots: {value:false, inner:false, description:"Disable Role Tag for Bots."},
addUserID: {value:false, inner:false, description:"Add the UserID as a Tag to the Chat Window."}
}
};
}
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");
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 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 Role Tags 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
}
2018-10-11 10:21:26 +02: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-05-26 13:55:26 +02:00
}
2018-10-11 10:21:26 +02: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!");
2018-10-11 10:21:26 +02: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();
BDFDB.PluginUtils.clear(this);
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
processMemberListItem (e) {
2020-06-06 10:49:00 +02:00
if (e.instance.props.user && settings.showInMemberList) {
2020-04-09 09:29:38 +02:00
this.injectRoleTag(BDFDB.ReactUtils.getValue(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, "member", {
tagClass: BDFDB.disCN.bottagmember
});
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
processMessageHeader (e) {
2020-06-06 10:49:00 +02:00
if (e.instance.props.message) {
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-06-06 10:49:00 +02:00
if (index > -1) {
if (settings.showInChat) this.injectRoleTag(children, e.instance.props.message.author, "chat", {
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
if (settings.addUserID) this.injectIdTag(children, e.instance.props.message.author, "chat", {
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
}
2020-02-04 08:20:40 +01:00
}
}
2019-01-26 22:45:19 +01:00
2020-04-09 09:29:38 +02:00
injectRoleTag (children, user, type, config = {}) {
2020-02-04 08:20:40 +01:00
if (!BDFDB.ArrayUtils.is(children) || !user) return;
let guild = BDFDB.LibraryModules.GuildStore.getGuild(BDFDB.LibraryModules.LastGuildStore.getGuildId());
if (!guild || user.bot && settings.disableForBots) return;
let role = BDFDB.LibraryModules.PermissionRoleUtils.getHighestRole(guild, user.id);
if (role && !role.colorString && !settings.includeColorless) {
let member = BDFDB.LibraryModules.MemberStore.getMember(guild.id, user.id);
if (member) for (let sortedRole of BDFDB.ArrayUtils.keySort(member.roles.map(roleId => guild.getRole(roleId)), "position").reverse()) if (sortedRole.colorString) {
role = sortedRole;
break;
}
2019-11-28 12:26:23 +01:00
}
2020-06-06 10:49:00 +02:00
if (role && (role.colorString || settings.includeColorless)) children.push(this.createTag(Object.assign({}, role, {
2020-02-04 08:20:40 +01:00
name: settings.showOwnerRole && user.id == guild.ownerId ? BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER : role.name
2020-04-09 09:29:38 +02:00
}), type, config));
2020-06-06 10:49:00 +02:00
}
injectIdTag (children, user, type, config = {}) {
if (!BDFDB.ArrayUtils.is(children) || !user) return;
children.push(this.createTag({
2020-02-04 08:20:40 +01:00
name: user.id
2020-04-09 09:29:38 +02:00
}, type, config));
2019-11-28 12:26:23 +01:00
}
2020-02-04 08:20:40 +01:00
2020-06-06 10:49:00 +02:00
createTag (role, type, config = {}) {
2020-02-04 08:20:40 +01:00
if (settings.useOtherStyle) {
let tagColor = BDFDB.ColorUtils.convert(role.colorString || BDFDB.DiscordConstants.Colors.PRIMARY_DARK_500, "RGB")
let isBright = role.colorString && BDFDB.ColorUtils.isBright(tagColor);
tagColor = isBright ? (settings.useBlackFont ? tagColor : BDFDB.ColorUtils.change(tagColor, -0.3)) : tagColor;
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BotTag, {
2020-04-09 09:29:38 +02:00
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._toproleseverywheretag, BDFDB.disCN[`_toproleseverywhere${type}tag`], BDFDB.disCN._toproleseverywherebadgestyle, config.tagClass),
useRemSizes: config.useRem,
invertColor: config.inverted,
2020-02-04 08:20:40 +01:00
style: {
backgroundColor: tagColor,
color: isBright && settings.useBlackFont ? "black" : null
},
2020-04-09 09:29:38 +02:00
tag: role.name,
2020-02-04 08:20:40 +01:00
onContextMenu: role.id ? e => {this.openRoleContextMenu(e, role);} : null
});
}
else return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.MemberRole, {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._toproleseverywheretag, BDFDB.disCN[`_toproleseverywhere${type}tag`], BDFDB.disCN._toproleseverywhererolestyle),
role: role,
2019-11-11 09:52:51 +01:00
onContextMenu: role.id ? e => {this.openRoleContextMenu(e, role);} : null
});
}
2020-02-04 08:20:40 +01:00
openRoleContextMenu (e, role) {
BDFDB.LibraryModules.ContextMenuUtils.openContextMenu(e, function (e) {
2020-05-20 10:10:54 +02:00
return BDFDB.ReactUtils.createElement(BDFDB.ModuleUtils.findByName("DeveloperContextMenu"), Object.assign({}, e, {id: role.id}));
2020-02-04 08:20:40 +01:00
});
}
2019-11-11 09:45:40 +01:00
2020-02-04 08:20:40 +01:00
forceUpdateAll () {
2020-06-06 10:49:00 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
2020-02-04 08:20:40 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
}
2018-10-11 10:21:26 +02:00
}
2020-02-04 08:20:40 +01:00
})();