From f5076d40ccacf708776ba8590ce7b106bd7cb36c Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 26 Mar 2023 20:14:23 -0400 Subject: [PATCH] Improve guildFromChannel --- Client/controllers/botController.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Client/controllers/botController.js b/Client/controllers/botController.js index c353668..afda7d8 100644 --- a/Client/controllers/botController.js +++ b/Client/controllers/botController.js @@ -19,7 +19,7 @@ function getGuilds(req) { * @param {*} req The request object to use to check the discord client */ function getChannels(guildId, req) { - const guild = req.discordClient.guilds.cache.get(guildId); + const guild = req.discordClient.guilds.find(guildId); log.DEBUG("Found Guild channels with guild", guild.channels, guild); return guild.channels; } @@ -40,13 +40,12 @@ function checkIfGuildHasChannel(guildId, channelId, req){ } function getGuildFromChannel(channelId, req){ - const guilds = getGuilds(req); - for (const guild in guilds){ - if (checkIfGuildHasChannel(guild.id, channelId, req)) { - return guild; - break; - } - } + const channel = req.discordClient.channels.cache.get(channelId); + + if (!channel) return undefined; + + if (channel.guild) return channel.guild; + return new Error("No Guild found with the given ID"); }