module.exports = (Plugin, Api, Vendor) => { if (!global.BDFDB || typeof BDFDB != "object") global.BDFDB = {myPlugins:{}, BDv2Api: Api}; return class extends Plugin { initConstructor () { this.patchModules = { "Guilds":"componentDidMount", "DirectMessage":["componentDidMount","componentDidUpdate","componentWillUnmount"], "LazyScroller":"render" }; this.dmContextEntryMarkup = `
`; this.dmContextSubMenuMarkup = ` `; this.dmPinContextMarkup = ` `; this.dmUnpinContextMarkup = ` `; this.recentDMMarkup = ``; this.css = ` ${BDFDB.dotCN.guild}.pinned:after { background-position: 50%; background-repeat: no-repeat; background-size: 16px; border-radius: 12px; content: " "; height: 24px; overflow: hidden; pointer-events: none; position: absolute; right: -6px; top: -6px; width: 24px; background-image: url('data:image/svg+xml; utf8, '); } ${BDFDB.dotCNS.themelight + BDFDB.dotCN.guild}.pinned:after { background-color: #202225; } ${BDFDB.dotCNS.themedark + BDFDB.dotCN.guild}.pinned:after { background-color: #202225; }`; } onStart () { if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.name] = this; var libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]'); if (!global.BDFDB || typeof BDFDB != "object" || performance.now() - BDFDB.creationTime > 600000) { if (libraryScript) libraryScript.remove(); libraryScript = document.createElement("script"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"); libraryScript.setAttribute("date", performance.now()); libraryScript.addEventListener("load", () => {if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();}); document.head.appendChild(libraryScript); } else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize(); this.startTimeout = setTimeout(() => {this.initialize();}, 30000); } initialize () { if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { if (this.started) return true; BDFDB.loadMessage(this); this.UserUtils = BDFDB.WebModules.findByProperties("getUsers", "getUser"); this.ChannelUtils = BDFDB.WebModules.findByProperties("getChannels", "getChannel"); this.PrivateChannelUtils = BDFDB.WebModules.findByProperties("ensurePrivateChannel"); this.ChannelSwitchUtils = BDFDB.WebModules.findByProperties("selectPrivateChannel"); this.UnreadUtils = BDFDB.WebModules.findByProperties("getUnreadCount"); this.DiscordConstants = BDFDB.WebModules.findByProperties("Permissions", "ActivityTypes", "StatusTypes"); this.Animations = BDFDB.WebModules.findByProperties("spring"); BDFDB.addEventListener(this, document, "click", BDFDB.dotCNS.dmchannels + BDFDB.dotCN.dmchannel, e => { let instance = BDFDB.getReactInstance(e.currentTarget); if (BDFDB.getReactValue(instance, "return.return.return.memoizedProps.ispin")) { let dmsscroller = document.querySelector(BDFDB.dotCNS.dmchannels + BDFDB.dotCN.scroller); if (dmsscroller) { this.oldScrollerPos = dmsscroller.scrollTop; setTimeout(() => {this.oldScrollerPos = null;},1000); } } }); BDFDB.addEventListener(this, document, "click", BDFDB.dotCNS.dmchannels + BDFDB.dotCNS.dmchannel + BDFDB.dotCN.dmchannelclose, e => { let instance = BDFDB.getReactInstance(e.currentTarget); if (BDFDB.getReactValue(instance, "return.return.return.return.return.memoizedProps.ispin")) { e.originalEvent.stopPropagation(); e.originalEvent.preventDefault(); this.removePinnedDM(BDFDB.getReactValue(instance, "return.return.return.return.return.memoizedProps.channel.id")); } }); this.forceAdding = true; BDFDB.WebModules.forceAllUpdates(this); delete this.forceAdding; return true; } else { console.error(`%c[${this.name}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!'); return false; } } onStop () { if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { let dmsscrollerinstance = BDFDB.getReactInstance(document.querySelector(BDFDB.dotCNS.dmchannels + BDFDB.dotCN.scroller)); if (dmsscrollerinstance) { let dms = dmsscrollerinstance.return.return.return.memoizedProps.children; let amount = 0; let insertpoint = null; for (let i in dms) { let ele = dms[i]; if (ele && ele.pinned) { delete ele.pinned; if (ele.props.ispin) { if (ele.type == "header") insertpoint = i; amount++; } } } dms.splice(insertpoint, amount); this.forceUpdateScroller(dmsscrollerinstance.stateNode); } for (let info of BDFDB.readDmList()) { this.unhideNativeDM(info.id); if (info.div) info.div.removeEventListener("contextmenu", info.div.PinDMsContextMenuListener); } BDFDB.removeEles(BDFDB.dotCNS.dms + BDFDB.dotCN.guild + ".pinned"); BDFDB.unloadMessage(this); return true; } else { return false; } } // begin of own functions changeLanguageStrings () { this.dmContextEntryMarkup = this.dmContextEntryMarkup.replace("REPLACE_context_pindm_text", this.labels.context_pindm_text); this.dmContextSubMenuMarkup = this.dmContextSubMenuMarkup.replace("REPLACE_context_pinchannel_text", this.labels.context_pinchannel_text); this.dmContextSubMenuMarkup = this.dmContextSubMenuMarkup.replace("REPLACE_context_unpinchannel_text", this.labels.context_unpinchannel_text); this.dmContextSubMenuMarkup = this.dmContextSubMenuMarkup.replace("REPLACE_context_pinguild_text", this.labels.context_pinguild_text); this.dmContextSubMenuMarkup = this.dmContextSubMenuMarkup.replace("REPLACE_context_unpinguild_text", this.labels.context_unpinguild_text); this.dmPinContextMarkup = this.dmPinContextMarkup.replace("REPLACE_context_pinguild_text", this.labels.context_pinguild_text); this.dmUnpinContextMarkup = this.dmUnpinContextMarkup.replace("REPLACE_context_unpinguild_text", this.labels.context_unpinguild_text); } onUserContextMenu (instance, menu) { if (instance.props && instance.props.user && !menu.querySelector(".pindms-item")) { let closeentry = BDFDB.React.findDOMNodeSafe(BDFDB.getOwnerInstance({node:menu,props:["handleClose"]})); if (closeentry) { let id = this.ChannelUtils.getDMFromUserId(instance.props.user.id); if (id) this.appendItem(instance, id, closeentry); else this.PrivateChannelUtils.ensurePrivateChannel(BDFDB.myData.id, instance.props.user.id).then(id => {this.appendItem(instance, id, closeentry);}); } } } onGroupDMContextMenu (instance, menu) { if (instance.props && instance.props.channelId && !menu.querySelector(".pindms-item")) { let changeentry = BDFDB.React.findDOMNodeSafe(BDFDB.getOwnerInstance({node:menu,props:["handleChangeIcon"]})); if (changeentry) { this.appendItem(instance, instance.props.channelId, changeentry); } } } appendItem (instance, id, target) { let dmContextEntry = BDFDB.htmlToElement(this.dmContextEntryMarkup); target.parentElement.insertBefore(dmContextEntry, target); let pindmsitem = dmContextEntry.querySelector(".pindms-item"); pindmsitem.addEventListener("mouseenter", () => { let dmContextSubMenu = BDFDB.htmlToElement(this.dmContextSubMenuMarkup); let pinchannelitem = dmContextSubMenu.querySelector(".pindm-channel-item"); let unpinchannelitem = dmContextSubMenu.querySelector(".unpindm-channel-item"); let pinguilditem = dmContextSubMenu.querySelector(".pindm-guild-item"); let unpinguilditem = dmContextSubMenu.querySelector(".unpindm-guild-item"); let pinnedDMs = BDFDB.loadAllData(this, "pinnedDMs"); if (pinnedDMs[id] == undefined) { BDFDB.removeEles(unpinchannelitem); pinchannelitem.addEventListener("click", () => { instance._reactInternalFiber.return.memoizedProps.closeContextMenu(); let dmsscrollerinstance = BDFDB.getReactInstance(document.querySelector(BDFDB.dotCNS.dmchannels + BDFDB.dotCN.scroller)); if (dmsscrollerinstance) { let dms = dmsscrollerinstance.return.return.return.memoizedProps.children; let insertpoint = this.getInsertPoint(dms); this.addPinnedDM(id, dms, insertpoint); this.forceUpdateScroller(dmsscrollerinstance.stateNode); } pinnedDMs[id] = Object.keys(pinnedDMs).length; BDFDB.saveAllData(pinnedDMs, this, "pinnedDMs"); }); } else { BDFDB.removeEles(pinchannelitem); unpinchannelitem.addEventListener("click", () => { instance._reactInternalFiber.return.memoizedProps.closeContextMenu(); this.removePinnedDM(id); }); } let pinnedRecents = BDFDB.loadAllData(this, "pinnedRecents"); if (pinnedRecents[id] == undefined) { BDFDB.removeEles(unpinguilditem); pinguilditem.addEventListener("click", () => { instance._reactInternalFiber.return.memoizedProps.closeContextMenu(); this.addPinnedRecent(id); pinnedRecents[id] = Object.keys(pinnedRecents).length; BDFDB.saveAllData(pinnedRecents, this, "pinnedRecents"); }); } else { BDFDB.removeEles(pinguilditem); unpinguilditem.addEventListener("click", () => { instance._reactInternalFiber.return.memoizedProps.closeContextMenu(); BDFDB.removeEles(document.querySelector(`${BDFDB.dotCNS.dms + BDFDB.dotCN.guild}.pinned[channelid="${id}"]`)); this.unhideNativeDM(id); delete pinnedRecents[id]; BDFDB.saveAllData(pinnedRecents, this, "pinnedRecents"); }); } BDFDB.appendSubMenu(pindmsitem, dmContextSubMenu); }); } processGuilds (instance, wrapper) { let dms = wrapper.querySelector(BDFDB.dotCN.dms); if (dms) for (let id of this.sortAndUpdate("pinnedRecents")) this.addPinnedRecent(id); } processDirectMessage (instance, wrapper, methodnames) { if (instance.props && instance.props.channel) { if (methodnames.includes("componentDidMount")) { wrapper.removeEventListener("contextmenu", wrapper.PinDMsContextMenuListener); wrapper.PinDMsContextMenuListener = e => { let freshPinnedRecents = BDFDB.loadAllData(this, "pinnedRecents"); if (freshPinnedRecents[instance.props.channel.id] == undefined) { let dmContext = BDFDB.htmlToElement(this.dmPinContextMarkup); dmContext.querySelector(".pindm-guild-item").addEventListener("click", () => { BDFDB.removeEles(dmContext); this.addPinnedRecent(instance.props.channel.id); freshPinnedRecents[instance.props.channel.id] = Object.keys(freshPinnedRecents).length; BDFDB.saveAllData(freshPinnedRecents, this, "pinnedRecents"); }); BDFDB.appendContextMenu(dmContext, e); } }; wrapper.addEventListener("contextmenu", wrapper.PinDMsContextMenuListener); } let pinnedRecents = BDFDB.loadAllData(this, "pinnedRecents"); if (pinnedRecents[instance.props.channel.id] != undefined) { if (methodnames.includes("componentDidMount")) this.hideNativeDM(instance.props.channel.id); this.updateUnreadCount(instance.props.channel.id); } } } processLazyScroller (instance, wrapper) { let privateChannelIds = BDFDB.getReactValue(instance, "_reactInternalFiber.return.memoizedProps.privateChannelIds"); if (privateChannelIds) { if (this.forceAdding || !instance.props.PinDMsPatched) { instance.props.PinDMsPatched = true; let dms = instance.props.children; let sortedDMs = this.sortAndUpdate("pinnedDMs"); if (sortedDMs.length > 0) { let insertpoint = this.getInsertPoint(dms); for (let pos in sortedDMs) this.addPinnedDM(sortedDMs[pos], dms, insertpoint); } this.forceUpdateScroller(instance.getScrollerNode()); } if (this.oldScrollerPos != null) { instance.getScrollerNode().scrollTop = this.oldScrollerPos; } } } getInsertPoint (dms) { let insertpoint = null; for (let i in dms) { let ele = dms[i]; if (ele && ele.type == "header") { insertpoint = parseInt(i); if (!ele.pinned && !ele.props.ispin) { ele.pinned = true; let headerpin = Object.assign({},ele); headerpin.key = "pin" + headerpin.key; headerpin.props = {children:this.labels.header_pinneddms_text,ispin:true}; dms.splice(insertpoint, 0, headerpin); } insertpoint++; break; } } return insertpoint; } addPinnedDM (id, dms, insertpoint) { for (let ele of dms) if (ele && !ele.pinned && id == ele.key) { ele.pinned = true; let dmpin = Object.assign({ispin:true},ele); dmpin.key = "pin" + ele.key; dmpin.props = {channel:ele.props.channel,selected:ele.props.selected,ispin:true}; dms.splice(insertpoint, 0, dmpin); } } removePinnedDM (id) { if (!id) return; BDFDB.removeData(id, this, "pinnedDMs"); let dmsscrollerinstance = BDFDB.getReactInstance(document.querySelector(BDFDB.dotCNS.dmchannels + BDFDB.dotCN.scroller)); if (dmsscrollerinstance) { let dms = dmsscrollerinstance.return.return.return.memoizedProps.children; let existingDMs = this.sortAndUpdate("pinnedDMs"); let removepoint = null; for (let i in dms) { let ele = dms[i]; if (ele && ele.pinned && (id == ele.key || ("pin" + id) == ele.key)) { delete ele.pinned; if (ele.props.ispin) removepoint = parseInt(i); } } if (removepoint) { let offset = existingDMs.length ? 0 : 1; if (offset) delete dms[removepoint + offset].pinned; dms.splice(removepoint-offset,1+offset); } this.forceUpdateScroller(dmsscrollerinstance.stateNode); } } sortAndUpdate (type) { let pinnedDMs = BDFDB.loadAllData(this, type); delete pinnedDMs[""]; let sortedDMs = [], existingDMs = [], sortDM = (id) => { if (typeof sortedDMs[pinnedDMs[id]] == "undefined") sortedDMs[pinnedDMs[id]] = id; else sortDM(sortedDMs, pinnedDMs[id]+1, id); }; for (let id in pinnedDMs) sortDM(id); sortedDMs = sortedDMs.filter(n => n); for (let pos in sortedDMs) { pinnedDMs[sortedDMs[pos]] = parseInt(pos); if (this.ChannelUtils.getChannel(sortedDMs[pos])) existingDMs.push(sortedDMs[pos]); } BDFDB.saveAllData(pinnedDMs, this, type); return existingDMs; } forceUpdateScroller (scroller) { if (this.updatingScroller) return; this.updatingScroller = true; var stateNode = BDFDB.getReactValue(scroller, "return.return.return.stateNode"); if (stateNode) stateNode.updater.enqueueForceUpdate(stateNode); setTimeout(() => {delete this.updatingScroller;},1000); } addPinnedRecent (id) { let dms = document.querySelector(BDFDB.dotCN.dms); if (dms && !dms.querySelector(`${BDFDB.dotCN.guild}.pinned[channelid="${id}"]`)) { let info = this.ChannelUtils.getChannel(id); if (info) { let dmdiv = BDFDB.htmlToElement(this.recentDMMarkup); let user = info.type == 1 ? this.UserUtils.getUser(info.recipients[0]) : null; dmdiv.setAttribute("channelid", id); dms.insertBefore(dmdiv, dms.firstElementChild); let avatar = dmdiv.querySelector(BDFDB.dotCN.avatarinner); let dmname = info.name; if (!dmname && info.recipients.length > 0) { for (let dmuser_id of info.recipients) { dmname = dmname ? dmname + ", " : dmname; dmname = dmname + this.UserUtils.getUser(dmuser_id).username; } } let EditUsersData = user && BDFDB.isPluginEnabled("EditUsers") ? BDFDB.Plugins["editusers"].getUserData(user.id, dmdiv) : {}; if (!EditUsersData.removeIcon) avatar.style.setProperty("background-image", `url(${EditUsersData.url || BDFDB.getChannelIcon(id)})`); avatar.setAttribute("channel", dmname); if (user) avatar.setAttribute("user", user.username); dmdiv.addEventListener("mouseenter", () => { let FreshEditUsersData = user && BDFDB.isPluginEnabled("EditUsers") ? BDFDB.Plugins["editusers"].getUserData(user.id, dmdiv) : {}; BDFDB.createTooltip(FreshEditUsersData.name || dmname, dmdiv, {selector:(BDFDB.isObjectEmpty(FreshEditUsersData) ? "" : "EditUsers-tooltip"),type:"right"}); }); dmdiv.addEventListener("click", () => { this.ChannelSwitchUtils.selectPrivateChannel(id); }); dmdiv.addEventListener("contextmenu", e => { let dmContext = BDFDB.htmlToElement(this.dmUnpinContextMarkup); dmContext.querySelector(".unpindm-guild-item").addEventListener("click", () => { BDFDB.removeEles(dmdiv, dmContext); this.unhideNativeDM(id); BDFDB.removeData(id, this, "pinnedRecents"); }); BDFDB.appendContextMenu(dmContext, e); }); this.addHoverBehaviour(dmdiv); this.updateUnreadCount(id); this.hideNativeDM(id); } } } updateUnreadCount (id) { let dmdiv = document.querySelector(`${BDFDB.dotCNS.dms + BDFDB.dotCN.guild}.pinned[channelid="${id}"]`); if (Node.prototype.isPrototypeOf(dmdiv)) { let count = this.UnreadUtils.getUnreadCount(dmdiv.getAttribute("channelid")); let badge = dmdiv.querySelector(BDFDB.dotCN.badge); BDFDB.toggleEles(badge, count > 0); BDFDB.toggleClass(dmdiv, "has-new-messages", count > 0); badge.innerText = count; } } hideNativeDM (id) { let dmdiv = BDFDB.getDmDiv(id); if (Node.prototype.isPrototypeOf(dmdiv)) { BDFDB.toggleEles(dmdiv, false); BDFDB.addClass(dmdiv, "hidden-by-pin"); } } unhideNativeDM (id) { let dmdiv = BDFDB.getDmDiv(id); if (Node.prototype.isPrototypeOf(dmdiv) && BDFDB.containsClass(dmdiv, "hidden-by-pin")) { BDFDB.toggleEles(dmdiv, true); BDFDB.removeClass(dmdiv, "hidden-by-pin"); } } addHoverBehaviour (div) { /* based on stuff from Zerebos */ let divinner = div.querySelector(BDFDB.dotCN.guildinner); let divicon = div.querySelector(BDFDB.dotCN.guildicon); let backgroundColor = new this.Animations.Value(0); backgroundColor .interpolate({ inputRange: [0, 1], outputRange: [this.DiscordConstants.Colors.CHANNELS_GREY, this.DiscordConstants.Colors.BRAND_PURPLE] }) .addListener((value) => { if (BDFDB.containsClass(divicon, BDFDB.disCN.avatarnoicon)) { let comp = BDFDB.colorCONVERT(value.value, "RGBCOMP"); if (comp) divinner.style.setProperty("background-color", `rgb(${comp[0]}, ${comp[1]}, ${comp[2]})`); } }); let borderRadius = new this.Animations.Value(0); borderRadius .interpolate({ inputRange: [0, 1], outputRange: [25, 15] }) .addListener((value) => { divinner.style.setProperty("border-radius", `${value.value}px`); }); let animate = (v) => { this.Animations.parallel([ this.Animations.timing(backgroundColor, {toValue: v, duration: 200}), this.Animations.spring(borderRadius, {toValue: v, friction: 3}) ]).start(); }; div.addEventListener("mouseenter", () => {animate(1);}) div.addEventListener("mouseleave", () => {if (!BDFDB.containsClass(div, BDFDB.disCN.guildselected)) animate(0);}); } getSettingsPanel () { if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return; let settingshtml = `