Init WIP Bot
This commit is contained in:
62
Client/discord-bot/controllers/voiceController.js
Normal file
62
Client/discord-bot/controllers/voiceController.js
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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`);
|
||||
}
|
||||
Reference in New Issue
Block a user