From 22a31762f36e02080d4cf12cb602da694042a750 Mon Sep 17 00:00:00 2001 From: entar Date: Fri, 23 May 2025 23:03:10 +0700 Subject: [PATCH] Added looping to music and fixed Boolean argument ( true | false ) --- commands/voice.js | 25 +++++++++++++++++++------ index.js | 7 ++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/commands/voice.js b/commands/voice.js index e56c0df..ca9f645 100644 --- a/commands/voice.js +++ b/commands/voice.js @@ -1,6 +1,7 @@ const {createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection} = require("@discordjs/voice") -const { Message } = require("discord.js") +const { Message, VoiceState } = require("discord.js") const {client, dir} = require("../index") +const { AudioPlayerStatus } = require("@discordjs/voice") const YTDlpWrap = require("yt-dlp-wrap").default const ytDlp = new YTDlpWrap(`${dir}/yt-dlp`) @@ -11,8 +12,8 @@ module.exports.commands = [ { name: "play", description: "Joins your VC and plays music by youtube link.", - arguments: ["link"], // If its not a OBJECT type you provide it like that lol. - run: async (ctx, link) => { + arguments: ["link", false], // If its not a OBJECT type you provide it like that lol. + run: async (ctx, link, loop) => { // Getting the guild member to get their voice client const author = ctx.author const member = await ctx.guild.members.fetch(author) @@ -24,7 +25,7 @@ module.exports.commands = [ if (member.voice) { channel = member.voice.channel } - + // Returns if the user is not in vc or the bot is already in one if (!channel) { return ctx.reply("You are not in a VC right now.") @@ -81,8 +82,7 @@ module.exports.commands = [ const player = createAudioPlayer() players[ctx.guild.id] = player - - // Loading music + const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`) // Playing the music with the player @@ -90,6 +90,17 @@ module.exports.commands = [ // Subscribing the connection to the player connection.subscribe(player) + + if (loop) { + player.on("stateChange", (oldState, newState) => { + if (newState.status == AudioPlayerStatus.Idle && players[ctx.guild.id] != undefined) { + // Gotta create a new resource every time + const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`) + + player.play(resource) + } + }) + } }) } }, @@ -104,6 +115,8 @@ module.exports.commands = [ } players[ctx.guild.id].stop() + + players[ctx.guild.id] = undefined } }, { diff --git a/index.js b/index.js index 516f414..c007561 100644 --- a/index.js +++ b/index.js @@ -117,6 +117,7 @@ client.on("messageCreate", async (message) => { // Parsing args.forEach((arg, index) => { + if (arg.startsWith("<@")) { // User, lets get the id const id = arg.slice(2, arg.length - 1) @@ -137,10 +138,10 @@ client.on("messageCreate", async (message) => { } } else if (Number(arg)) { args[index] = Number(arg) - } else if (Boolean(arg)) { - args[index] = Boolean(arg) + } else if (arg == "true" || arg == "false") { + args[index] = arg == "true" // This is awful man. } - + // Checks if the type is wrong if (typeof args[index] != typeof realCommand.arguments[index] && !(args[index] instanceof realCommand.arguments[index])) { wrongArg = true // So it cant continue