87 lines
2.7 KiB
JavaScript
87 lines
2.7 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(48000, 2);
|
|
|
|
const player = createAudioPlayer({
|
|
behaviors: {
|
|
noSubscriber: NoSubscriberBehavior.Play,
|
|
},
|
|
});
|
|
|
|
const audioInstance = createAudioInstance();
|
|
|
|
audioInstance.on('data', buffer => {
|
|
buffer = Buffer.from(buffer);
|
|
const encoded = encoder.encode(buffer);
|
|
voiceConnection.playOpusPacket(encoded);
|
|
})
|
|
|
|
audioInstance.start();
|
|
}
|
|
|
|
/**
|
|
* 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`);
|
|
}
|
|
|
|
/*
|
|
* Brute forcing the required buffer size, 16 bit, 48KHz, 2ch = 480 Bytes (need to get frames still)
|
|
let notWorking = true;
|
|
let splitIndexInverse = 0
|
|
while (notWorking) {
|
|
try {
|
|
const newBuffer = buffer.slice(buffer.length - splitIndexInverse);
|
|
console.log(timestamp ,newBuffer, "end")
|
|
const encoded = encoder.encode(newBuffer);
|
|
console.log("Working Buffer Length" ,newBuffer.length)
|
|
console.log("Working Buffer" ,newBuffer)
|
|
notWorking = false
|
|
break;
|
|
} catch (err){
|
|
console.log(err)
|
|
if (splitIndexInverse >= buffer.length) {
|
|
notWorking = false
|
|
throw new Error("Shit fucked")
|
|
}
|
|
splitIndexInverse += 1
|
|
}
|
|
}
|
|
*/ |