BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

267 lines
12 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name TopRoleEverywhere
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2021-06-25 16:15:38 +02:00
* @version 3.0.7
2021-03-05 13:26:41 +01:00
* @description Adds the highest Role of a User as a Tag
2020-10-20 23:25:34 +02:00
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @website https://mwittrien.github.io/
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/TopRoleEverywhere/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/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",
2021-06-25 16:15:38 +02:00
"version": "3.0.7",
2021-03-05 11:21:21 +01:00
"description": "Adds the highest Role of a User as a Tag"
2020-10-07 10:20:55 +02:00
},
"changeLog": {
2021-06-11 22:06:02 +02:00
"fixed": {
2021-06-25 16:15:38 +02:00
"Role/ID Swap": "Added Option to swap userId/role"
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
2021-06-15 13:42:02 +02:00
return (window.Lightcord || window.LightCord) ? class {
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
getDescription () {return "Do not use LightCord!";}
load () {BdApi.alert("Attention!", "By using LightCord you are risking your Discord Account, due to using a 3rd Party Client. Switch to an official Discord Client (https://discord.com/) with the proper BD Injection (https://betterdiscord.app/)");}
start() {}
stop() {}
} : !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) => {
2021-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-03-06 14:59:48 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library");
2021-02-01 17:13:13 +01:00
});
}
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]) => {
return class TopRoleEverywhere extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
2021-06-11 22:06:02 +02:00
general: {
2021-06-25 16:15:38 +02:00
useOtherStyle: {value: false, description: "Uses BotTag Style instead of the Role Style"},
useBlackFont: {value: false, description: "Uses black Font instead of darkening the Color for BotTag Style on bright Colors"},
includeColorless: {value: false, description: "Includes colorless Roles"},
showOwnerRole: {value: false, description: `Displays Role Tag of Server Owner as "${BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER}".`},
disableForBots: {value: false, description: "Disables Role Tag for Bots"},
addUserId: {value: false, description: "Adds the User Id as a Tag to the Chat Window"},
userIdFirst: {value: false, description: "Places the User Id before the Role Tag"}
2021-06-11 22:06:02 +02:00
},
places: {
2021-06-25 16:15:38 +02:00
chat: {value: true, description: "Chat Window"},
memberList: {value: true, description: "Member List"},
voiceList: {value: true, description: "Voice User List"},
2020-09-19 20:49:33 +02:00
}
};
this.patchedModules = {
after: {
MemberListItem: "render",
2021-03-03 14:35:05 +01:00
MessageUsername: "default",
VoiceUser: "render"
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} {
2021-03-03 14:35:05 +01:00
display: inline-flex;
flex: 0 1 auto;
2020-09-19 20:49:33 +02:00
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;
2021-06-11 22:06:02 +02:00
margin-left: 0;
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
2021-06-11 22:06:02 +02:00
for (let key in this.defaults.general) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-09-19 20:49:33 +02:00
type: "Switch",
plugin: this,
2021-06-11 22:06:02 +02:00
keys: ["general", key],
label: this.defaults.general[key].description,
value: this.settings.general[key]
2020-09-19 20:49:33 +02:00
}));
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:",
2021-06-11 22:06:02 +02:00
children: Object.keys(this.defaults.places).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-09-19 20:49:33 +02:00
type: "Switch",
plugin: this,
2021-06-11 22:06:02 +02:00
keys: ["places", key],
label: this.defaults.places[key].description,
value: this.settings.places[key]
2020-09-19 20:49:33 +02:00
}))
}));
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
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) {
2021-06-11 22:06:02 +02:00
if (e.instance.props.user && this.settings.places.memberList) this.injectRoleTag(BDFDB.ObjectUtils.get(e.returnvalue, "props.decorators.props.children"), e.instance.props.user, "member", 2, {
tagClass: BDFDB.disCN.bottagmember
});
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) {
2021-06-11 22:06:02 +02:00
if (!e.instance.props.message) return;
const author = e.instance.props.userOverride || e.instance.props.message.author;
if (this.settings.places.chat) this.injectRoleTag(e.returnvalue.props.children, author, "chat", e.instance.props.compact ? 0 : 2, {
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
2021-06-25 16:15:38 +02:00
if (this.settings.general.addUserId) this.injectIdTag(e.returnvalue.props.children, author, "chat", (e.instance.props.compact ? 0 : 2) + (this.settings.general.userIdFirst ? 0 : 1), {
2021-06-11 22:06:02 +02:00
tagClass: e.instance.props.compact ? BDFDB.disCN.messagebottagcompact : BDFDB.disCN.messagebottagcozy,
useRem: true
});
2019-11-28 12:26:23 +01:00
}
2020-06-06 10:49:00 +02:00
2021-03-03 14:35:05 +01:00
processVoiceUser (e) {
2021-06-11 22:06:02 +02:00
if (e.instance.props.user && this.settings.places.voiceList) {
2021-03-03 14:35:05 +01:00
let content = BDFDB.ReactUtils.findChild(e.returnvalue, {props: [["className", BDFDB.disCN.voicecontent]]});
if (content) this.injectRoleTag(content.props.children, e.instance.props.user, "voice", 3);
}
}
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());
2021-06-11 22:06:02 +02:00
if (!guild || user.bot && this.settings.general.disableForBots) return;
2020-09-19 20:49:33 +02:00
let role = BDFDB.LibraryModules.PermissionRoleUtils.getHighestRole(guild, user.id);
2021-06-11 22:06:02 +02:00
if (this.settings.general.showOwnerRole && user.id == guild.ownerId) role = Object.assign({}, role, {name: BDFDB.LanguageUtils.LanguageStrings.GUILD_OWNER, ownerRole: true});
if (role && !role.colorString && !this.settings.general.includeColorless && !role.ownerRole) {
2020-09-19 20:49:33 +02:00
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;
}
}
2021-06-11 22:06:02 +02:00
if (role && (role.colorString || role.ownerRole || this.settings.general.includeColorless)) children.splice(insertIndex, 0, this.createTag(role, type, config));
2020-09-19 20:49:33 +02:00
}
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 = {}) {
2021-06-11 22:06:02 +02:00
if (this.settings.general.useOtherStyle) {
2020-09-19 20:49:33 +02:00
let tagColor = BDFDB.ColorUtils.convert(role.colorString || BDFDB.DiscordConstants.Colors.PRIMARY_DARK_500, "RGB")
let isBright = role.colorString && BDFDB.ColorUtils.isBright(tagColor);
2021-06-11 22:06:02 +02:00
tagColor = isBright ? (this.settings.general.useBlackFont ? tagColor : BDFDB.ColorUtils.change(tagColor, -0.3)) : tagColor;
2020-09-19 20:49:33 +02:00
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,
2021-06-11 22:06:02 +02:00
color: isBright && this.settings.general.useBlackFont ? "black" : null
2020-09-19 20:49:33 +02:00
},
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
})();