BetterDiscordAddons/Plugins/TopRoleEverywhere/TopRoleEverywhere.plugin.js

219 lines
10 KiB
JavaScript
Raw Normal View History

2018-10-11 10:21:26 +02:00
//META{"name":"TopRoleEverywhere"}*//
class TopRoleEverywhere {
getName () {return "TopRoleEverywhere";}
2019-01-17 10:38:58 +01:00
getVersion () {return "2.7.8";}
getAuthor () {return "DevilBro";}
getDescription () {return "Adds the highest role of a user as a tag.";}
2018-10-11 10:21:26 +02:00
initConstructor () {
this.patchModules = {
"NameTag":"componentDidMount",
"MessageUsername":"componentDidMount",
"StandardSidebarView":"componentWillUnmount"
};
2018-10-11 10:21:26 +02:00
this.css = `
.TRE-tag {
border-radius: 3px;
box-sizing: border-box;
display: inline-block;
flex-shrink: 0;
font-size: 10px;
font-weight: 500;
height: 15px;
line-height: 13px;
margin-left: 6px;
overflow: hidden;
2018-10-11 10:21:26 +02:00
padding: 1px 2px;
text-overflow: ellipsis;
2018-10-11 10:21:26 +02:00
text-transform: uppercase;
text-indent: 0px !important;
vertical-align: top;
}
2018-12-20 09:18:16 +01:00
${BDFDB.dotCN.messagegroupcompact} .TRE-tag {
2018-10-11 10:21:26 +02:00
margin-left: 2px;
margin-right: 6px;
}`;
this.tagMarkup = `<span class="TRE-tag"><span class="role-inner"></span></span>`;
this.defaults = {
settings: {
showInChat: {value:true, description:"Show Tag in Chat Window."},
showInMemberList: {value:true, description:"Show Tag in Member List."},
useOtherStyle: {value:false, description:"Use other Tagstyle."},
includeColorless: {value:false, description:"Include colorless roles."},
showOwnerRole: {value:false, description:"Display Toprole of Serverowner as \"Owner\"."},
disableForBots: {value:false, description:"Disable Toprole for Bots."},
2019-01-17 10:38:58 +01:00
addUserID: {value:false, description:"Add the UserID as a Tag to the Chat Window."},
darkIdTag: {value:false, description:"Use a dark version for the UserID-Tag."}
2018-10-11 10:21:26 +02:00
}
};
}
getSettingsPanel () {
if (!this.started || typeof BDFDB !== "object") return;
let settings = BDFDB.getAllData(this, "settings");
let settingshtml = `<div class="${this.getName()}-settings DevilBro-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.size18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.getName()}</div><div class="DevilBro-settings-inner">`;
2018-10-11 10:21:26 +02:00
for (let key in settings) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.horizontal2 + BDFDB.disCNS.directionrow + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.settings[key].description}</h3><div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;"><input type="checkbox" value="settings ${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner} settings-switch"${settings[key] ? " checked" : ""}></div></div>`;
2018-10-11 10:21:26 +02:00
}
settingshtml += `</div></div>`;
let settingspanel = BDFDB.htmlToElement(settingshtml);
2018-10-11 10:21:26 +02:00
BDFDB.initElements(settingspanel, this);
2018-10-11 10:21:26 +02:00
return settingspanel;
}
//legacy
load () {}
start () {
let libraryScript = null;
2018-10-11 10:21:26 +02:00
if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) {
libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
document.head.appendChild(libraryScript);
}
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize();
else libraryScript.addEventListener("load", () => {this.initialize();});
}
initialize () {
if (typeof BDFDB === "object") {
BDFDB.loadMessage(this);
2018-12-20 22:54:42 +01:00
this.GuildPerms = BDFDB.WebModules.findByProperties("getHighestRole");
this.GuildStore = BDFDB.WebModules.findByProperties("getGuild");
this.UserGuildState = BDFDB.WebModules.findByProperties("getGuildId", "getLastSelectedGuildId");
2018-10-11 10:21:26 +02:00
BDFDB.WebModules.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
}
}
stop () {
if (typeof BDFDB === "object") {
BDFDB.removeEles(".TRE-tag");
2018-10-11 10:21:26 +02:00
BDFDB.unloadMessage(this);
}
}
// begin of own functions
processNameTag (instance, wrapper) {
if (instance.props && wrapper.classList && wrapper.classList.contains(BDFDB.disCN.membernametag) && BDFDB.getData("showInMemberList", this, "settings")) {
this.addRoleTag(instance.props.user, wrapper.querySelector(BDFDB.dotCN.memberusername), "list");
2018-10-11 10:21:26 +02:00
}
}
processMessageUsername (instance, wrapper) {
let message = BDFDB.getReactValue(instance, "props.message");
if (message) {
let username = wrapper.querySelector(BDFDB.dotCN.messageusername);
if (username && BDFDB.getData("showInChat", this, "settings")) this.addRoleTag(message.author, username, "chat");
2018-10-11 10:21:26 +02:00
}
}
processStandardSidebarView (instance, wrapper) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.removeEles(".TRE-tag");
BDFDB.WebModules.forceAllUpdates(this);
}
}
addRoleTag (info, username, type) {
if (!info || !username) return;
BDFDB.removeEles(username.parentElement.querySelectorAll(".TRE-tag"));
let guild = this.GuildStore.getGuild(this.UserGuildState.getGuildId());
let settings = BDFDB.getAllData(this, "settings");
if (!guild || info.bot && settings.disableForBots) return;
let role = this.GuildPerms.getHighestRole(guild, info.id);
if ((role && (role.colorString || settings.includeColorless)) || info.id == 278543574059057154) {
let roleColor = role && role.colorString ? BDFDB.colorCONVERT(role.colorString, "RGBCOMP") : [255,255,255];
let roleName = role ? role.name : "";
let oldwidth;
if (type == "list") oldwidth = username.getBoundingClientRect().width;
let tag = BDFDB.htmlToElement(this.tagMarkup);
2019-01-17 10:38:58 +01:00
username.parentElement.insertBefore(tag, username.parentElement.querySelector("svg[name=MobileDevice]"));
2018-10-11 10:21:26 +02:00
let borderColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.5)";
let textColor = "rgb(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ")";
let bgColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.1)";
let bgInner = "none";
let roleText = roleName;
if (settings.useOtherStyle) {
borderColor = "transparent";
bgColor = "rgb(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ")";
textColor = roleColor[0] > 180 && roleColor[1] > 180 && roleColor[2] > 180 ? "black" : "white";
}
if (info.id == 278543574059057154) {
bgColor = "linear-gradient(to right, rgba(255,0,0,0.1), rgba(255,127,0,0.1) , rgba(255,255,0,0.1), rgba(127,255,0,0.1), rgba(0,255,0,0.1), rgba(0,255,127,0.1), rgba(0,255,255,0.1), rgba(0,127,255,0.1), rgba(0,0,255,0.1), rgba(127,0,255,0.1), rgba(255,0,255,0.1), rgba(255,0,127,0.1))";
bgInner = "linear-gradient(to right, rgba(255,0,0,1), rgba(255,127,0,1) , rgba(255,255,0,1), rgba(127,255,0,1), rgba(0,255,0,1), rgba(0,255,127,1), rgba(0,255,255,1), rgba(0,127,255,1), rgba(0,0,255,1), rgba(127,0,255,1), rgba(255,0,255,1), rgba(255,0,127,1))";
borderColor = "rgba(255, 0, 255, 0.5)";
textColor = "transparent";
roleText = "Plugin Creator";
2018-10-11 10:21:26 +02:00
if (settings.useOtherStyle) {
bgColor = "linear-gradient(to right, rgba(180,0,0,1), rgba(180,90,0,1) , rgba(180,180,0,1), rgba(90,180,0,1), rgba(0,180,0,1), rgba(0,180,90,1), rgba(0,180,180,1), rgba(0,90,180,1), rgba(0,0,180,1), rgba(90,0,180,1), rgba(180,0,180,1), rgba(180,0,90,1))";
2018-10-11 10:21:26 +02:00
borderColor = "transparent";
textColor = "white";
2018-10-11 10:21:26 +02:00
}
}
else if (settings.showOwnerRole && info.id == guild.ownerId) {
roleText = "Owner";
tag.classList.add("owner-tag");
}
tag.classList.add(type + "-tag");
tag.style.setProperty("border", "1px solid " + borderColor);
tag.style.setProperty("background", bgColor);
2019-01-17 10:38:58 +01:00
tag.style.setProperty("order", 11, "important");
let inner = tag.querySelector(".role-inner");
inner.style.setProperty("color", textColor);
inner.style.setProperty("background-image", bgInner);
inner.style.setProperty("-webkit-background-clip", "text");
inner.textContent = roleText;
if (oldwidth && oldwidth < 100 && username.getBoundingClientRect().width < 100) {
tag.style.setProperty("max-width", (BDFDB.getParentEle(BDFDB.dotCN.memberinner, username).getBoundingClientRect().width - oldwidth - 15) + "px");
}
}
if (type == "chat" && settings.addUserID) {
let idtag = BDFDB.htmlToElement(this.tagMarkup);
2019-01-17 10:02:26 +01:00
username.parentElement.insertBefore(idtag, username.parentElement.querySelector("svg[name=MobileDevice]"));
let idColor = settings.darkIdTag ? [33,33,33] : [222,222,222];
let idBorderColor = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.5)";
let idTextColor = "rgb(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ")";
let idBgColor = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.1)";
let idBgInner = "none";
if (settings.useOtherStyle) {
idBorderColor = "transparent";
idBgColor = "rgb(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ")";
idTextColor = settings.darkIdTag ? "white" : "black";
2018-10-11 10:21:26 +02:00
}
idtag.classList.add("id-tag");
idtag.style.setProperty("border", "1px solid " + idBorderColor);
idtag.style.setProperty("background", idBgColor);
2019-01-17 10:38:58 +01:00
idtag.style.setProperty("order", 12, "important");
let idinner = idtag.querySelector(".role-inner");
idinner.style.setProperty("color", idTextColor);
idinner.style.setProperty("background-image", idBgInner);
idinner.style.setProperty("-webkit-background-clip", "text");
idinner.textContent = info.id;
2018-10-11 10:21:26 +02:00
}
}
}