[ShowHiddenChannels] Add last message info, fix collapsing (#146)

Co-authored-by: Snek <107999380+sneksnake@users.noreply.github.com>
This commit is contained in:
Nico 2022-10-24 23:22:39 +02:00 committed by GitHub
parent 5fac8be0ae
commit 30dd4b9e01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,7 @@ import { Settings } from "../Vencord";
import { waitFor } from "../webpack"; import { waitFor } from "../webpack";
import { Button, ChannelStore, Text } from "../webpack/common"; import { Button, ChannelStore, Text } from "../webpack/common";
const CONNECT = 1048576n;
const VIEW_CHANNEL = 1024n; const VIEW_CHANNEL = 1024n;
let can = (permission, channel) => true; let can = (permission, channel) => true;
@ -39,7 +40,11 @@ export default definePlugin({
{ {
name: "Average React Enjoyer", name: "Average React Enjoyer",
id: 1004904120056029256n id: 1004904120056029256n
} },
{
name: "D3SOX",
id: 201052085641281538n
},
], ],
options: { options: {
hideUnreads: { hideUnreads: {
@ -55,11 +60,11 @@ export default definePlugin({
find: ".CannotShow", find: ".CannotShow",
replacement: { replacement: {
match: /renderLevel:(\w+)\.CannotShow/g, match: /renderLevel:(\w+)\.CannotShow/g,
replace: "renderLevel:$1.Show" replace: "renderLevel:Vencord.Plugins.plugins.ShowHiddenChannels.shouldShow(this.record, this.category, this.isMuted) ? $1.Show : $1.CannotShow"
} }
}, },
{ {
// This is where the logic that chooses the icon is, we overide it to be a locked voice channel if it's hidden // This is where the logic that chooses the icon is, we override it to be a locked voice channel if it's hidden
find: ".rulesChannelId))", find: ".rulesChannelId))",
replacement: { replacement: {
match: /(\w+)\.locked(.*?)switch\((\w+)\.type\)({case \w+\.\w+\.GUILD_ANNOUNCEMENT)/g, match: /(\w+)\.locked(.*?)switch\((\w+)\.type\)({case \w+\.\w+\.GUILD_ANNOUNCEMENT)/g,
@ -67,15 +72,31 @@ export default definePlugin({
} }
}, },
{ {
find: "selectChannel:function", // Prevents Discord from trying to fetch message and create a 403 error
replacement: [ find: "fetchMessages:function",
{ replacement: {
match: /selectChannel:function\((\w)\){/g, match: /fetchMessages:function\((\w)\){/g,
replace: "selectChannel:function($1){if(Vencord.Plugins.plugins.ShowHiddenChannels.channelSelected($1)) return;" replace: "fetchMessages:function($1){if(Vencord.Plugins.plugins.ShowHiddenChannels.isHiddenChannel($1)) return;"
} }
]
}, },
{ {
// Don't switch to the channel if it's hidden, but show a modal instead
find: ".selectGuild(",
replacement: {
match: /(function \w+\(\w\)\{var (\w)=\w+\(\w\);)/g,
replace: "$1if(Vencord.Plugins.plugins.ShowHiddenChannels.channelSelected($2?.params))return;"
}
},
{
// Prevent categories from disappearing when they're collapsed
find: ".prototype.shouldShowEmptyCategory=function(){",
replacement: {
match: /(\.prototype\.shouldShowEmptyCategory=function\(\){)/g,
replace: "$1return true;"
}
},
{
// Hide unreads
find: "?\"button\":\"link\"", find: "?\"button\":\"link\"",
predicate: () => Settings.plugins.ShowHiddenChannels.hideUnreads === true, predicate: () => Settings.plugins.ShowHiddenChannels.hideUnreads === true,
replacement: { replacement: {
@ -84,18 +105,33 @@ export default definePlugin({
} }
} }
], ],
shouldShow(channel, category, isMuted) {
if (!this.isHiddenChannel(channel)) return false;
if (!category) return false;
if (channel.type === 0 && category.guild?.hideMutedChannels && isMuted) return false;
return !category.isCollapsed;
},
isHiddenChannel(channel) { isHiddenChannel(channel) {
if (!channel) return false; if (!channel) return false;
channel._isHiddenChannel = !can(VIEW_CHANNEL, channel); if (channel.channelId)
channel = ChannelStore.getChannel(channel.channelId);
if (!channel || channel.isDM() || channel.isGroupDM() || channel.isMultiUserDM())
return false;
// check for disallowed voice channels too so that they get hidden when collapsing the category
channel._isHiddenChannel = !can(VIEW_CHANNEL, channel) || (channel.type === 2 && !can(CONNECT, channel));
return channel._isHiddenChannel; return channel._isHiddenChannel;
}, },
channelSelected(channelData) { channelSelected(channelData) {
if (!channelData) return false;
const channel = ChannelStore.getChannel(channelData.channelId); const channel = ChannelStore.getChannel(channelData.channelId);
if (!channel) return false;
if (!channel || channel.isDM() || channel.isGroupDM() || channel.isMultiUserDM()) return false;
const isHidden = this.isHiddenChannel(channel); const isHidden = this.isHiddenChannel(channel);
if (isHidden) // check for type again, otherwise it would show it for hidden stage channels
if (channel.type === 0 && isHidden) {
const lastMessageDate = channel.lastActiveTimestamp ? new Date(channel.lastActiveTimestamp).toLocaleString() : null;
openModal(modalProps => ( openModal(modalProps => (
<ModalRoot size={ModalSize.SMALL} {...modalProps}> <ModalRoot size={ModalSize.SMALL} {...modalProps}>
<ModalHeader> <ModalHeader>
@ -118,6 +154,14 @@ export default definePlugin({
<Text variant="code">{channel.topic}</Text> <Text variant="code">{channel.topic}</Text>
</> </>
)} )}
{lastMessageDate && (
<>
<Text variant="text-md/bold" style={{ marginTop: 10 }}>
Last message sent:
</Text>
<Text variant="code">{lastMessageDate}</Text>
</>
)}
</ModalContent> </ModalContent>
<ModalFooter> <ModalFooter>
<Flex> <Flex>
@ -132,6 +176,7 @@ export default definePlugin({
</ModalFooter> </ModalFooter>
</ModalRoot> </ModalRoot>
)); ));
}
return isHidden; return isHidden;
} }
}); });