Files
DRB-CnC/Client/controllers/commandController.js
Logan Cusano 4fbed169ab Fixed audio stopping bug
- Added basic noisegate
- Handling more voice connection statuses
2023-05-06 14:09:09 -04:00

163 lines
6.2 KiB
JavaScript

require('dotenv').config();
// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "commandController");
// Modules
const { joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require("@discordjs/voice");
const { OpusEncoder } = require("@discordjs/opus");
const { calcRmsSync } = require("../utilities/rmsCalculator.js");
// Utilities
const {replyToInteraction} = require("../utilities/messageHandler.js");
const {createAudioInstance} = require("../controllers/audioController.js");
const { all } = require('../routes/index.js');
// Declare the encoder
const encoder = new OpusEncoder(48000, 2);
const noiseGateOpen = process.env.AUDIO_NOISE_GATE_OPEN ?? 100;
/**
* Join the specified voice channel
*
* @param interaction Message interaction from discord
* @param {string||any} guildID The specified Guild ID if this function is run from the client instead of from an interaction in Discord
* @param {string||any} channelID The channel ID to join
* @param guild The guild object to be used to create a voice adapter
* @param {function} callback The callback that will be needed if this function is run with a Guild ID instead of an interaction
*/
exports.join = async function join({interaction= undefined, guildID= undefined, channelID = undefined, guildObj = undefined, callback = undefined}){
if (interaction){
const voiceChannel = interaction.options.getChannel('voicechannel');
channelID = voiceChannel.id;
guildID = interaction.guildId;
guildObj = interaction.guild;
if (interaction) replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
}
log.DEBUG("Channel ID: ", channelID)
log.DEBUG("Guild ID: ", guildID)
const vcConnectionObj = {
channelId: channelID,
guildId: guildID,
adapterCreator: guildObj.voiceAdapterCreator,
selfMute: false,
selfDeaf: false,
};
// Join the voice channel
const voiceConnection = await joinVoiceChannel(vcConnectionObj);
// Create the audio stream instance
const audioInstance = await createAudioInstance();
log.VERBOSE("Audio Instance: ", audioInstance);
// Play audio data when it's received from the stream
audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer);
//log.VERBOSE("Audio buffer: ", buffer);
// Check intensity of audio and only play when audio is present (no white noise/static)
volume = Math.trunc(calcRmsSync(buffer, buffer.length));
if (parseInt(volume) > parseInt(noiseGateOpen)) {
//log.VERBOSE("Noisegate and buffer volume: ", (parseInt(volume) > parseInt(noiseGateOpen)), noiseGateOpen, volume);
const encodedBuffer = encoder.encode(buffer);
voiceConnection.playOpusPacket(encodedBuffer);
}
});
audioInstance.start();
// Handle state changes in the voice connection
voiceConnection.on('stateChange', async (oldState, newState) => {
//log.VERBOSE("VoiceConnection state Changed from state: ", oldState, "\n\nto state: ", newState);
log.DEBUG("VoiceConnection state Changed from: ", oldState.status, "to: ", newState.status);
// Ready -> Connecting
if (oldState.status === VoiceConnectionStatus.Ready && newState.status === VoiceConnectionStatus.Connecting) {
log.DEBUG("Configuring Network");
voiceConnection.configureNetworking();
return;
}
// Ready -> Disconnected
if (oldState.status === VoiceConnectionStatus.Ready && newState.status === VoiceConnectionStatus.Disconnected) {
log.DEBUG("Attempting to reconnect the voice connection");
voiceConnection = joinVoiceChannel(vcConnectionObj);
return;
}
// Ready
if (newState.status === VoiceConnectionStatus.Ready){
log.DEBUG("Bot connected to voice channel");
return;
}
// Destroyed
if (newState.status === VoiceConnectionStatus.Destroyed){
log.DEBUG("Quitting audio instance");
audioInstance.quit();
return;
}
});
voiceConnection.on('error', async (error) => {
log.ERROR("Voice Connection Error: ", error);
})
if (guildID && callback) return callback();
else return;
}
/**
* If in a voice channel for the specified guild, leave
*
* @param interaction Message interaction from discord
* @param guildID
* @param callback
*/
exports.leave = async function leave({interaction = undefined, guildID= undefined, callback = undefined}) {
if(interaction) {
guildID = interaction.guild.id;
}
const voiceConnection = getVoiceConnection(guildID);
let response;
if (!voiceConnection){
response = "Not in a voice channel."
if (interaction) return replyToInteraction(interaction, response);
else callback(response);
}
voiceConnection.destroy();
response = "Goodbye"
if (interaction) return replyToInteraction(interaction, response);
else callback(response);
}
/**
* Get the voice status of the bots
* @param {*} param0
* @returns
*/
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": typeof g !== 'undefined' ? true : false // True if there is a voice connection, false if undefined
}
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);
}
}