0

Im trying to make an _soundboard / _sb command and i try to check if the .mp3 file exists or not but for any reason it won't work. I hope someone can help me with that..

const Discord = require('discord.js');
const fs = require('fs');

module.exports.run = (client, message, args) => {
    if(message.author.id !== '201679157124399104') {
        message.channel.send(`Nope this is a testing command!`)
        return;
    };
    if(!message.member.voiceChannel) {
        message.channel.send(`You need to be in a voicechannel to do this!`)
        return;
    };
    let sound = args[0] + ".mp3";
    if(fs.exists(`./sounds/${sound}`,function(exists){})) {
    let vc = message.member.voiceChannel
    vc.join().then(connection => {
        const dispatcher = connection.playFile(`./sounds/${sound}`);
        message.channel.send(`playing: ${sound}`).then(m => m.delete(10000))
        dispatcher.on('end', () => {
        vc.leave();
        })
        return;
        
    });
}
    message.channel.send(`The sound "${sound}" doesn't exists!`)

    
    
    
};

module.exports.help = {
    name: "soundboard",
    name2: "sb"
};
SchdowNVIDIA
  • 55
  • 1
  • 1
  • 9
  • try sound = \`"${args[0]}.mp3"`. args[0] might have space? – kiddorails Jul 01 '18 at 10:11
  • `fs.exsits` is depricated, you should probably use [`fs.access`](https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback) instead – Olian04 Jul 01 '18 at 10:12
  • 1
    also, `exists` is async, and takes a callback. the `if` condition should ideally be inside `fs.exists`, rather than outside it. Currently, your callback for it is empty. – kiddorails Jul 01 '18 at 10:14
  • sound = `"${args[0]}.mp3"` and `${args[0]}.mp3` won't work :/ – SchdowNVIDIA Jul 01 '18 at 10:14
  • You should probably look into using absolute paths, instead of relative paths. This is easily done using `absolutePath = path.join(__dirname, relativePath)`. – Olian04 Jul 01 '18 at 10:16
  • also, exists is async, and takes a callback. the if condition should ideally be inside fs.exists won't work too... it's joining everytime it don't matters if it exists or not :C – SchdowNVIDIA Jul 01 '18 at 10:16

2 Answers2

0

You have to learn about how asynchronousity and continuations work in javascript. You are misusing fs.exists and .then. The problem is not the file not being found, its the control flow being flawed. You could simplify the first one using fs.existsSync, but then you still have to deal with the Promise.then part. Writing return in the callback wont return from the outer function. I suggest you to use async/await instead.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
0

!Fixed!

i just added the module path-exists it's a lot easier to use. and the f. way worked too after i tried a bit.

SchdowNVIDIA
  • 55
  • 1
  • 1
  • 9