/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import "./style.css";
import { definePluginSettings } from "@api/settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy, findLazy } from "@webpack";
import { ChannelStore, PermissionStore, Tooltip } from "@webpack/common";
import { Channel } from "discord-types/general";
import HiddenChannelLockScreen from "./components/HiddenChannelLockScreen";
const ChannelListClasses = findByPropsLazy("channelName", "subtitle", "modeMuted", "iconContainer");
const Permissions = findLazy(m => typeof m.VIEW_CHANNEL === "bigint");
enum ShowMode {
LockIcon,
HiddenIconWithMutedStyle
}
const settings = definePluginSettings({
hideUnreads: {
description: "Hide Unreads",
type: OptionType.BOOLEAN,
default: true,
restartNeeded: true
},
showMode: {
description: "The mode used to display hidden channels.",
type: OptionType.SELECT,
options: [
{ label: "Plain style with Lock Icon instead", value: ShowMode.LockIcon, default: true },
{ label: "Muted style with hidden eye icon on the right", value: ShowMode.HiddenIconWithMutedStyle },
],
restartNeeded: true
}
});
export default definePlugin({
name: "ShowHiddenChannels",
description: "Show channels that you do not have access to view.",
authors: [Devs.BigDuck, Devs.AverageReactEnjoyer, Devs.D3SOX, Devs.Ven, Devs.Nuckyz, Devs.Nickyux, Devs.dzshn],
settings,
patches: [
{
// RenderLevel defines if a channel is hidden, collapsed in category, visible, etc
find: ".CannotShow",
// These replacements only change the necessary CannotShow's
replacement: [
{
match: /(?<=isChannelGatedAndVisible\(this\.record\.guild_id,this\.record\.id\).+?renderLevel:)(?\i)\..+?(?=,)/,
replace: "this.category.isCollapsed?$.WouldShowIfUncollapsed:$.Show"
},
// Move isChannelGatedAndVisible renderLevel logic to the bottom to not show hidden channels in case they are muted
{
match: /(?<=(?if\(!\i\.\i\.can\(\i\.\i\.VIEW_CHANNEL.+?{)if\(this\.id===\i\).+?};)(?if\(!\i\.\i\.isChannelGatedAndVisible\(.+?})(?.+?)(?=return{renderLevel:\i\.Show.{1,40}return \i)/,
replace: "$$$}"
},
{
match: /(?<=renderLevel:(?\i\(this,\i\)\?\i\.Show:\i\.WouldShowIfUncollapsed).+?renderLevel:).+?(?=,)/,
replace: "$"
},
{
match: /(?<=activeJoinedRelevantThreads.+?renderLevel:.+?,threadIds:\i\(this.record.+?renderLevel:)(?\i)\..+?(?=,)/,
replace: "$.Show"
},
{
match: /(?<=getRenderLevel=function.+?return ).+?\?(?.+?):\i\.CannotShow(?=})/,
replace: "$"
}
]
},
{
find: "VoiceChannel.renderPopout: There must always be something to render",
replacement: [
// Do nothing when trying to join a voice channel if the channel is hidden
{
match: /(?<=handleClick=function\(\){)(?=.{1,80}(?\i)\.handleVoiceConnect\(\))/,
replace: "if($self.isHiddenChannel($.props.channel))return;"
},
// Render null instead of the buttons if the channel is hidden
...[
"renderEditButton",
"renderInviteButton",
"renderOpenChatButton"
].map(func => ({
match: new RegExp(`(?<=\\i\\.${func}=function\\(\\){)`, "g"), // Global because Discord has multiple declarations of the same functions
replace: "if($self.isHiddenChannel(this.props.channel))return null;"
}))
]
},
{
find: ".Messages.CHANNEL_TOOLTIP_DIRECTORY",
predicate: () => settings.store.showMode === ShowMode.LockIcon,
replacement: {
// Lock Icon
match: /(?=switch\((?\i)\.type\).{1,30}\.GUILD_ANNOUNCEMENT.{1,30}\(0,\i\.\i\))/,
replace: "if($self.isHiddenChannel($))return $self.LockIcon;"
}
},
{
find: ".UNREAD_HIGHLIGHT",
predicate: () => settings.store.hideUnreads === true,
replacement: [{
// Hide unreads
match: /(?<=\i\.connected,\i=)(?=(?\i)\.unread)/,
replace: "$self.isHiddenChannel($.channel)?false:"
}]
},
{
find: ".UNREAD_HIGHLIGHT",
predicate: () => settings.store.showMode === ShowMode.HiddenIconWithMutedStyle,
replacement: [
// Make the channel appear as muted if it's hidden
{
match: /(?<=\i\.name,\i=)(?=(?\i)\.muted)/,
replace: "$self.isHiddenChannel($.channel)?true:"
},
// Add the hidden eye icon if the channel is hidden
{
match: /(?<=(?\i)=\i\.channel,.+?\(\)\.children.+?:null)/,
replace: ",$self.isHiddenChannel($)?$self.HiddenChannelIcon():null"
},
// Make voice channels also appear as muted if they are muted
{
match: /(?<=\i\(\)\.wrapper:\i\(\)\.notInteractive,)(?.+?)(?(?\i)\?\i\.MUTED)/,
replace: "$:\"\",$$?\"\""
}
]
},
// Make muted channels also appear as unread if hide unreads is false, using the HiddenIconWithMutedStyle and the channel is hidden
{
find: ".UNREAD_HIGHLIGHT",
predicate: () => settings.store.hideUnreads === false && settings.store.showMode === ShowMode.HiddenIconWithMutedStyle,
replacement: {
match: /(?<=(?\i)=\i\.channel,.+?\.LOCKED:\i)/,
replace: "&&!($self.settings.store.hideUnreads===false&&$self.isHiddenChannel($))"
}
},
{
// Hide New unreads box for hidden channels
find: '.displayName="ChannelListUnreadsStore"',
replacement: {
match: /(?<=return null!=(?\i))(?=.{1,130}hasRelevantUnread\(\i\))/,
replace: "&&!$self.isHiddenChannel($)"
}
},
// Only render the channel header and buttons that work when transitioning to a hidden channel
{
find: "Missing channel in Channel.renderHeaderToolbar",
replacement: [
{
match: /(?<=renderHeaderToolbar=function.+?case \i\.\i\.GUILD_TEXT:)(?=.+?;(?.+?{channel:(?\i)},"notifications"\)\);))/,
replace: "if($self.isHiddenChannel($)){$break;}"
},
{
match: /(?<=renderHeaderToolbar=function.+?case \i\.\i\.GUILD_FORUM:if\(!\i\){)(?=.+?;(?.+?{channel:(?\i)},"notifications"\)\)))/,
replace: "if($self.isHiddenChannel($)){$;break;}"
},
{
match: /(?<=(?\i)\.renderMobileToolbar=function.+?case \i\.\i\.GUILD_FORUM:)/,
replace: "if($self.isHiddenChannel($.props.channel))break;"
},
{
match: /(?<=renderHeaderBar=function.+?hideSearch:(?\i)\.isDirectory\(\))/,
replace: "||$self.isHiddenChannel($)"
},
{
match: /(?<=renderSidebar=function\(\){)/,
replace: "if($self.isHiddenChannel(this.props.channel))return null;"
},
{
match: /(?<=renderChat=function\(\){)/,
replace: "if($self.isHiddenChannel(this.props.channel))return $self.HiddenChannelLockScreen(this.props.channel);"
},
]
},
// Avoid trying to fetch messages from hidden channels
{
find: '"MessageManager"',
replacement: [
{
match: /(?<=if\(null!=(?\i)\).{1,100}"Skipping fetch because channelId is a static route".{1,10}else{)/,
replace: "if($self.isHiddenChannel({channelId:$}))return;"
},
]
},
// Patch keybind handlers so you can't accidentally jump to hidden channels
{
find: '"alt+shift+down"',
replacement: {
match: /(?<=getChannel\(\i\);return null!=(?\i))(?=.{1,130}hasRelevantUnread\(\i\))/,
replace: "&&!$self.isHiddenChannel($)"
}
},
{
find: '"alt+down"',
replacement: {
match: /(?<=getState\(\)\.channelId.{1,30}\(0,\i\.\i\)\(\i\))(?=\.map\()/,
replace: ".filter(ch=>!$self.isHiddenChannel(ch))"
}
},
// Export the emoji component used on the lock screen
{
find: 'jumboable?"jumbo":"default"',
replacement: {
match: /(?<=\i:\(\)=>\i)(?=}.+?(?\i)=function.{1,20}node,\i=\i.isInteracting)/,
replace: ",hc1:()=>$" // Blame Ven length check for the small name :pensive_cry:
}
}
],
isHiddenChannel(channel: Channel & { channelId?: string; }) {
if (!channel) return false;
if (channel.channelId) channel = ChannelStore.getChannel(channel.channelId);
if (!channel || channel.isDM() || channel.isGroupDM() || channel.isMultiUserDM()) return false;
return !PermissionStore.can(Permissions.VIEW_CHANNEL, channel);
},
HiddenChannelLockScreen: (channel: any) => ,
LockIcon: () => (
),
HiddenChannelIcon: ErrorBoundary.wrap(() => (
{({ onMouseLeave, onMouseEnter }) => (
)}
), { noop: true })
});