BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

206 lines
7.8 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";}
2019-11-17 12:21:34 +01:00
getVersion () {return "2.9.2";}
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 = {
2019-11-17 12:21:34 +01:00
"fixed":[["Chat","Elements now properly get added to the chat again"]],
2019-11-11 09:45:40 +01:00
"improved":[["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;
}
.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 () {
2019-01-22 11:28:32 +01:00
if (!global.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-11-11 09:45:40 +01:00
let settingsitems = [], inneritems = [];
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
}));
return BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2018-10-11 10:21:26 +02: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) {
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());
2019-05-26 13:55:26 +02: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
}
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);
2018-10-11 10:21:26 +02:00
}
initialize () {
2019-01-22 11:05:54 +01:00
if (global.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 () {
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-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;
e.returnvalue.props.children = () => {
let renderedChildren = renderChildren(e.instance);
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);
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
}
}