//META{"name":"FriendNotifications","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/FriendNotifications","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/FriendNotifications/FriendNotifications.plugin.js"}*// class FriendNotifications { getName () {return "FriendNotifications";} getVersion () {return "1.1.9";} getAuthor () {return "DevilBro";} getDescription () {return "Notifies you when a friend either logs in or out. Click the Online Friend-Counter to display a timelog of the current session.";} initConstructor () { this.changelog = { "fixed":[["Missing Friend Counter","Falls back to a check interval if the Online Counter isn't rendered. Timelog can now also be viewed in the settings"]] }; this.patchModules = { "FriendsOnline":["componentDidMount","componentDidUpdate"] }; this.friendsOnlineList = {}; this.checkInterval = null; this.timeLog = []; this.timeLogModalMarkup = `

Friends LogIn/-Out Timelog

`; this.logEntryMarkup = `

`; this.css = ` ${BDFDB.dotCN.guilds} > ${BDFDB.dotCN.friendsonline} { cursor: pointer; } .${this.name}-modal .log-time { width: 110px; } .${this.name}-modal .log-avatar { width: 35px; height: 35px; background-size: cover; background-position: center; border-radius: 50%; } .${this.name}-settings .avatar-list { display: flex; align-items: center; flex-wrap: wrap; } .${this.name}-settings .type-toast, .${this.name}-settings .type-desktop { border-radius: 3px; padding: 0 3px; } .${this.name}-settings .type-toast { background-color: #7289DA; } .${this.name}-settings .type-desktop { background-color: #43B581; } .${this.name}-settings .settings-avatar.desktop { border-color: #43B581; } .${this.name}-settings .settings-avatar { margin: 5px; width: 50px; height: 50px; background-size: cover; background-position: center; border: 5px solid #7289DA; border-radius: 50%; box-sizing: border-box; cursor: pointer; } .${this.name}-settings .settings-avatar.desktop { border-color: #43B581; } .${this.name}-settings .settings-avatar.disabled { border-color: #36393F; filter: grayscale(100%) brightness(50%); }`; this.defaults = { settings: { muteOnDND: {value:false, description:"Do not notify me when I am DnD:"}, onlyOnOnline: {value:false, description:"Only notify me when a User logs in:"}, openOnClick: {value:false, description:"Open the DM when you click a Notification:"} }, notificationsounds: { toastonline: {value:{url:null,song:null,mute:false}}, toastoffline: {value:{url:null,song:null,mute:false}}, desktoponline: {value:{url:null,song:null,mute:false}}, desktopoffline: {value:{url:null,song:null,mute:false}} } }; } getSettingsPanel () { if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return; let settings = BDFDB.getAllData(this, "settings"); let notificationsounds = BDFDB.getAllData(this, "notificationsounds"); let desktop = BDFDB.loadAllData(this, "desktop"); let disabled = BDFDB.loadAllData(this, "disabled"); let settingshtml = `
${this.name}
`; for (let key in settings) { settingshtml += `

${this.defaults.settings[key].description}

`; } for (let key in notificationsounds) { if (key.indexOf("desktop") == -1 || "Notification" in window) settingshtml += `
${key} notification sound:
Mute:
`; } settingshtml += `

Click on a Icon to toggle Notifications for that User:

`; if ("Notification" in window) settingshtml += `

Rightclick on a Icon to toggle Notifications for that User:

`; settingshtml += `
`; for (let id of this.FriendUtils.getFriendIDs()) { let user = this.UserUtils.getUser(id); if (user) { let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; settingshtml += `
`; } } settingshtml += `
`; settingshtml += `

Batch set Users:

${"Notification" in window ? `` : ``}
`; settingshtml += `

Timelog of LogIns/-Outs:

`; settingshtml += `
`; let settingspanel = BDFDB.htmlToElement(settingshtml); BDFDB.initElements(settingspanel, this); BDFDB.addEventListener(this, settingspanel, "click", ".btn-savesong", e => {this.saveAudio(settingspanel, e.currentTarget.getAttribute("option"));}); BDFDB.addEventListener(this, settingspanel, "click", ".mute-checkbox", e => { let option = e.currentTarget.getAttribute("option"); let notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.mute = e.currentTarget.checked; BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }); BDFDB.addEventListener(this, settingspanel, "mouseenter", ".settings-avatar", e => { let user = this.UserUtils.getUser(e.currentTarget.getAttribute("user-id")); let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; BDFDB.createTooltip(data.name ? data.name : user.username, e.currentTarget, {type:"top"}); }); BDFDB.addEventListener(this, settingspanel, "contextmenu", ".settings-avatar", e => { if (!("Notification" in window)) return; let desktopoff = !BDFDB.containsClass(e.currentTarget, "desktop"); let id = e.currentTarget.getAttribute("user-id"); BDFDB.removeClass(e.currentTarget, "disabled"); BDFDB.toggleClass(e.currentTarget, "desktop", desktopoff); BDFDB.saveData(id, desktopoff, this, "desktop"); BDFDB.removeData(id, this, "disabled"); }); BDFDB.addEventListener(this, settingspanel, "click", ".settings-avatar", e => { let disableoff = !BDFDB.containsClass(e.currentTarget, "disabled"); let id = e.currentTarget.getAttribute("user-id"); BDFDB.removeClass(e.currentTarget, "desktop"); BDFDB.toggleClass(e.currentTarget, "disabled", disableoff); BDFDB.saveData(id, disableoff, this, "disabled"); BDFDB.removeData(id, this, "desktop"); }); BDFDB.addEventListener(this, settingspanel, "click", ".disable-all, .toast-all, .desktop-all", e => { let disableon = e.currentTarget.getAttribute("do-disable"); let desktopon = e.currentTarget.getAttribute("do-desktop"); let disabledata = BDFDB.loadAllData(this, "disabled"); let desktopdata = BDFDB.loadAllData(this, "desktop"); settingspanel.querySelectorAll(".settings-avatar").forEach(avatar => { let id = avatar.getAttribute("user-id"); BDFDB.toggleClass(avatar, "disabled", disableon); BDFDB.toggleClass(avatar, "desktop", desktopon); disableon ? disabledata[id] = true : delete disabledata[id]; desktopon ? desktopdata[id] = true : delete desktopdata[id]; }); BDFDB.saveAllData(disabledata, this, "disabled"); BDFDB.saveAllData(desktopdata, this, "desktop"); }); BDFDB.addEventListener(this, settingspanel, "click", ".btn-timelog", () => {this.showTimeLog();}); return settingspanel; } //legacy load () {} start () { if (!global.BDFDB) global.BDFDB = {myPlugins:{}}; if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this; var libraryScript = document.querySelector('head script#BDFDBLibraryScript'); if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) { if (libraryScript) libraryScript.remove(); libraryScript = document.createElement("script"); libraryScript.setAttribute("id", "BDFDBLibraryScript"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"); libraryScript.setAttribute("date", performance.now()); libraryScript.addEventListener("load", () => {this.initialize();}); document.head.appendChild(libraryScript); this.libLoadTimeout = setTimeout(() => { libraryScript.remove(); require("request")("https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js", (error, response, body) => { if (body) { libraryScript = document.createElement("script"); libraryScript.setAttribute("id", "BDFDBLibraryScript"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("date", performance.now()); libraryScript.innerText = body; document.head.appendChild(libraryScript); } this.initialize(); }); }, 15000); } 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; BDFDB.loadMessage(this); this.FriendUtils = BDFDB.WebModules.findByProperties("getFriendIDs", "getRelationships"); this.ChannelUtils = BDFDB.WebModules.findByProperties("getDMFromUserId"); this.ChannelSwitchUtils = BDFDB.WebModules.findByProperties("selectPrivateChannel"); this.PrivateChannelUtils = BDFDB.WebModules.findByProperties("openPrivateChannel"); this.UserMetaStore = BDFDB.WebModules.findByProperties("getStatus", "getOnlineFriendCount"); this.UserUtils = BDFDB.WebModules.findByProperties("getUsers"); for (let id of this.FriendUtils.getFriendIDs()) { this.friendsOnlineList[id] = this.UserMetaStore.getStatus(id) != "offline"; } this.startInterval(); BDFDB.WebModules.forceAllUpdates(this); } else { console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!'); } } stop () { if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { clearInterval(this.checkInterval); BDFDB.unloadMessage(this); } } // begin of own functions saveAudio (settingspanel, option) { let successSavedAudio = (parsedurl, parseddata) => { if (parsedurl && parseddata) BDFDB.showToast(`Sound was saved successfully.`, {type:"success"}); let notificationsound = BDFDB.getData(option, this, "notificationsounds"); notificationsound.url = parsedurl; notificationsound.song = parseddata; BDFDB.saveData(option, notificationsound, this, "notificationsounds"); }; let url = settingspanel.querySelector(`.songInput[option="${option}"]`).value; if (url.length == 0) { BDFDB.showToast(`Sound file was removed.`, {type:"warn"}); successSavedAudio(url, url); } else if (url.indexOf("http") == 0) { require("request")(url, (error, response, result) => { if (response) { let type = response.headers["content-type"]; if (type && (type.indexOf("octet-stream") > -1 || type.indexOf("audio") > -1 || type.indexOf("video") > -1)) { successSavedAudio(url, url); return; } } BDFDB.showToast("Use a valid direct link to a video or audio source. They usually end on something like .mp3, .mp4 or .wav.", {type:"danger"}); }); } else { require("fs").readFile(url, (error, response) => { if (error) BDFDB.showToast("Could not fetch file. Please make sure the file exists.", {type:"danger"}); else successSavedAudio(url, `data:audio/mpeg;base64,${response.toString("base64")}`); }); } } processFriendsOnline (instance, wrapper) { BDFDB.addEventListener(this, wrapper, "mouseenter", () => {BDFDB.createTooltip("Timelog", wrapper, {type:"right"});}); BDFDB.addEventListener(this, wrapper, "click", () => {this.showTimeLog();}); clearInterval(this.checkInterval); this.checkFriends(); } checkFriends () { let settings = BDFDB.getAllData(this, "settings"); for (let id of this.FriendUtils.getFriendIDs()) { let online = this.UserMetaStore.getStatus(id) != "offline"; let user = this.UserUtils.getUser(id); if (user && this.friendsOnlineList[id] != online && !BDFDB.loadData(id, this, "disabled")) { this.timeLog.push({user, online, time: new Date()}); if (!(settings.onlyOnOnline && !online) && !(settings.muteOnDND && BDFDB.getUserStatus() == "dnd")) { let data = BDFDB.loadData(user.id, "EditUsers", "users") || {}; let string = `${BDFDB.encodeToHTML(data.name ? data.name : user.username)} is ${online ? "online" : "offline"}.`; let avatar = data.removeIcon ? "" : (data.url ? data.url : BDFDB.getUserAvatar(user.id)); let openChannel = () => { if (settings.openOnClick) { let DMid = this.ChannelUtils.getDMFromUserId(user.id) if (DMid) this.ChannelSwitchUtils.selectPrivateChannel(DMid); else this.PrivateChannelUtils.openPrivateChannel(BDFDB.myData.id, user.id); require("electron").remote.getCurrentWindow().maximize(); } }; if (!BDFDB.loadData(id, this, "desktop")) { let toast = BDFDB.showToast(`
${string}
`, {html:true, timeout:5000, type:(online ? "success" : null), icon:false, selector:`friendnotifications-${online ? "online" : "offline"}-toast`}); toast.addEventListener("click", openChannel); let notificationsound = BDFDB.getData(online ? "toastonline" : "toastoffline", this, "notificationsounds"); if (!notificationsound.mute && notificationsound.song) { let audio = new Audio(); audio.src = notificationsound.song; audio.play(); } } else { let notificationsound = BDFDB.getData(online ? "desktoponline" : "desktopoffline", this, "notificationsounds"); BDFDB.showDesktopNotification(string, {icon:avatar, timeout:5000, click:openChannel, silent:notificationsound.mute, sound:notificationsound.song}); } } } this.friendsOnlineList[id] = online; } } startInterval () { clearInterval(this.checkInterval); let oldcount = this.UserMetaStore.getOnlineFriendCount(), newcount = 0; this.checkInterval = setInterval(() => { newcount = this.UserMetaStore.getOnlineFriendCount(); if (oldcount != newcount) { oldcount = newcount; this.checkFriends(); } },10000); } showTimeLog () { let timeLogModal = BDFDB.htmlToElement(this.timeLogModalMarkup); let container = timeLogModal.querySelector(".entries"); if (!container) return; let logs = this.timeLog.slice(0).reverse(); for (let log of logs) { if (container.childElementCount) container.appendChild(BDFDB.htmlToElement(`
`)); let data = BDFDB.loadData(log.user.id, "EditUsers", "users") || {}; let entry = BDFDB.htmlToElement(this.logEntryMarkup); entry.querySelector(".log-time").innerText = `[${log.time.toLocaleTimeString()}]`; entry.querySelector(".log-avatar").style.setProperty("background-image", `url(${data.removeIcon ? "" : (data.url ? data.url : BDFDB.getUserAvatar(log.user.id))})`); entry.querySelector(".log-description").innerText = `${data.name || log.user.username} is ${log.online ? "online" : "offline"}.`; container.appendChild(entry) } BDFDB.appendModal(timeLogModal); } }