Functioning audio bot

This commit is contained in:
Logan Cusano
2022-12-11 20:10:36 -05:00
parent 9ce846f1d9
commit ca9463856b
10 changed files with 142 additions and 122 deletions

View File

@@ -0,0 +1,35 @@
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);
voiceConnection.playOpusPacket(encoded);
})
audioInstance.start();
}

View File

@@ -0,0 +1,18 @@
import {getVoiceConnection} from "@discordjs/voice";
import {replyToInteraction} from "../utilities/messageHandler.js";
// Debug
//import debugBuilder from "../utilities/debugBuilder.js";
//const log = new debugBuilder("bot", "leave");
/**
* If in a voice channel for the specified guild, leave
*
* @param interaction Message interaction from discord
*/
export default async 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`);
}

View 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!");
}