Search code examples
discorddiscord.jscommandvolume

Volume command in discord.js


Ive made the command and it works but it doesn't change the volume of the bot it keeps saying "Volume only can be set in a range of `1` - `100`" but i typed "volume 1 and it didnt work

Command -

 } else if (message.content.startsWith('"volume')) {
        const args = message.content
        if (!message.member.voice.channel) return message.channel.send("I'm sorry, but you need to be in a voice channel to set a volume!");
        if (!serverQueue) return message.channel.send("There is nothing playing");
        if (!args[1]) return message.channel.send(`The current volume is: **\`${!serverQueue.volume}%\`**`);
        if (isNaN(args[1]) || args[1] > 100) return message.channel.send("Volume only can be set in a range of **\`1\`** - **\`100\`**");
        serverQueue.volume = args[1];
        serverQueue.connection.dispatcher.setVolume(args[1] / 100);
        return message.channel.send(`I set the volume to: **\`${args[1]}%\`**`);
        return;

Functions -

 const dispatcher = serverQueue.connection
        .play(ytdl(song.url))
        .on("finish", () => {
            if (!serverQueue.loop) serverQueue.songs.shift()
            play(guild, serverQueue.songs[0]);
        })
        .on("error", error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    serverQueue.textChannel.send(`Started playing: **${song.title}**`)

Solution

  • Isn't it because you are missing .split() when you are assigning to args?

    const args = message.content.split(" ");
    

    It will break the message content by spaces into an array. Your commands would look like this "volume 5.