Files
DRB-CnC/Client/discord-bot/commands/join.js
Logan Cusano e5d885cc3e Logging Update
- Changed to uniform logging with the 'debug' module
- Updated all apps
2022-12-11 21:11:29 -05:00

36 lines
1.3 KiB
JavaScript

import {joinVoiceChannel} from "@discordjs/voice";
import {replyToInteraction} from "../utilities/messageHandler.js";
import {createAudioInstance} from "../controllers/audioController.js";
import OpusEncoderPkg from "@discordjs/opus";
// Declare the encoder (module is incompatible modern import method)
const { OpusEncoder } = OpusEncoderPkg;
const encoder = new OpusEncoder(48000, 2);
/**
* Join the specified voice channel
*
* @param interaction Message interaction from discord
*/
export default async function join(interaction){
const voiceChannel = interaction.options.getChannel('voicechannel');
const voiceConnection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator,
selfMute: false,
selfDeaf: false,
});
replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
const audioInstance = createAudioInstance();
audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer);
const encoded = encoder.encode(buffer);
// TODO Add a function here to check the volume of either buffer and only play audio to discord when there is audio to be played
voiceConnection.playOpusPacket(encoded);
})
audioInstance.start();
}