Update ShowHiddenChannels.plugin.js

This commit is contained in:
Mirco Wittrien 2022-06-10 19:44:50 +02:00
parent 724e4047c8
commit 8b4f171311
1 changed files with 35 additions and 137 deletions

View File

@ -2,7 +2,7 @@
* @name ShowHiddenChannels
* @author DevilBro
* @authorId 278543574059057154
* @version 3.1.2
* @version 3.1.3
* @description Displays all hidden Channels, which can't be accessed due to Role Restrictions, this won't allow you to read them (impossible)
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
@ -17,8 +17,13 @@ module.exports = (_ => {
"info": {
"name": "ShowHiddenChannels",
"author": "DevilBro",
"version": "3.1.2",
"version": "3.1.3",
"description": "Displays all hidden Channels, which can't be accessed due to Role Restrictions, this won't allow you to read them (impossible)"
},
"changeLog": {
"fixed": {
"Works again": "Plugin itself works again, had to remove the option for the separate category 'hidden', since Discord completely remodeled how they structure the channel list internally and i have yet to find an easy way to insert custom categories"
}
}
};
@ -60,7 +65,7 @@ module.exports = (_ => {
return template.content.firstElementChild;
}
} : (([Plugin, BDFDB]) => {
var blackList = [], collapseList = [], hiddenCategory, lastGuildId, overrideTypes = [];
var blackList = [], overrideTypes = [];
var hiddenChannelCache = {};
var accessModal;
@ -83,10 +88,16 @@ module.exports = (_ => {
PRIVATE_THREAD: "PRIVATE_THREAD"
};
const renderLevels = {
CAN_NOT_SHOW: 1,
DO_NOT_SHOW: 2,
WOULD_SHOW_IF_UNCOLLAPSED: 3,
SHOW: 4
};
const sortOrders = {
NATIVE: {value: "native", label: "Native Category in correct Order"},
BOTTOM: {value: "bottom", label: "Native Category at the bottom"},
EXTRA: {value: "extra", label: "Extra Category 'Hidden'"}
BOTTOM: {value: "bottom", label: "Native Category at the bottom"}
};
const UserRowComponent = class UserRow extends BdApi.React.Component {
@ -165,7 +176,7 @@ module.exports = (_ => {
return class ShowHiddenChannels extends Plugin {
onLoad () {
overrideTypes = Object.keys(BDFDB.DiscordConstants.PermissionOverrideType);
this.defaults = {
sortOrder: {
hidden: {
@ -175,7 +186,6 @@ module.exports = (_ => {
}
},
general: {
alwaysCollapse: {value: false, description: "Always collapse 'Hidden' Category after switching Servers"},
showVoiceUsers: {value: true, description: "Show connected Users in hidden Voice Channels"},
showForNormal: {value: true, description: "Add Access-Overview ContextMenu Entry for non-hidden Channels"}
},
@ -212,9 +222,6 @@ module.exports = (_ => {
onStart () {
this.saveBlackList(this.getBlackList());
let loadedCollapseList = BDFDB.DataUtils.load(this, "categorydata");
this.saveCollapseList(!BDFDB.ArrayUtils.is(loadedCollapseList) ? [] : loadedCollapseList);
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.GuildUtils, "setChannel", {instead: e => {
let channelId = (BDFDB.LibraryModules.VoiceUtils.getVoiceStateForUser(e.methodArguments[1]) || {}).channelId;
if (!channelId || !this.isChannelHidden(channelId)) return e.callOriginalMethod();
@ -225,45 +232,7 @@ module.exports = (_ => {
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.UnreadChannelUtils, "getMentionCount", {after: e => {
return this.isChannelHidden(e.methodArguments[0]) ? 0 : e.returnValue;
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.CategoryCollapseStore, "isCollapsed", {after: e => {
if (e.methodArguments[0] && e.methodArguments[0].endsWith("hidden")) {
if (this.settings.general.alwaysCollapse && e.methodArguments[0] != lastGuildId && !collapseList.includes(e.methodArguments[0])) {
collapseList.push(e.methodArguments[0]);
this.saveCollapseList(BDFDB.ArrayUtils.removeCopies(collapseList));
}
lastGuildId = e.methodArguments[0];
return collapseList.includes(e.methodArguments[0]);
}
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.CategoryCollapseUtils, "categoryCollapse", {before: e => {
if (e.methodArguments[0] && e.methodArguments[0].endsWith("hidden")) {
if (!collapseList.includes(e.methodArguments[0])) {
collapseList.push(e.methodArguments[0]);
this.saveCollapseList(BDFDB.ArrayUtils.removeCopies(collapseList));
}
}
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.CategoryCollapseUtils, "categoryExpand", {before: e => {
if (e.methodArguments[0] && e.methodArguments[0].endsWith("hidden")) {
if (collapseList.includes(e.methodArguments[0])) {
BDFDB.ArrayUtils.remove(collapseList, e.methodArguments[0], true);
this.saveCollapseList(BDFDB.ArrayUtils.removeCopies(collapseList));
}
}
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.GuildChannelStore, "getTextChannelNameDisambiguations", {after: e => {
let all = BDFDB.LibraryModules.ChannelStore.getMutableGuildChannelsForGuild(e.methodArguments[0]);
for (let channel_id in all) if (!e.returnValue[channel_id] && (all[channel_id].type != BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY && all[channel_id].type != BDFDB.DiscordConstants.ChannelTypes.GUILD_VOICE)) e.returnValue[channel_id] = {id: channel_id, name: all[channel_id].name};
}});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.ChannelIconUtils, "getChannelIconComponent", {before: e => {
if (e.methodArguments[2] && e.methodArguments[2].locked && e.methodArguments[0] && this.isChannelHidden(e.methodArguments[0].id)) e.methodArguments[2].locked = false;
return e.returnValue ? (this.isChannelHidden(e.methodArguments[0]) ? 0 : e.returnValue) : e.returnValue;
}});
this.forceUpdateAll();
@ -430,74 +399,17 @@ module.exports = (_ => {
processChannels (e) {
if (!e.instance.props.guild || blackList.includes(e.instance.props.guild.id)) return;
let [hiddenChannels, amount] = this.getHiddenChannels(e.instance.props.guild);
if (amount) {
e.instance.props.categories = Object.assign({}, e.instance.props.categories);
for (let catId in e.instance.props.categories) e.instance.props.categories[catId] = [].concat(e.instance.props.categories[catId]);
e.instance.props.channels = Object.assign({}, e.instance.props.channels);
for (let type in e.instance.props.channels) e.instance.props.channels[type] = [].concat(e.instance.props.channels[type]);
let hiddenId = e.instance.props.guild.id + "_hidden";
delete e.instance.props.categories[hiddenId];
e.instance.props.categories._categories = e.instance.props.categories._categories.filter(n => n.channel.id != hiddenId);
e.instance.props.channels[BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY] = e.instance.props.channels[BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY].filter(n => n.channel.id != hiddenId);
let index = -1;
for (let catId in e.instance.props.categories) {
if (catId != "_categories") e.instance.props.categories[catId] = e.instance.props.categories[catId].filter(n => !this.isChannelHidden(n.channel.id));
for (let channelObj of e.instance.props.categories[catId]) if (channelObj.index > index) index = parseInt(channelObj.index);
}
if (this.settings.sortOrder.hidden == sortOrders.EXTRA.value) {
hiddenCategory = new BDFDB.DiscordObjects.Channel({
guild_id: e.instance.props.guild.id,
id: hiddenId,
name: "hidden",
type: BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY
});
e.instance.props.categories[hiddenId] = [];
e.instance.props.categories._categories.push({
channel: hiddenCategory,
index: ++index
});
e.instance.props.channels[BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY].push({
comparator: (e.instance.props.channels[BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY][e.instance.props.channels[BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY].length - 1] || {comparator: 0}).comparator + 1,
channel: hiddenCategory
});
}
else hiddenCategory = null;
for (let type in hiddenChannels) {
let channelType = channelGroupMap[BDFDB.DiscordConstants.ChannelTypes[type]] || type;
if (!BDFDB.ArrayUtils.is(e.instance.props.channels[channelType])) e.instance.props.channels[channelType] = [];
for (let channel of hiddenChannels[type]) {
let hiddenChannel = new BDFDB.DiscordObjects.Channel(Object.assign({}, channel, {
parent_id: hiddenCategory ? hiddenId : channel.parent_id,
position: this.settings.sortOrder.hidden == sortOrders.BOTTOM.value ? 999999999 : channel.position
}));
let parent_id = hiddenChannel.parent_id || "null";
e.instance.props.categories[parent_id].push({
channel: hiddenChannel,
index: hiddenChannel.position
});
e.instance.props.channels[channelType].push({
comparator: hiddenChannel.position,
channel: hiddenChannel
});
}
}
for (let parent_id in e.instance.props.categories) BDFDB.ArrayUtils.keySort(e.instance.props.categories[parent_id], "index");
for (let channelType in e.instance.props.channels) BDFDB.ArrayUtils.keySort(e.instance.props.channels[channelType], "comparator");
}
}
processChannelCategoryItem (e) {
if (hiddenCategory && e.instance.props.channel && !e.instance.props.channel.id && e.instance.props.channel.type != BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY) e.instance.props.channel = hiddenCategory;
e.instance.props.guildChannels.categories = Object.assign({}, e.instance.props.guildChannels.categories);
let sortAtBottom = this.settings.sortOrder.hidden == sortOrders.BOTTOM.value;
for (let id in e.instance.props.guildChannels.categories) if (!e.instance.props.guildChannels.categories[id].isCollapsed) e.instance.props.guildChannels.categories[id].shownChannelIds = BDFDB.ObjectUtils.toArray(e.instance.props.guildChannels.categories[id].channels).filter(n => n.renderLevel == renderLevels.SHOW || n.renderLevel == renderLevels.CAN_NOT_SHOW && (!e.instance.props.guildChannels.hideMutedChannels || !e.instance.props.guildChannels.mutedChannelIds.has(n.record.id))).sort((x, y) => {
let xPos = x.record.position + (x.record.isVocal() ? 1e4 : 0) + (sortAtBottom && x.renderLevel == renderLevels.CAN_NOT_SHOW ? 1e5 : 0);
let yPos = y.record.position + (y.record.isVocal() ? 1e4 : 0) + (sortAtBottom && y.renderLevel == renderLevels.CAN_NOT_SHOW ? 1e5 : 0);
return xPos < yPos ? -1 : xPos > yPos ? 1 : 0;
}).map(n => n.id);
}
processChannelItem (e) {
if (e.instance.props.channel && !blackList.includes(e.instance.props.channel.guild_id) && this.isChannelHidden(e.instance.props.channel.id)) {
if (e.instance.props.channel && this.isChannelHidden(e.instance.props.channel.id)) {
if (!e.returnvalue) e.instance.props.className = BDFDB.DOMUtils.formatClassName(e.instance.props.className, BDFDB.disCN._showhiddenchannelshiddenchannel);
else {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: "ChannelItemIcon"});
@ -537,24 +449,15 @@ module.exports = (_ => {
isChannelHidden (channelId) {
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(channelId);
return channel && hiddenChannelCache[channel.guild_id] && hiddenChannelCache[channel.guild_id].hidden[channel.type] && hiddenChannelCache[channel.guild_id].hidden[channel.type].find(c => c.id == channel.id);
}
getHiddenChannels (guild) {
if (!guild) return [{}, 0];
let hiddenChannels = {}, visibleAmount = BDFDB.ArrayUtils.sum(BDFDB.ObjectUtils.toArray((BDFDB.LibraryModules.GuildChannelStore.getChannels(guild.id))).filter(BDFDB.ArrayUtils.is).map(n => n.length)), rolesAmount = (BDFDB.LibraryModules.MemberStore.getMember(guild.id, BDFDB.UserUtils.me.id) || {roles: []}).roles.length;
if (!hiddenChannelCache[guild.id] || hiddenChannelCache[guild.id].visible != visibleAmount || hiddenChannelCache[guild.id].roles != rolesAmount) {
let all = BDFDB.LibraryModules.ChannelStore.getMutableGuildChannelsForGuild(guild.id);
for (let type in BDFDB.DiscordConstants.ChannelTypes) hiddenChannels[BDFDB.DiscordConstants.ChannelTypes[type]] = [];
for (let channel_id in all) {
let channel = all[channel_id];
if (channel.guild_id == guild.id && channel.type != BDFDB.DiscordConstants.ChannelTypes.GUILD_CATEGORY && (this.settings.channels[BDFDB.DiscordConstants.ChannelTypes[channel.type]] || this.settings.channels[BDFDB.DiscordConstants.ChannelTypes[channel.type]] === undefined) && !BDFDB.DMUtils.isDMChannel(channel.id) && !BDFDB.UserUtils.can("VIEW_CHANNEL", BDFDB.UserUtils.me.id, channel.id)) hiddenChannels[channel.type].push(channel);
}
}
else hiddenChannels = hiddenChannelCache[guild.id].hidden;
for (let type in hiddenChannels) hiddenChannels[type] = hiddenChannels[type].filter(c => BDFDB.LibraryModules.ChannelStore.getChannel(c.id));
hiddenChannelCache[guild.id] = {hidden: hiddenChannels, amount: BDFDB.ObjectUtils.toArray(hiddenChannels).flat().length, visible: visibleAmount, roles: rolesAmount};
return [hiddenChannelCache[guild.id].hidden, hiddenChannelCache[guild.id].amount];
if (!channel || !channel.guild_id) return false;
let visibleAmount = BDFDB.ArrayUtils.sum(BDFDB.ObjectUtils.toArray((BDFDB.LibraryModules.GuildChannelStore.getChannels(channel.guild_id))).filter(BDFDB.ArrayUtils.is).map(n => n.length)), rolesAmount = (BDFDB.LibraryModules.MemberStore.getMember(channel.guild_id, BDFDB.UserUtils.me.id) || {roles: []}).roles.length;
if (!hiddenChannelCache[channel.guild_id] || hiddenChannelCache[channel.guild_id].visibleAmount != visibleAmount || hiddenChannelCache[channel.guild_id].rolesAmount != rolesAmount) hiddenChannelCache[channel.guild_id] = {
categories: BDFDB.ModuleUtils.findByName("ChannelListStore").getGuild(channel.guild_id).guildChannels.categories,
rolesAmount: rolesAmount,
visibleAmount: visibleAmount
};
let channels = hiddenChannelCache[channel.guild_id].categories[channel.parent_id || "no-parent-channel-id"].channels;
return !channels || !channels[channel.id] ? false : channels[channel.id].renderLevel == renderLevels.CAN_NOT_SHOW;
}
batchSetGuilds (settingsPanel, collapseStates, value) {
@ -577,11 +480,6 @@ module.exports = (_ => {
BDFDB.DataUtils.save(savedBlackList, this, "blacklist");
}
saveCollapseList (savedCollapseList) {
collapseList = savedCollapseList;
BDFDB.DataUtils.save(savedCollapseList, this, "categorydata");
}
openAccessModal (channel, allowed) {
let isThread = BDFDB.ChannelUtils.isThread(channel);
let guild = BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id);