Fix guild related components (@Strencher)

- Fixes #1074
- Fixes #1055
This commit is contained in:
Zack Rauen 2021-10-22 14:01:27 -04:00
parent c616495f55
commit f53fe0ecb1
2 changed files with 56 additions and 47 deletions

View File

@ -10,10 +10,9 @@ export default new class PublicServers extends Builtin {
get id() {return "publicServers";} get id() {return "publicServers";}
enabled() { enabled() {
const GuildList = WebpackModules.find(m => m.type && m.type.displayName == "NavigableGuilds"); const GuildList = WebpackModules.find(m => m.type && m.type.toString().includes("guildsnav"));
const GuildListOld = WebpackModules.findByDisplayName("Guilds"); if (!GuildList) this.warn("Can't find GuildList component");
if (!GuildList && !GuildListOld) this.warn("Can't find GuildList component"); this.guildPatch = this.after(GuildList, "type", () => {this._appendButton();});
this.guildPatch = this.after(GuildList ? GuildList : GuildListOld.prototype, GuildList ? "type" : "render", () => {this._appendButton();});
this._appendButton(); this._appendButton();
} }

View File

@ -30,6 +30,8 @@ export default new class ComponentPatcher {
debug(...message) {return Logger.debug("ComponentPatcher", ...message);} debug(...message) {return Logger.debug("ComponentPatcher", ...message);}
initialize() { initialize() {
this.guildsClasses = WebpackModules.getByProps("downloadProgressCircle", "guilds");
Utilities.suppressErrors(this.patchSocial.bind(this), "BD Social Patch")(); Utilities.suppressErrors(this.patchSocial.bind(this), "BD Social Patch")();
Utilities.suppressErrors(this.patchGuildPills.bind(this), "BD Guild Pills Patch")(); Utilities.suppressErrors(this.patchGuildPills.bind(this), "BD Guild Pills Patch")();
@ -77,61 +79,69 @@ export default new class ComponentPatcher {
}); });
} }
isGuildMuted(guildId) { async forceUpdateGuilds() {
if (!MutedStore || typeof(MutedStore.isMuted) !== "function") return false; const guildClasses = this.guildsClasses;
if (!guildClasses) return this.warn("Failed to get guilds classes!");
return MutedStore.isMuted(guildId); const guilds = await new Promise((resolve) => Utilities.onAdded(`.${guildClasses.guilds}`, resolve));
const instance = Utilities.getOwnerInstance(guilds);
if (!instance) return this.error("Failed to get Guilds instance.");
instance.forceUpdate();
} }
/** /**
* @updated 07.07.2021 * @updated 24.09.2021
*/ */
async patchGuildListItems() { patchGuildListItems() {
if (this.guildListItemsPatch) return; if (this.guildListItemsPatch) return;
const guildClasses = WebpackModules.getByProps("downloadProgressCircle", "guilds"); const GuildComponents = WebpackModules.getByProps("HubGuild");
if (!guildClasses) return this.warn("Failed to get guilds classes!");
const start = Date.now();
const guilds = await new Promise((resolve) => Utilities.onAdded(`.${guildClasses.guilds}`, resolve));
if (!guilds) return this.error("Cannot find guilds component.");
const reactInstance = Utilities.getReactInstance(guilds);
if (!reactInstance) return this.error("Failed to get Guilds instance.");
const GuildComponent = await new Promise((resolve) => {
let tries = 0;
const searchForGuild = function () {
tries++;
const guild = Utilities.findInTree(reactInstance, e => e?.type?.displayName === "Guild", {walkable: ["child", "sibling"]});
if (guild) {resolve(guild);}
else if (tries < 10) {setTimeout(searchForGuild, 300);}
else {resolve(null);}
};
searchForGuild();
});
if (!GuildComponent || typeof(GuildComponent.type) !== "function") return this.error("Failed to get Guild component."); if (!GuildComponents || typeof(GuildComponents.default) !== "function") return this.error("Failed to get Guild component.");
this.debug(`Found Guild component in ${Date.now() - start}ms`);
const Guild = GuildComponent.type; const isGuildMuted = function (guildId) {
this.guildListItemsPatch = Patcher.after("ComponentPatcher", Guild.prototype, "render", (thisObject, _, returnValue) => { if (!MutedStore || typeof(MutedStore.isMuted) !== "function") return false;
if (!returnValue || !thisObject) return;
const guildData = thisObject.props; return MutedStore.isMuted(guildId);
returnValue.props.className += " bd-guild"; };
if (guildData.unread) returnValue.props.className += " bd-unread";
if (guildData.selected) returnValue.props.className += " bd-selected"; function BDGuild(props) {
if (guildData.audio) returnValue.props.className += " bd-audio"; const {originalGuild, ...guildProps} = props;
if (guildData.video) returnValue.props.className += " bd-video"; const returnValue = Reflect.apply(originalGuild, this, [guildProps]);
if (guildData.badge) returnValue.props.className += " bd-badge";
if (guildData.animatable) returnValue.props.className += " bd-animatable"; try {
if (guildData.unavailable) returnValue.props.className += " bd-unavailable"; returnValue.props.className += " bd-guild";
if (guildData.screenshare) returnValue.props.className += " bd-screenshare"; if (guildProps.unread) returnValue.props.className += " bd-unread";
if (guildData.liveStage) returnValue.props.className += " bd-live-stage"; if (guildProps.selected) returnValue.props.className += " bd-selected";
if (this.isGuildMuted(guildData.guild.id)) returnValue.props.className += " bd-muted"; if (guildProps.audio) returnValue.props.className += " bd-audio";
if (guildProps.video) returnValue.props.className += " bd-video";
if (guildProps.badge) returnValue.props.className += " bd-badge";
if (guildProps.animatable) returnValue.props.className += " bd-animatable";
if (guildProps.unavailable) returnValue.props.className += " bd-unavailable";
if (guildProps.screenshare) returnValue.props.className += " bd-screenshare";
if (guildProps.liveStage) returnValue.props.className += " bd-live-stage";
if (isGuildMuted(guildProps.guild.id)) returnValue.props.className += " bd-muted";
}
catch (err) {
Logger.error("ComponentPatcher:Guilds", `Error inside BDGuild:`, err);
}
return returnValue; return returnValue;
}
this.guildListItemsPatch = Patcher.after("ComponentPatcher", GuildComponents, "default", (_, args, returnValue) => {
if (!returnValue || !returnValue.props) return;
const original = returnValue.type;
Object.assign(returnValue.props, {
originalGuild: original
});
returnValue.type = BDGuild;
}); });
if (reactInstance.forceUpdate) reactInstance.forceUpdate(); this.forceUpdateGuilds();
} }
patchGuildPills() { patchGuildPills() {