From eac9895c28d0e19e6d5f2bcc1b9db74137fa7941 Mon Sep 17 00:00:00 2001 From: Farcrada Withman Date: Mon, 7 Jun 2021 11:42:32 +0000 Subject: [PATCH] Resolving console errors There was some funky shit with uncaught promises and fetching just straight up being a bitch. Seems to be resolved now. Also, there's an issue when there are more users connected than there are slots (i.e. exceeding the imposed limit on a voice channel) More about that here: https://github.com/Farcrada/DiscordPlugins/issues/29 --- Right-Click-Join/RightClickJoin.plugin.js | 123 ++++++++++------------ 1 file changed, 58 insertions(+), 65 deletions(-) diff --git a/Right-Click-Join/RightClickJoin.plugin.js b/Right-Click-Join/RightClickJoin.plugin.js index 35426a1..773bae9 100644 --- a/Right-Click-Join/RightClickJoin.plugin.js +++ b/Right-Click-Join/RightClickJoin.plugin.js @@ -1,7 +1,7 @@ /** * @name RightClickJoin * @author Farcrada - * @version 1.1.1 + * @version 1.1.2 * @description Right click a user to join a voice channel they are in. * * @website https://github.com/Farcrada/DiscordPlugins @@ -15,7 +15,7 @@ const config = { name: "Right Click Join", id: "RightClickJoin", description: "Right click a user to join a voice channel they are in.", - version: "1.1.1", + version: "1.1.2", author: "Farcrada", updateUrl: "https://raw.githubusercontent.com/Farcrada/DiscordPlugins/master/Right-Click-Join/RightClickJoin.plugin.js" } @@ -84,11 +84,12 @@ function createCache() { RightClickJoin.getVoiceStatesForChannel = BdApi.findModuleByProps("getAllVoiceStates", "getVoiceStatesForChannel").getVoiceStatesForChannel; RightClickJoin.getChannels = BdApi.findModuleByProps("getChannels", "getDefaultChannel").getChannels; RightClickJoin.selectVoiceChannel = BdApi.findModuleByProps("selectChannel").selectVoiceChannel; - RightClickJoin.getMutualGuilds = BdApi.findModuleByProps('isFetching', 'getUserProfile').getMutualGuilds; + RightClickJoin.fetchProfile = BdApi.findModuleByProps("open", "fetchProfile").fetchProfile; //GuildStore RightClickJoin.GuildStore = BdApi.findModuleByProps("getGuild", "getGuilds"); RightClickJoin.ChannelStore = BdApi.findModuleByProps("getChannel", "getDMFromUserId"); + RightClickJoin.MutualStore = BdApi.findModuleByProps("isFetching", "getUserProfile"); //React and shit RightClickJoin.ce = BdApi.React.createElement; @@ -106,7 +107,7 @@ function patchGuildChannelUserContextMenu() { if (channel.isVocal()) //if we right click a channel in the list, we can mitigate our intense searching. - checkChannelMenuItem(channel, props.user.id, returnValue, indexObject) + constructMenuItem(props.user.id, returnValue, indexObject, channel.id) else //Drop right into it for the guilds; Don't have to catch the return either. checkMenuItem(RightClickJoin.getChannels(props.guildId).VOCAL, props.user.id, returnValue, indexObject) @@ -118,78 +119,70 @@ function patchDMUserContextMenu() { //Patch in our context item under our name BdApi.Patcher.after(config.info.id, RightClickJoin.dmUserContextMenu, "default", (that, [props], returnValue) => { //Enter the world of patching - - //Now we gotta check mutual guilds to see if we can find anything. - let mutualGuilds = RightClickJoin.getMutualGuilds(props.user.id); + + //Now we gotta check mutual guilds to see if we match anything + let userId = props.user.id; + // Dumb shit, null checking 'n all. + let mutualGuilds = RightClickJoin.MutualStore.getMutualGuilds(userId) ?? []; let indexObject = { section: 2, child: 1 }; - if (typeof (mutualGuilds) === "undefined") - return; + if (mutualGuilds.length < 1) { + //Gotta make sure we're not fetching already (or risk spamming the API) + if (RightClickJoin.MutualStore.isFetching(userId)) + return; - //So we need a loop through if there's many - for (let i = 0; i < mutualGuilds.length; i++) { - //We need to have a way to break early if we found anything - //You can only be connected to one voicechannel anyway. - let result = checkMenuItem(RightClickJoin.getChannels(mutualGuilds[i].guild.id).VOCAL, props.user.id, returnValue, indexObject); - if (result) - break; + //Fetch and then we need to fill "mutualGuilds" again, so we just pass the call + RightClickJoin.fetchProfile(userId).then(dmPatchHandler(RightClickJoin.MutualStore.getMutualGuilds(userId))); + } + else + dmPatchHandler(mutualGuilds) + + function dmPatchHandler(_mutualGuilds = []) { + //So we need a loop through if there's many + for (let i = 0; i < _mutualGuilds.length; i++) + //We need to have a way to break early + //if we find anything to reduce resource consumption + //You can only be connected to one voicechannel anyway + if (checkMenuItem(RightClickJoin.getChannels(_mutualGuilds[i].guild.id).VOCAL, userId, returnValue, indexObject)) + break; } }); - } -function constructMenuItem(returnValue, indexObject, channelId) { - //Splice and insert our context item - // the menu, the sections, the items of this section - returnValue.props.children.props.children[indexObject.section].props.children.splice( - //We want it after the "call" option. - indexObject.child, - 0, - RightClickJoin.ce(RightClickJoin.MenuItem, { - //Discord Is One Of Those - label: "Join Call", - id: config.info.name.toLowerCase().replace(' ', '-'), - action: () => { - //Joining a voicechannel - RightClickJoin.selectVoiceChannel(channelId); - } - }) - ); +function checkMenuItem(voiceChannels, userId, returnValue, indexObject) { + //Gotta make sure this man is actually in a voice call + //Loopy whoop + for (let i = 0; i < voiceChannels.length; i++) + if (constructMenuItem(userId, returnValue, indexObject, voiceChannels[i].channel.id)) + return true; + //Return false so our DM patch knows what to do. + return false; } -function checkChannelMenuItem(channel, userId, returnValue, indexObject) { - //Gotta make sure this man is actually in a voice call, - //otherwise this is a wasted effort. - +function constructMenuItem(userId, returnValue, indexObject, channelId) { //Get all the participants in this voicechannel - let participants = RightClickJoin.getVoiceStatesForChannel(channel.id); - + let participants = RightClickJoin.getVoiceStatesForChannel(channelId); //Loopy doop for (let id in participants) //If a matching participant is found, engage - if (participants[id].userId === userId) - constructMenuItem(returnValue, indexObject, channel.id); -} - -function checkMenuItem(voiceChannels, userId, returnValue, indexObject) { - //Gotta make sure this man is actually in a voice call, - //otherwise this is a wasted effort. - - //Loopy whoop - for (let i = 0; i < voiceChannels.length; i++) { - //Get all the participants in this voicechannel - let channelId = voiceChannels[i].channel.id; - let participants = RightClickJoin.getVoiceStatesForChannel(channelId); - - //Loopy doop - for (let id in participants) - //If a matching participant is found, engage - if (participants[id].userId === userId) { - constructMenuItem(returnValue, indexObject, channelId); - //Return entirely, since only one voicechannel is possible. - return true; - } - } - //Return false so our DM patch knows what to do. - return false; + if (participants[id].userId === userId) { + //Splice and insert our context item + // the menu, the sections, the items of this section + returnValue.props.children.props.children[indexObject.section].props.children.splice( + //We want it after the "call" option. + indexObject.child, + 0, + RightClickJoin.ce(RightClickJoin.MenuItem, { + //Discord Is One Of Those + label: "Join Call", + id: config.info.name.toLowerCase().replace(' ', '-'), + action: () => { + //Joining a voicechannel + RightClickJoin.selectVoiceChannel(channelId); + } + }) + ); + //Return entirely, since only one voicechannel is possible. + return true; + } }