SquogAdmin/commands/voice.js
2025-05-22 20:39:02 +07:00

74 lines
2.4 KiB
JavaScript

const {createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection} = require("@discordjs/voice")
const { Message } = require("discord.js")
const {client, dir} = require("../index")
const YTDlpWrap = require("yt-dlp-wrap").default
const ytDlp = new YTDlpWrap(`${dir}/yt-dlp`)
module.exports = [
{
name: "play",
description: "Hello tested for now",
arguments: ["link"], // If its not a OBJECT type you provide it like that lol.
run: async (ctx, link) => {
// Getting the guild member to get their voice client
const author = ctx.author
const member = await ctx.guild.members.fetch(author)
// Initiating the channel variable (undefined)
var channel
// Checking if member is in a voice chat
if (member.voice) {
channel = member.voice.channel
}
// Returns
if (!channel) {
return ctx.reply("You are not in a VC right now.")
} else if (ctx.guild.members.me.voice.channel && ctx.guild.members.me.voice.channel.id != channel.id) {
return ctx.reply("Already in a vc.")
}
var connection
// Checking if the bot is already in the vc
if (!ctx.guild.members.me.voice.channel) {
// Connecting to voice channel
connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
})
} else {
// Getting a existing connection
connection = getVoiceConnection(ctx.guild.id)
}
console.log(connection)
const metadata = await ytDlp.getVideoInfo(link);
ctx.reply(`Playing ${metadata.title}`)
const music = ytDlp.exec([
link,
'-f',
'best',
'-o',
`${dir}/music/${metadata.title}.mp3`,
'--extract-audio',
'--audio-format',
'mp3'
]).on('close', () => {
const player = createAudioPlayer()
const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`)
player.play(resource)
connection.subscribe(player)
})
}
}
]