Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord music bot saying I need to send a second argument even when i do

Whats wrong with my code, I cant seem to figure it out. Followed a few tutorials before and made a few simple bots on my own, but cant seem to figure this out. Its not playing music nor connecting to the vc
Heres My Code:

const ytdl = require(‘ytdl-core’);
const ytSearch = require(‘yt-search’);

module.exports = {
name: ‘play’,
description: ‘Joins and plays a video from youtube’,
async execute(message, args) {
const voiceChannel = message.member.voice.channel;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command!'); 
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
    if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
    if (!args.length) return message.channel.send('You need to send the second argument!');

    const validURL = (str) =>{
        var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
        if(!regex.test(str)){
            return false;
        } else {
            return true;
        }
    }

    if(validURL(args[0])){

        const  connection = await voiceChannel.join();
        const stream  = ytdl(args[0], {filter: 'audioonly'});

        connection.play(stream, {seek: 0, volume: 1})
        .on('finish', () =>{
            voiceChannel.leave();
            message.channel.send('leaving channel');
        });

        await message.reply(`:thumbsup: Now Playing ***Your Link!***`)

        return
    }

    
    const  connection = await voiceChannel.join();

    const videoFinder = async (query) => {
        const videoResult = await ytSearch(query);

        return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;

    }

    const video = await videoFinder(args.join(' '));

    if(video){
        const stream  = ytdl(video.url, {filter: 'audioonly'});
        connection.play(stream, {seek: 0, volume: 1})
        .on('finish', () =>{
            voiceChannel.leave();
        });

        await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
    } else {
        message.channel.send('No video results found');
    }
}

}

>Solution :

I’m glad you’re developing a discord bot, those are a lot of fun! The main principle we want to focus on here is Guard Statements which you make good use of in your code.

If you aren’t already familiar, a guard statement is one that resides at the top of a block of code and prevents the execution of code below it if a specific condition is met. This is really useful for making discord bots because it will allow us to stop command execution if the permission to use it is missing for example (which you use in your code provided)

The caveat here is that in your specific guard statement where you check for the number of args used, you provide this line:

if (!args.length) return message.channel.send('You need to send the second argument!');

Now, there’s nothing wrong with this line, except that we’re only checking if the property length doesn’t exist on args which isn’t what we’re really trying to check.

If we want to make sure that there are exactly two arguments provided, we would want to rewrite the condition like this:

if (args.length == 2) return message.channel.send('You need to send the second argument!');

From here, you can change the condition to meet your desired requirements and it should work great!

Happy programing ~

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading