BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

216 lines
8.5 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"TopRoleEverywhere","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
class TopRoleEverywhere {
getName () {return "TopRoleEverywhere";}
2020-01-09 15:50:58 +01:00
getVersion () {return "2.9.5";}
getAuthor () {return "DevilBro";}
getDescription () {return "Adds the highest role of a user as a tag.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-07-18 12:18:54 +02:00
this.changelog = {
2020-01-09 15:50:58 +01:00
"fixed":[["Compact", "Fixed roles in role style for compact mode"]],
2019-11-28 12:26:23 +01:00
"improved":[["Coloress", "If you disabled the plugin for colorless roles and the highest role of a user is a role without color, then the plugin will try to find the next highest role with a color"], ["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-07-18 12:18:54 +02: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-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.css = `
2019-09-12 22:57:39 +02:00
${BDFDB.dotCNS.member + BDFDB.dotCN.namecontainercontent} {
overflow: visible;
}
2019-08-07 09:24:44 +02:00
.TRE-tag {
2019-07-27 23:14:00 +02:00
overflow: hidden;
text-overflow: ellipsis;
2019-11-11 09:45:40 +01:00
white-space: nowrap;
}
.TRE-tag.TRE-roletag {
margin: 0 0 0 0.3rem;
}
2020-01-09 15:50:58 +01:00
${BDFDB.dotCN.messageheadercompact} .TRE-roletag {
text-indent: 0;
}
2019-11-11 09:45:40 +01:00
.TRE-tag.TRE-bottag {
margin-left: 0.3rem;
}
.TRE-tag.TRE-listtag {
max-width: 50%;
}
.TRE-roletag {
display: inline-flex;
}
.TRE-roletag ${BDFDB.dotCN.userpopoutrolecircle} {
flex: 0 0 auto;
2018-10-11 10:21:26 +02:00
}`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.defaults = {
settings: {
2019-11-11 09:45:40 +01:00
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."},
2019-11-11 09:52:51 +01:00
useBlackFont: {value:false, inner:false, description:"Instead of darkening the color for BotTag Style on bright colors use black font."},
2019-11-11 09:45:40 +01:00
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."},
2019-11-11 09:52:51 +01:00
addUserID: {value:false, inner:false, description:"Add the UserID as a Tag to the Chat Window."}
2018-10-11 10:21:26 +02:00
}
};
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getSettingsPanel () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-12-18 16:45:08 +01:00
let settingspanel, settingsitems = [], inneritems = [];
2019-11-11 09:45:40 +01:00
for (let key in settings) (!this.defaults.settings[key].inner ? settingsitems : inneritems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelInner, {
title: "Add Role Tags in:",
first: settingsitems.length == 0,
last: true,
children: inneritems
}));
2019-12-18 16:45:08 +01:00
return settingspanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2018-10-11 10:21:26 +02: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) {
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
}
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);
2018-10-11 10:21:26 +02:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.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-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
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 () {
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-11 09:45:40 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
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-11-11 09:45:40 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-11-11 09:45:40 +01:00
processMemberListItem (e) {
if (e.instance.props.user && BDFDB.DataUtils.get(this, "settings", "showInMemberList")) {
this.injectRoleTag(BDFDB.ReactUtils.getValue(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, "list", BDFDB.disCN.bottagmember);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-11-11 09:45:40 +01:00
processMessageUsername (e) {
let user = BDFDB.ReactUtils.getValue(e.instance, "props.message.author");
2019-11-17 12:21:34 +01:00
if (user && typeof e.returnvalue.props.children == "function" && BDFDB.DataUtils.get(this, "settings", "showInChat")) {
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:22:46 +01:00
this.injectRoleTag(renderedChildren.props.children, user, "chat", e.instance.props.isCompact ? BDFDB.disCN.bottagmessagecompact : BDFDB.disCN.bottagmessagecozy);
2019-11-17 12:21:34 +01:00
return renderedChildren;
};
}
}
2019-01-26 22:45:19 +01:00
2019-11-11 09:45:40 +01:00
injectRoleTag (children, user, type, tagclass) {
if (!BDFDB.ArrayUtils.is(children) || !user) return;
2019-09-11 12:14:43 +02:00
let guild = BDFDB.LibraryModules.GuildStore.getGuild(BDFDB.LibraryModules.LastGuildStore.getGuildId());
2019-10-22 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-11-11 09:45:40 +01:00
if (!guild || user.bot && settings.disableForBots) return;
let role = BDFDB.LibraryModules.PermissionRoleUtils.getHighestRole(guild, user.id);
2019-11-28 12:26:23 +01:00
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-11 09:45:40 +01:00
if (role && (role.colorString || settings.includeColorless)) children.push(this.createRoleTag(settings, Object.assign({}, role, {
name: settings.showOwnerRole && user.id == guild.ownerId ? BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER : role.name
}),type, tagclass));
if (type == "chat" && settings.addUserID) children.push(this.createRoleTag(settings, {
name: user.id
}, type, tagclass));
}
createRoleTag (settings, role, type, tagclass) {
2019-11-11 09:52:51 +01:00
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;
if (settings.useOtherStyle) {
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.BotTag, {
className: `${tagclass} TRE-tag TRE-bottag TRE-${type}tag`,
tag: role.name,
style: {
backgroundColor: tagcolor,
color: isbright && settings.useBlackFont ? "black" : null
},
onContextMenu: role.id ? e => {this.openRoleContextMenu(e, role);} : null
});
}
else return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.MemberRole, {
2019-11-11 09:45:40 +01:00
className: `TRE-tag TRE-roletag TRE-${type}tag`,
role: role,
onContextMenu: role.id ? e => {this.openRoleContextMenu(e, role);} : null
});
}
openRoleContextMenu (e, role) {
BDFDB.LibraryModules.ContextMenuUtils.openContextMenu(e, function (e) {
return BDFDB.ReactUtils.createElement(BDFDB.ModuleUtils.findByName("GuildRoleContextMenu"), Object.assign({}, e, {
roleId: role.id
}));
});
2018-10-11 10:21:26 +02:00
}
}