Files
DRB-CnC/Client/discord-bot/controllers/voiceController.js
Logan Cusano 4e1b82c557 Init WIP Bot
2022-12-11 06:09:25 -05:00

62 lines
1.9 KiB
JavaScript

// Modules
import {
joinVoiceChannel,
getVoiceConnection,
createAudioResource,
createAudioPlayer,
NoSubscriberBehavior, StreamType
} from '@discordjs/voice';
import OpusEncoderPkg from "@discordjs/opus";
const { OpusEncoder } = OpusEncoderPkg;
// Utilities
import { replyToInteraction } from '../utilities/messageHandler.js';
import { createAudioInstance } from "./audioController.js";
/**
* Join the specified voice channel
*
* @param interaction Message interaction from discord
*/
export 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}`);
// Declare the encoder
const encoder = new OpusEncoder(44100, 2);
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
},
});
const audioInstance = createAudioInstance();
const audioResource = createAudioResource(audioInstance, { inputType: StreamType.Raw });
audioInstance.start();
//audioInstance.on('data', buffer => {
// Do on buffer event
//})
player.play(audioResource);
voiceConnection.subscribe(player);
}
/**
* If in a voice channel for the specified guild, leave
*
* @param interaction Message interaction from discord
*/
export function leave(interaction){
const guildId = interaction.guild.id;
const voiceConnection = getVoiceConnection(guildId);
if (!voiceConnection) return replyToInteraction(interaction, "Not in a voice channel.");
voiceConnection.destroy();
return replyToInteraction(interaction, `Goodbye`);
}