Bugfixing the botController
This commit is contained in:
@@ -13,6 +13,42 @@ function getGuilds(req) {
|
|||||||
return req.discordClient.guilds.cache.map(guild => guild.id)
|
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
|
* Get Status of the discord process
|
||||||
*/
|
*/
|
||||||
@@ -21,26 +57,28 @@ exports.getStatus = (req, res) => {
|
|||||||
guildIds = getGuilds(req);
|
guildIds = getGuilds(req);
|
||||||
log.DEBUG("Guild IDs: ", guildIds);
|
log.DEBUG("Guild IDs: ", guildIds);
|
||||||
var guildStatuses = []
|
var guildStatuses = []
|
||||||
for (const guildID of guildIds){
|
for (const guildId of guildIds){
|
||||||
status({guildID: guildID, callback: (statusObj) => {
|
status({guildID: guildId, callback: (statusObj) => {
|
||||||
log.DEBUG("Status Object string: ", statusObj);
|
log.DEBUG("Status Object string: ", statusObj);
|
||||||
if (!statusObj.voiceConnection) guildStatuses.push({ guildID : 201 });
|
if (!statusObj.voiceConnection) guildStatuses.push({ guildId : 201 });
|
||||||
else guildStatuses.push({ guildID: 202 })
|
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
|
* Start the bot and join the server and preset specified
|
||||||
*/
|
*/
|
||||||
exports.joinServer = (req, res) => {
|
exports.joinServer = (req, res) => {
|
||||||
const channelID = req.body.channelID;
|
const channelId = req.body.channelID;
|
||||||
const presetName = req.body.presetName;
|
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"});
|
if (!channelId || !presetName || !guildId) return res.status(400).json({'message': "Request does not have all components to proceed"});
|
||||||
// Start the bot
|
|
||||||
join({guildID: guildID, guildObj: client.guilds.cache.get(guildID), channelID: channelID, callback: () => {
|
// join the sever
|
||||||
|
join({guildID: guildId, guildObj: client.guilds.cache.get(guildId), channelID: channelId, callback: () => {
|
||||||
return req.sendStatus(202);
|
return req.sendStatus(202);
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
@@ -49,7 +87,11 @@ exports.joinServer = (req, res) => {
|
|||||||
* Leaves the server if it's in one
|
* Leaves the server if it's in one
|
||||||
*/
|
*/
|
||||||
exports.leaveServer = (req, res) => {
|
exports.leaveServer = (req, res) => {
|
||||||
leave({guildID: guildID, callback: (response) => {
|
const guildIds = getGuilds(req);
|
||||||
return res.sendStatus(202);
|
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);
|
||||||
|
}});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user