Files
DRB-CnC/Client/controllers/botController.js
2023-03-26 16:12:03 -04:00

86 lines
2.7 KiB
JavaScript

// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "clientController");
// Modules
const path = require('path');
const fork = require('child_process').fork;
const discordBotPath = path.resolve('discord-bot/app.js');
const {getVoiceConnection} = require("@discordjs/voice");
const {replyToInteraction} = require("../utilities/messageHandler.js");
// Commands
const { ping } = require('../commands/ping.js');
const { join } = require('../commands/join.js');
const { leave } = require('../commands/leave.js');
/**
* Get an object of client guilds
* @param req The express request which includes the discord client
* @returns
*/
function getGuilds(req) {
return req.discordClient.guilds.cache.map(guild => guild.id)
}
/**
* Get Status of the discord process
*/
exports.getStatus = (res, req) => {
log.INFO("Getting the status of the bot");
guildIds = getGuilds(req);
log.DEBUG("Guild IDs: ", guildIds);
var guildStatuses = []
for (const guildID of guildIds){
this.status({guildID: guildID, callback: (statusObj) => {
log.DEBUG("Status Object string: ", statusObj);
if (!statusObj.voiceConnection) guildStatuses.push({ guildID : 201 });
else guildStatuses.push({ guildID: 202 })
}});
}
return res.send(200).json(guildStatuses);
}
/**
* Start the bot and join the server and preset specified
*/
exports.joinServer = (res, req) => {
const channelID = req.body.channelID;
const presetName = req.body.presetName;
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: () => {
return req.sendStatus(202);
}});
}
/**
* Leaves the server if it's in one
*/
exports.leaveServer = (res, req) => {
leave({guildID: guildID, callback: (response) => {
return res.sendStatus(202);
}});
}
exports.status = async function status({interaction= undefined, guildID= undefined, callback = undefined}) {
//if (!interaction && !guildID) // Need error of sorts
if (interaction){
guildID = interaction.guild.id;
}
const voiceConnection = getVoiceConnection(guildID);
const statusObj = {
"guildID": guildID, "voiceConnection": voiceConnection
}
//log.DEBUG('Status Object: ', statusObj);
// get the status and return it accordingly (message reply / module)
if (interaction) {
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
}
else {
callback(statusObj);
}
}