const {SlashCommandBuilder} = require('discord.js'); const {generateDependencyReport, entersState, VoiceConnectionStatus, getVoiceConnection, createAudioResource, joinVoiceChannel, createAudioPlayer, AudioPlayerStatus} = require('@discordjs/voice'); const fs = require("node:fs"); module.exports={ data: new SlashCommandBuilder().setName("join").setDescription("joins the same voice channel as the executing user"), async run(c, i){ if(i.user.id != "866315117926940682"){ return await i.reply("Only <@866315117926940682> can use this command!"); } const connection = joinVoiceChannel({channelId: '1137707126530261006', guildId:i.guildId, adapterCreator:i.guild.voiceAdapterCreator}); //const connectio = getVoiceConnection(i.guildId); connection.on(VoiceConnectionStatus.Ready, (oldState, newState) => { console.log('Connection is in the Ready state!'); }); const player = createAudioPlayer(); const resource = createAudioResource("C:/Users/saksh/Downloads/correct-156911.mp3", {inlineVolume:true}); resource.volume.setVolume(1.0); console.log(generateDependencyReport()); try { await entersState(connection, VoiceConnectionStatus.Ready, 30e3); console.log("Connected: " + connection); player.play(resource, {seek: 0, volume:1.0}); console.log("player started playing!") player.on(AudioPlayerStatus.Playing, () => { console.log('Playing'); }) player.on('error', error => { console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`); player.play(getNextResource()); }); const sub = connection.subscribe(player); if(sub){ console.log("sub created!"); setTimeout(function(){ sub.unsubscribe(); }, 5_000); } }catch(error) { console.log("Voice Connection not ready within 5s.", error); return await i.reply("not working"); }finally{ player.stop(); connection.destroy(); } } }