diff --git a/Client/controllers/botController.js b/Client/controllers/botController.js index 3c218d2..ae62246 100644 --- a/Client/controllers/botController.js +++ b/Client/controllers/botController.js @@ -13,6 +13,42 @@ function getGuilds(req) { return req.discordClient.guilds.cache.map(guild => guild.id) } +/** + * Get an object of the channels in a guild + * @param {*} guildId The Guild ID to check the channels of + * @param {*} req The request object to use to check the discord client + */ +function getChannels(guildId, req) { + const guild = req.discordClient.guilds.cache.get(guildId); + return guild.channels; +} + +/** + * Check to see if a given guild has a given channel + * @param {*} guildId The guild ID to check if the channel exists + * @param {*} channelId The channel ID to check if exists in the guild + * @param {*} req The express request param to use the discord client + * @returns {true|false} + */ +function checkIfGuildHasChannel(guildId, channelId, req){ + const guildChannels = getChannels(guildId, req) + const checkedChannel = guildChannels.find(c => c.id === channelId); + if (!checkedChannel) return false; + + return true; +} + +function getGuildFromChannel(channelId, req){ + const guilds = getGuilds(req); + for (const guild in guilds){ + if (checkIfGuildHasChannel(guild.id, channelId, req)) { + return guild; + break; + } + } + return new Error("No Guild found with the given ID"); +} + /** * Get Status of the discord process */ @@ -21,26 +57,28 @@ exports.getStatus = (req, res) => { guildIds = getGuilds(req); log.DEBUG("Guild IDs: ", guildIds); var guildStatuses = [] - for (const guildID of guildIds){ - status({guildID: guildID, callback: (statusObj) => { + for (const guildId of guildIds){ + status({guildID: guildId, callback: (statusObj) => { log.DEBUG("Status Object string: ", statusObj); - if (!statusObj.voiceConnection) guildStatuses.push({ guildID : 201 }); - else guildStatuses.push({ guildID: 202 }) + if (!statusObj.voiceConnection) guildStatuses.push({ guildId : 201 }); + else guildStatuses.push({ guildId: 202 }) }}); } - return res.send(200).json(guildStatuses); + return res.status(200).json(guildStatuses); } /** * Start the bot and join the server and preset specified */ exports.joinServer = (req, res) => { - const channelID = req.body.channelID; + const channelId = req.body.channelID; const presetName = req.body.presetName; + const guildId = getGuildFromChannel(channelId, req); - if (!channelID || !presetName) return res.status(400).json({'message': "Channel ID or Preset Name not present in the request"}); - // Start the bot - join({guildID: guildID, guildObj: client.guilds.cache.get(guildID), channelID: channelID, callback: () => { + if (!channelId || !presetName || !guildId) return res.status(400).json({'message': "Request does not have all components to proceed"}); + + // join the sever + join({guildID: guildId, guildObj: client.guilds.cache.get(guildId), channelID: channelId, callback: () => { return req.sendStatus(202); }}); } @@ -49,7 +87,11 @@ exports.joinServer = (req, res) => { * Leaves the server if it's in one */ exports.leaveServer = (req, res) => { - leave({guildID: guildID, callback: (response) => { - return res.sendStatus(202); - }}); + const guildIds = getGuilds(req); + for (const guildId of GuildIds){ + leave({guildID: guildId, callback: (response) => { + log.DEBUG("Response from leaving server on guild ID", guildId, response); + return res.sendStatus(202); + }}); + } } \ No newline at end of file