Apex-Plugins/ReadAll/index.js
2025-07-31 19:50:20 +02:00

1 line
7.9 KiB
JavaScript

(function(F,u,o,s,S,M,i,c){"use strict";const k={excludedServers:[],excludedDMs:[]},g=function(){return{...k,...M.storage}},v=function(t){Object.assign(M.storage,t)},L=function(t){const e=g();return e.excludedServers.includes(t)?!1:(e.excludedServers.push(t),v(e),!0)},U=function(t){const e=g(),n=e.excludedServers.indexOf(t);return n>-1?(e.excludedServers.splice(n,1),v(e),!0):!1},$=function(t){const e=g();return e.excludedDMs.includes(t)?!1:(e.excludedDMs.push(t),v(e),!0)},O=function(t){const e=g(),n=e.excludedDMs.indexOf(t);return n>-1?(e.excludedDMs.splice(n,1),v(e),!0):!1},j=function(t){return g().excludedServers.includes(t)},G=function(t){return g().excludedDMs.includes(t)},J=function(t){try{return u.findByStoreName("GuildStore")?.getGuild?.(t)?.name||`Unknown Server (${t})`}catch{return`Unknown Server (${t})`}},K=function(t){try{const e=u.findByStoreName("ChannelStore")?.getChannel?.(t);if(e){if(e.name)return e.name;if(e.recipients&&e.recipients.length>0){const n=u.findByStoreName("UserStore")?.getUser?.(e.recipients[0]);return n?.username?`@${n.username}`:`Unknown User (${t})`}}return`Unknown DM (${t})`}catch{return`Unknown DM (${t})`}},V=function(){const t=g();t.excludedServers=[],t.excludedDMs=[],v(t)},_=function(){const t=g();return{servers:t.excludedServers.map(function(e){return{id:e,name:J(e)}}),dms:t.excludedDMs.map(function(e){return{id:e,name:K(e)}})}};let p,R,B,l,b,I,x=null,E=null,A=null,D=null,m=0;const y=6e4,Q=function(){p=u.findByStoreName("GuildStore"),R=u.findByStoreName("GuildChannelStore")||u.findByStoreName("ChannelStore"),I=u.findByStoreName("ChannelStore"),l=u.findByStoreName("ReadStateStore"),B=u.findByStoreName("ActiveJoinedThreadsStore")||u.findByProps("getActiveJoinedThreadsForGuild"),b=u.findByProps("dispatch","subscribe")||u.findByStoreName("Dispatcher")},q=function(){const t=[],e=I||R;if(!e)return t;if(e.getPrivateChannels)try{const n=e.getPrivateChannels();n&&typeof n=="object"&&Object.values(n).forEach(function(r){r&&r.id&&t.push(r)})}catch{}if(t.length===0&&e.getSortedPrivateChannels)try{const n=e.getSortedPrivateChannels();Array.isArray(n)&&n.forEach(function(r){r&&r.id&&t.push(r)})}catch{}if(t.length===0&&e.getChannels)try{const n=e.getChannels("@me");n&&n.SELECTABLE&&n.SELECTABLE.forEach(function(r){const a=r.channel||r;a&&a.id&&t.push(a)})}catch{}if(t.length===0&&e.getChannel&&l?.getAllReadStates)try{const n=l.getAllReadStates();Object.keys(n).forEach(function(r){try{const a=e.getChannel(r);a&&(a.type===1||a.type===3||!a.guild_id&&!a.guildId)&&t.push(a)}catch{}})}catch{}return t},N=function(){if(!p||!l)return[];const t=[],e=p.getGuilds();return Object.values(e).forEach(function(n){if(n?.id)try{let r=[];const a=R||I;if(a?.getChannels){const h=a.getChannels(n.id);h?.SELECTABLE&&(r=r.concat(h.SELECTABLE)),h?.VOCAL&&(r=r.concat(h.VOCAL))}if(B?.getActiveJoinedThreadsForGuild)try{const h=B.getActiveJoinedThreadsForGuild(n.id),f=Object.values(h).flatMap(function(w){return Object.values(w||{})});r=r.concat(f)}catch{}r.forEach(function(h){const f=h?.channel||h;if(f?.id&&!j(n.id))try{l.hasUnread&&l.hasUnread(f.id)&&t.push({channelId:f.id,messageId:l.lastMessageId?.(f.id)||null,readStateType:0})}catch{}})}catch{}}),t},T=function(){const t=[];return q().forEach(function(e){if(e?.id&&!G(e.id))try{let n=!1;if(l.hasUnread&&(n=l.hasUnread(e.id)),!n&&l.getAllReadStates){const r=l.getAllReadStates()[e.id];r&&(n=r.mentionCount&&r.mentionCount>0||r._unreadCount&&r._unreadCount>0||r.unreadCount&&r.unreadCount>0)}n&&t.push({channelId:e.id,messageId:l.lastMessageId?.(e.id)||null,readStateType:0})}catch{}}),t},C=function(t="all"){if(!p||!l||!b)return!1;let e=[],n="";switch(t){case"server":e=N(),n="server";break;case"dm":e=T(),n="DM";break;case"all":default:e=[...N(),...T()],n="";break}if(e.length===0){const a=t==="all"?"No unread notifications found!":`No unread ${n} notifications found!`;return o.showToast(a,s.getAssetIDByName("ic_message_edit")),!0}b.dispatch({type:"BULK_ACK",context:"APP",channels:e});const r=t==="all"?`Cleared ${e.length} unread notifications!`:`Cleared ${e.length} unread ${n} notifications!`;return o.showToast(r,s.getAssetIDByName("ic_message_edit")),!0},z=function(){const t=Date.now(),e=t-m;if(e<y){const n=Math.ceil((y-e)/1e3);o.showToast(`Please wait ${n}s before using again`,s.getAssetIDByName("ic_close_16px"));return}m=t,C("all")},H=function(){const t=Date.now(),e=t-m;if(e<y){const n=Math.ceil((y-e)/1e3);o.showToast(`Please wait ${n}s before using again`,s.getAssetIDByName("ic_close_16px"));return}m=t,C("server")},W=function(){const t=Date.now(),e=t-m;if(e<y){const n=Math.ceil((y-e)/1e3);o.showToast(`Please wait ${n}s before using again`,s.getAssetIDByName("ic_close_16px"));return}m=t,C("server")},X=function(){const t=Date.now(),e=t-m;if(e<y){const n=Math.ceil((y-e)/1e3);o.showToast(`Please wait ${n}s before using again`,s.getAssetIDByName("ic_close_16px"));return}m=t,C("dm")};var Y={onLoad:function(){Q();try{x=S.registerCommand({name:"read",description:"Clear all unread notifications",applicationId:"-1",execute:function(){z()}}),E=S.registerCommand({name:"read all",description:"Clear server unread notifications only",applicationId:"-1",execute:function(){H()}}),A=S.registerCommand({name:"read server",description:"Clear server unread notifications only",applicationId:"-1",execute:function(){W()}}),D=S.registerCommand({name:"read dm",description:"Clear DM unread notifications only",applicationId:"-1",execute:function(){X()}})}catch{}},onUnload:function(){if(x)try{x(),x=null}catch{}if(E)try{E(),E=null}catch{}if(A)try{A(),A=null}catch{}if(D)try{D(),D=null}catch{}},settings:function(){const[t,e]=i.React.useState(""),[n,r]=i.React.useState(""),[a,h]=i.React.useState(_()),f=function(){h(_())},w=function(){t.trim()&&(L(t.trim())?(o.showToast("Added server to exceptions",s.getAssetIDByName("ic_check")),e(""),f()):o.showToast("Server already in exceptions",s.getAssetIDByName("ic_close_16px")))},P=function(){n.trim()&&($(n.trim())?(o.showToast("Added DM to exceptions",s.getAssetIDByName("ic_check")),r(""),f()):o.showToast("DM already in exceptions",s.getAssetIDByName("ic_close_16px")))},Z=function(d){U(d),o.showToast("Server removed from exceptions",s.getAssetIDByName("ic_check")),f()},ee=function(d){O(d),o.showToast("DM removed from exceptions",s.getAssetIDByName("ic_check")),f()},te=function(){V(),o.showToast("All exceptions cleared",s.getAssetIDByName("ic_check")),f()};return i.React.createElement(i.React.Fragment,null,i.React.createElement(c.Forms.FormSection,{title:"Server Exceptions"},i.React.createElement(c.Forms.FormText,{style:{marginBottom:10}},"Add server IDs to exclude from notification clearing:"),i.React.createElement(c.Forms.FormInput,{placeholder:"Enter server ID (e.g., 1325923169164333178)",value:t,onChange:e,onSubmitEditing:w}),i.React.createElement(c.Forms.FormRow,{label:"Add Server",onPress:w}),a.servers.map(function(d,ne){return i.React.createElement(c.Forms.FormRow,{key:d.id,label:d.name,subLabel:d.id,trailing:i.React.createElement(c.Forms.FormRow,{label:"Remove",style:{color:"#ff4757"},onPress:function(){return Z(d.id)}})})})),i.React.createElement(c.Forms.FormSection,{title:"DM Exceptions"},i.React.createElement(c.Forms.FormText,{style:{marginBottom:10}},"Add channel IDs to exclude from notification clearing:"),i.React.createElement(c.Forms.FormInput,{placeholder:"Enter channel ID (e.g., 1258452286682697890)",value:n,onChange:r,onSubmitEditing:P}),i.React.createElement(c.Forms.FormRow,{label:"Add DM",onPress:P}),a.dms.map(function(d,ne){return i.React.createElement(c.Forms.FormRow,{key:d.id,label:d.name,subLabel:d.id,trailing:i.React.createElement(c.Forms.FormRow,{label:"Remove",style:{color:"#ff4757"},onPress:function(){return ee(d.id)}})})})),i.React.createElement(c.Forms.FormSection,{title:"Actions"},i.React.createElement(c.Forms.FormRow,{label:"Clear All Exceptions",onPress:te})))}};return F.default=Y,Object.defineProperty(F,"__esModule",{value:!0}),F})({},vendetta.metro,vendetta.ui.toasts,vendetta.ui.assets,vendetta.commands,vendetta.plugin,vendetta.metro.common,vendetta.ui.components);