I'm having trouble with playFile()

I want download a audio file via ytdl-core and play it, but when i try to play it with connection.playFile() i get the following error:

TypeError [ERR_INVALID_ARG_TYPE]: The “file” argument must be of type string. Received type object
TypeError [ERR_INVALID_ARG_TYPE]: The “file” argument must be of type string. Received type object

var Module=typeof Module!==“undefined”?Module:{};var moduleOverrides={};var key;for(key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}Module[“arguments”]=;Module[“thisProgram”]="./this.program";Module[“quit”]=function(status,toThrow){throw toThrow};Module[“preRun”]=;Module[“postRun”]=;var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_HAS_NODE=false;var ENVIRONMENT_IS_SHELL=false;ENVIRONMENT_IS_WEB=typeof window===“object”;ENVIRONMENT_IS_WORKER=typeof importScripts===“function”;ENVIRONMENT_HAS_NODE=typeof process===“object”&&typeof process.versions===“object”&&typeof process.versions.node===“string”;ENVIRONMENT_IS_NODE=ENVIRONMENT_HAS_NODE&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;var scriptDirectory="";function locateFile
abort(TypeError [ERR_INVALID_ARG_TYPE]: The “file” argument must be of type string. Received type object). Build with -s ASSERTIONS=1 for more info.strong text

This is the code i’ using:

async run(args: string[], msgObject: Discord.Message, client: Discord.Client): Promise<void> {  
    
    const url = args[0];
    var stream = ytdl(url);
    const streamOptions = { seek: 0, volume: 1 };
    try {
        
        
        await stream.pipe(fs.createWriteStream('tmp_buf_audio.mp3'))
        msgObject.delete();
        msgObject.member.voiceChannel.join()
            .then(connection =>{
                msgObject.reply("Succesfully joined")
                connection.playFile('./tmp_buf_audio', streamOptions)
                .on("end", () => {
                    connection.channel.leave();
                })
            })
            
    }
    catch {
        msgObject.channel.send("You have to  be in a Voicechannel to use this command");

        
    }

try this…

const client = require("discord.js");
const config = require("./config.json")
var stream = require("ytdl-core");
const streamOptions = { seek: 0, volume: 1 };

client.on("ready", async client => { console.log("connected.") }) client.on("message", async msgObject => {

    msgObject.delete();
    let voiceChannel = msgObject.guild.channels.find("id", message.author.guild.channel.id)
    msgObject.member.voiceChannel.join()
        .then(connection =>{
            msgObject.reply("Succesfully joined")
            connection.playFile('./tmp_buf_audio', streamOptions)
            .on("end", () => {
                voiceChannel.leave();
            })
        }).catch(error => {
   msgObject.reply("You have to be in a Voicechannel to use this command");

still getting the same error.

let voiceChannel = msgObject.guild.channels.find(“id”, message.author.guild.channel.id)
doesnt work because it returns a guild channel and not a voicechannel.

Hi !
I have the same issue and I don’t know how to solve it, have you found a solution now?
I think it’s a problem with the modules but i think all is already install :
“ffmpeg”: “0.0.4”,
“ffmpeg-binaries”: “^3.2.2-3”,
“ffmpeg-static”: “^3.0.0”,
“opusscript”: “0.0.7”,
“simple-youtube-api”: “^5.2.1”,
“ytdl-core”: “^1.0.6”

try using ytdl-core-discord instead of ytdl core
instead of downloading and playing the field you have to create a stream and play it with
voiceConnection.playOpusStream(await stream) worked for me

1 Like