BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

248 lines
10 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @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
2020-11-12 17:30:48 +01:00
* @updateUrl https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js
2020-10-20 23:25:34 +02:00
*/
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2020-10-09 21:09:35 +02:00
const config = {
2020-09-19 20:49:33 +02:00
"info": {
"name": "TopRoleEverywhere",
"author": "DevilBro",
2020-12-07 19:42:24 +01:00
"version": "3.0.5",
2020-10-16 10:25:30 +02:00
"description": "Add the highest role of a user as a tag"
2020-10-07 10:20:55 +02:00
},
"changeLog": {
"fixed": {
2020-12-07 19:42:24 +01:00
"Compact": "Fixed some issues with compact mode"
2020-10-07 10:20:55 +02:00
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
};
2020-12-07 19:42:24 +01:00
2020-10-09 21:09:35 +02:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2021-01-06 12:38:36 +01:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
2021-02-01 17:13:13 +01:00
getDescription () {return `The Library Plugin needed for ${config.info.name} is missing. Open the Plugin Settings to download it. \n\n${config.info.description}`;}
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => {});
else BdApi.alert("Error", "Could not download BDFDB Library Plugin, try again later or download it manually from GitHub: https://github.com/mwittrien/BetterDiscordAddons/tree/master/Library/");
});
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2021-01-14 16:14:44 +01:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
}
if (!window.BDFDB_Global.pluginQueue.includes(config.info.name)) window.BDFDB_Global.pluginQueue.push(config.info.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2021-01-14 16:14:44 +01:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${config.info.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
2020-09-19 20:49:33 +02:00
var settings = {};
2020-10-09 21:09:35 +02:00
return class TopRoleEverywhere extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
settings: {
2020-11-19 16:51:14 +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"},
2021-01-11 14:11:17 +01:00
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 Server Owner as "${BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER}".`},
2020-11-19 16:51:14 +01:00
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"}
2020-09-19 20:49:33 +02:00
}
};
this.patchedModules = {
after: {
MemberListItem: "render",
2020-10-07 10:20:55 +02:00
MessageUsername: "default"
2020-09-19 20:49:33 +02:00
}
};
2020-12-07 19:42:24 +01:00
this.patchPriority = 4;
2020-09-19 20:49:33 +02:00
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.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);
}
2020-12-07 19:55:57 +01:00
${BDFDB.dotCNS.messagerepliedmessage + BDFDB.dotCN._toproleseverywhererolestyle},
${BDFDB.dotCNS.messagecompact + BDFDB.dotCN._toproleseverywhererolestyle} {
2020-09-19 20:49:33 +02:00
margin-right: 0.3rem;
2020-10-07 10:20:55 +02:00
text-indent: 0;
2020-09-19 20:49:33 +02:00
}
${BDFDB.dotCN._toproleseverywhererolestyle} {
display: inline-flex;
margin: 0 0 0 0.3rem;
}
${BDFDB.dotCNS._toproleseverywhererolestyle + BDFDB.dotCN.userpopoutrolecircle} {
flex: 0 0 auto;
}
`;
}
2020-02-04 08:20:40 +01:00
2021-01-06 12:38:36 +01:00
onStart () {
2020-09-19 20:49:33 +02:00
this.forceUpdateAll();
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
let settingsPanel, settingsItems = [];
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
for (let key in settings) if (!this.defaults.settings[key].inner) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
2020-12-15 10:35:30 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
2020-09-19 20:49:33 +02:00
title: "Add Role Tags in:",
children: Object.keys(settings).map(key => this.defaults.settings[key].inner && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}))
}));
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
2020-02-04 08:20:40 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
forceUpdateAll () {
2020-09-19 20:49:33 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processMemberListItem (e) {
if (e.instance.props.user && settings.showInMemberList) {
2020-12-07 19:42:24 +01:00
this.injectRoleTag(BDFDB.ObjectUtils.get(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, "member", 2, {
2020-09-19 20:49:33 +02:00
tagClass: BDFDB.disCN.bottagmember
2020-06-06 10:49:00 +02:00
});
}
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-10-07 10:20:55 +02:00
processMessageUsername (e) {
2020-09-19 20:49:33 +02:00
if (e.instance.props.message) {
2020-12-07 19:42:24 +01:00
if (settings.showInChat) this.injectRoleTag(e.returnvalue.props.children, e.instance.props.message.author, "chat", e.instance.props.compact ? 0 : 2, {
2020-10-07 10:20:55 +02:00
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
2020-12-07 19:42:24 +01:00
if (settings.addUserID) this.injectIdTag(e.returnvalue.props.children, e.instance.props.message.author, "chat", e.instance.props.compact ? 0 : 2, {
2020-10-07 10:20:55 +02:00
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
2020-02-04 08:20:40 +01:00
}
2019-11-28 12:26:23 +01:00
}
2020-06-06 10:49:00 +02:00
2020-12-07 19:42:24 +01:00
injectRoleTag (children, user, type, insertIndex, config = {}) {
2020-09-19 20:49:33 +02: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;
}
}
2020-12-07 19:42:24 +01:00
if (role && (role.colorString || settings.includeColorless)) children.splice(insertIndex, 0, this.createTag(Object.assign({}, role, {
2020-09-19 20:49:33 +02:00
name: settings.showOwnerRole && user.id == guild.ownerId ? BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER : role.name
}), type, config));
}
2020-12-07 19:42:24 +01:00
injectIdTag (children, user, type, insertIndex, config = {}) {
2020-09-19 20:49:33 +02:00
if (!BDFDB.ArrayUtils.is(children) || !user) return;
2020-12-07 19:42:24 +01:00
children.splice(insertIndex, 0, this.createTag({
2020-09-19 20:49:33 +02:00
name: user.id
}, type, config));
}
createTag (role, type, config = {}) {
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, {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._toproleseverywheretag, BDFDB.disCN[`_toproleseverywhere${type}tag`], BDFDB.disCN._toproleseverywherebadgestyle, config.tagClass),
useRemSizes: config.useRem,
invertColor: config.inverted,
style: {
backgroundColor: tagColor,
color: isBright && settings.useBlackFont ? "black" : null
},
tag: role.name,
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,
2020-02-04 08:20:40 +01:00
onContextMenu: role.id ? e => {this.openRoleContextMenu(e, role);} : null
});
}
2020-06-06 10:49:00 +02:00
2020-09-19 20:49:33 +02:00
openRoleContextMenu (e, role) {
BDFDB.LibraryModules.ContextMenuUtils.openContextMenu(e, function (e) {
return BDFDB.ReactUtils.createElement(BDFDB.ModuleUtils.findByName("DeveloperContextMenu"), Object.assign({}, e, {id: role.id}));
});
}
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2020-09-19 20:49:33 +02:00
})();