Init WIP Bot
This commit is contained in:
37
Client/discord-bot/controllers/audioController.js
Normal file
37
Client/discord-bot/controllers/audioController.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// Config
|
||||
import { getDeviceID, getDeviceName } from '../utilities/configHandler.js'
|
||||
// Modules
|
||||
import portAudio from 'naudiodon';
|
||||
import {createAudioResource} from "@discordjs/voice";
|
||||
|
||||
export function getAudioDevice({deviceName = undefined, deviceId = undefined}){
|
||||
const deviceList = getAudioDevices();
|
||||
if (!deviceName && !deviceId) throw new Error("No device given");
|
||||
if (deviceName) return deviceList.find(device => device.name === deviceName);
|
||||
if (deviceId) return deviceList.find(device => device.id === deviceId);
|
||||
}
|
||||
|
||||
export function getAudioDevices(){
|
||||
const deviceList = portAudio.getDevices();
|
||||
console.log("Devices:", deviceList);
|
||||
return deviceList;
|
||||
}
|
||||
|
||||
export function createAudioInstance() {
|
||||
//const resource = createAudioResource();
|
||||
const selectedDevice = getAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"});
|
||||
console.log(selectedDevice);
|
||||
// Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream
|
||||
const audioInstance = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 2,
|
||||
sampleFormat: portAudio.SampleFormat16Bit,
|
||||
sampleRate: 44100,
|
||||
deviceId: selectedDevice.id, // Use -1 or omit the deviceId to select the default device
|
||||
closeOnError: false, // Close the stream if an audio error is detected, if set false then just log the error
|
||||
framesPerBuffer: 0 // 44100 / 1000 * 120 / 2 // Get 120ms of audio
|
||||
}
|
||||
});
|
||||
//audioInstance.start();
|
||||
return audioInstance;
|
||||
}
|
||||
6
Client/discord-bot/controllers/ping.js
Normal file
6
Client/discord-bot/controllers/ping.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// Utilities
|
||||
import { replyToInteraction } from '../utilities/messageHandler.js';
|
||||
|
||||
export default function ping(interaction) {
|
||||
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
|
||||
}
|
||||
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