2017-10-21 96 views
0

J'ai "index.js" et "settings.json" dans le même dossier mais j'obtiens une erreur en disant que le fichier (settings.json) ne peut pas être trouvé. Il cherche au bon endroit mais ne le trouve pas.mon index.js ne peut pas trouver settings.json

var config = JSON.parse(fs.readFileSync('./settings.json', 'utf-8')); 

Error Message

[code complet]

function isYoutube(str) { 
 
    console.log('function isYoutube is running') 
 
    return str.toLowerCase().indexOf("youtube.com") > -1; 
 
} 
 
const Discord = require("discord.js"); 
 
//const client = new Discord.client(); 
 
const client = new Discord.Client(); //my better version hahahaahahhh 
 
const ytdl = require("ytdl-core"); 
 
const request = require("request"); 
 
const fs = require("fs"); 
 
const getYouTubeID = require("get-youtube-id"); 
 
const fetchVideoInfo = require("youtube-info"); 
 

 
var config = JSON.parse(fs.readFileSync('./settings.json', 'utf-8')); 
 

 
const yt_api_key = config.yt_api_key; 
 
const bot_controller = config.bot_controller; 
 
const prefix = config.prefix; 
 
const discord_token = config.discord_token; 
 

 
var queue = []; 
 
//var isPlaying = false; 
 
var dispatcher = null; 
 
var voiceChannel = null; 
 
var skipReq = 0; 
 
var skippers = []; 
 
//b 
 
var musicServers = {}; //all servers playing music 
 
var musicServer = {}; //current music server 
 
var musicQueue = []; //queue in current server 
 
var musicList = []; //names of music queue 
 
var musicResults = []; //all results of search query 
 
var musicSearch; //query for youtube search 
 
var isPlaying = false; //is music playing 
 
var isStreaming = false; //if music is streaming 
 
var isSearch = true; //if query is for search 
 
var isLooping = false; //if music is looping 
 
var isSearching = false; //if searching 
 

 
client.login(discord_token) 
 

 
client.on('message', function(message) { 
 
\t \t \t if (message.author.equals(client.user)) return; //check if the client sent the message, if so ignore 
 
\t \t \t if (!message.content.startsWith(prefix)) return; //check for prefix 
 
\t \t \t var args = message.content.substring(prefix.length).split(" "); //take each argument 
 
\t \t \t console.log('function client.on is running') 
 
\t \t \t switch (args[0].toLowerCase()) { 
 
\t \t \t \t /* 
 
    case "play": 
 
     if (args[1].indexOf('youtube.com')) { //if it is a youtube link 
 
     console.log('playing from link i think') 
 
     voiceChannel = message.member.voiceChannel; 
 
     isPlaying = true; 
 
     voiceChannel.join().then(function (connection) { 
 
      stream = ytdl(args[1], { 
 
      filter: 'audioonly' 
 
      }); 
 
      console.log(args[1]) 
 
      skipReq = 0; 
 
      skippers = []; 
 

 
      dispatcher = connection.playStream(stream); 
 
     }) 
 
     } 
 
     else { // if its a query 
 
     //do shit 
 
     for (var i = 1; i < args.length; i++) { 
 
      var query = query + args[i] + " " 
 
     } 
 
     console.log(query) 
 
     } 
 
     break; 
 
    case "pause": 
 
     if (isPlaying == true) musicServer.dispatcher.paused = true, message.reply('paused') 
 
     else message.reply('not playing anything b') 
 
     break; 
 
    } 
 
    */ 
 
\t \t \t \t case "play": 
 
\t \t \t \t \t isSearch = true; 
 
\t \t \t \t \t if (args[1]) { //if link or search query is provided, run code 
 
\t \t \t \t \t \t serverID = JSON.parse(message.guild.id); 
 
\t \t \t \t \t \t if (!message.member.voiceChannel) { //check if on voice channel 
 
\t \t \t \t \t \t \t message.reply('u not in voice channel b') 
 
\t \t \t \t \t \t \t return 
 
\t \t \t \t \t \t }; 
 
\t \t \t \t \t \t if (isSearching == true) { 
 
\t \t \t \t \t \t \t message.reply('choose a song before you search again') 
 
\t \t \t \t \t \t \t return 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t if (args[1].indexOf('.com') && !args[1].indexOf('youtube.com')) { 
 
\t \t \t \t \t \t \t message.reply('only youtube b') 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t if (!musicServers[serverID]) musicServers[serverID] = { 
 
\t \t \t \t \t \t \t musicQueue: [] 
 
\t \t \t \t \t \t }; 
 
\t \t \t \t \t \t if (args[1].indexOf('youtube.com') >= 0) { //if its a link, run code 
 
\t \t \t \t \t \t \t musicServers[serverID].musicQueue.push(args[1]); 
 
\t \t \t \t \t \t \t musicServer = musicServers[serverID]; 
 
\t \t \t \t \t \t \t info(message) 
 
\t \t \t \t \t \t \t isPlaying = true; 
 
\t \t \t \t \t \t \t isSearch = false; 
 
\t \t \t \t \t \t } else { //if its a search query, run code 
 
\t \t \t \t \t \t \t for (var i = 1; i < args.length; i++) { //for loop to loop through search query 
 
\t \t \t \t \t \t \t \t musicSearch = musicSearch + ' ' + args[i] 
 
\t \t \t \t \t \t \t } 
 

 
     youtube.search(musicSearch, 5, function(error, result) { 
 
      if (error) { 
 
      console.log(error); 
 
      } else { 
 
      //push results to public variable 
 
      for (var i = 0; i < 5; i++) { 
 
       //console.log(result.items[i]) 
 
       musicResults[i] = result.items[i] 
 
      } 
 
      //choose song out of results 
 
      var ret = "\n\n`"; 
 
      for (var i = 0; i < 4; i++) { 
 
       //console.log(musicResults[i].snippet) 
 
       console.log(i) 
 
       //console.log(ret += (i + 1) + ": " + musicResults[i].snippet.title + "\n") 
 
       ret += (i + 1) + ": " + musicResults[i].snippet.title + "\n"; 
 
      } 
 
      ret += "`" 
 
      message.reply('search results:' + ret); 
 
      isSearching = true; 
 
      isSearch = true; 
 
      } 
 
     }); 
 
     } 
 
     if (!message.guild.voiceConnection && isSearch == false) message.member.voiceChannel.join().then(function(connection) { 
 
     play(connection, message) 
 
     }); 
 
     else if (isStreaming == false && isSearch == false) { 
 
     play(connection, message) 
 
     } 
 
    } else { 
 
     message.reply('pls provide a link or search query') 
 
     return; 
 
    } 
 
    break; 
 
    case "choose": 
 
    serverID = JSON.parse(message.guild.id); 
 
    args[1] = args[1] - 1; 
 
    message.reply('now playing: ' + musicResults[args[1]].snippet.title) 
 
    musicServers[serverID].musicQueue.push('https://www.youtube.com/watch?v=' + musicResults[args[1]].id.videoId); 
 
    console.log(musicServers[serverID].musicQueue[0]) 
 
    musicServer = musicServers[serverID]; 
 
    isPlaying = true; 
 
    isSearching = false; 
 
    info(message) 
 
    if (!message.guild.voiceConnection) message.member.voiceChannel.join().then(function(connection) { 
 
     play(connection, message) 
 
    }); 
 
    else if (isStreaming == false && message.guild.voiceConnection) { 
 
     play(connection, message) 
 
    } 
 
    break; 
 
    case "join": 
 
    message.member.voiceChannel.join() 
 
    message.reply('joining now!') 
 
    break; 
 
    case "skip": 
 
    if (musicServer.musicQueue[0]) { 
 
     serverID = JSON.parse(message.guild.id); 
 
     musicServer = musicServers[serverID]; 
 
     musicList.shift() 
 
     musicServer.dispatcher.end() 
 
    } 
 
    break; 
 
    case "song": 
 
    message.reply('currently playing ' + musicList[0].title + ' ' + duration) 
 
    break; 
 
    case "queue": 
 
    var ret = "\n\n`"; 
 
    for (var i = 0; i < musicList.length; i++) { 
 
     ret += (i + 1) + ": " + musicList[i].title + (i === 0 ? " **(current)**" : "") + "\n"; 
 
    } 
 
    ret += "`" 
 
    message.reply(ret); 
 
    break; 
 
    case "pause": 
 
    if (isPlaying == true) musicServer.dispatcher.paused = true, message.reply('paused') 
 
    else message.reply('not playing anything b') 
 
    break; 
 
    case "resume": 
 
    if (isPlaying == true) musicServer.dispatcher.paused = false, message.reply('resumed') 
 
    else message.reply('not playing anything b') 
 
    break; 
 
    case "loop": 
 
    if (isLooping == true) { 
 
     isLooping = false 
 
    } 
 
    if (isLooping == false) { 
 
     isLooping = true 
 
    } 
 
    break; 
 
    //moderation commmands 
 
/* old shit 
 
    if (mess.startsWith(prefix + "play")) { 
 
    if (queue.length > 0 || isPlaying) { //add to queeueueueueu 
 
     getID(args, function (id) { 
 
     add_to_queue(id); 
 
     fetchVideoInfo(id, function (err, videoInfo) { 
 

 
      console.log('function fetchVideoInfo is running') 
 

 
      if (err) throw new Error(err); 
 
      message.reply(" added to queue: **" + videoInfo.title + "**"); 
 
     }); 
 
     }); 
 
    } else { //start playin and also joinininin 
 
     console.log('start playing and joinin') 
 
     isPlaying = true; 
 
     getID(args, function (id) { 
 
     queue.push(id); 
 
     console.log('playing' + id) 
 
     playMusic(id, message); 
 
     fetchVideoInfo(id, function (err, videoInfo) { 
 
      if (err) throw new Error(err); 
 
      message.reply(" now playing: **" + videoInfo.title + "**"); 
 
     }); 
 
     }); 
 
    } 
 

 
    }*/ 
 
    client.on('ready', function() { 
 
    console.log("I am ready!"); 
 
    }); 
 
} 
 
}); 
 

 

 
client.on('ready', function() { 
 
    console.log("I am ready!"); 
 
}); 
 

 

 
function play(connection, message) { 
 
    isStreaming = true; 
 
    musicServer.dispatcher = connection.playStream(ytdl(musicServer.musicQueue[0], { 
 
    filter: "audioonly" 
 
    })); 
 
    console.log('joined') 
 
    info(connection, message) 
 
    if (isLooping == false) musicServer.musicQueue.shift(); 
 
    musicServer.dispatcher.on("end", function() { 
 
    if (musicServer.musicQueue[0]) musicList.shift(), message.reply('now playing ' + musicInfo.title.toLowerCase() + ' `' + duration + '` '), play(connection, message); 
 
    else isPlaying = false, isStreaming = false, connection.disconnect(); 
 
    }); 
 
} 
 

 
function info(message) { 
 
    fetchVideoInfo(getYouTubeID(musicServer.musicQueue[0])).then(function(musicInfo) { 
 
    musicList.push(musicInfo) 
 
    if ((musicInfo.duration/60) >= 1) { //if duration is more than a minute 
 
     if ((musicInfo.duration/3600) >= 1) { //if duration is more than a hour 
 
     var hours = Math.floor(musicInfo.duration/3600); 
 
     if (Math.floor((musicInfo.duration/60) - (hours * 3600)) < 0) { 
 
      var minutes = 0 
 
     } else { 
 
      var minutes = Math.floor((musicInfo.duration/60) - (hours * 3600)); 
 
     } 
 
     var seconds = Math.floor((musicInfo.duration - (minutes * 60)) - (hours * 3600)); 
 
     if (minutes < 10) { //if less than 10 mins 
 
      if (seconds < 10) { //if less than 10 secconds 
 
      duration = `${hours}:0${minutes}:0${seconds}` 
 
      } else { 
 
      duration = `${hours}:0${minutes}:${seconds}` 
 
      } 
 
     } else { //if more than 10 mins 
 
      if (seconds < 10) { //if less than 10 secconds 
 
      duration = `${hours}:${minutes}:0${seconds}` 
 
      } else { 
 
      duration = `${hours}:${minutes}:${seconds}` 
 
      } 
 
     } 
 
     } else { //if duration is less than an hour, more than a minute 
 
     if (Math.floor(musicInfo.duration/60) < 10) { //if less than 10 minutes 
 
      var minutes = Math.floor(musicInfo.duration/60); 
 
      var seconds = musicInfo.duration - minutes * 60; 
 
      if (seconds < 10) { //if less than 10 secconds 
 
      duration = `0${minutes}:0${seconds}` 
 
      } else { 
 
      duration = `0${minutes}:${seconds}` 
 
      } 
 
     } else { 
 
      var minutes = Math.floor(musicInfo.duration/60); 
 
      var seconds = musicInfo.duration - minutes * 60; 
 
      duration = `${minutes}:${seconds}` 
 
     } 
 
     } 
 
    } else { //if duration is less than a minute 
 
     if (musicInfo.duration < 10) { //if less than 10 secconds 
 
     var seconds = musicInfo.duration; 
 
     duration = `0${seconds}` 
 
     } else { 
 
     var seconds = musicInfo.duration; 
 
     duration = `${seconds}` 
 
     } 
 
    } 
 
    message.reply('added ' + musicInfo.title + ' `' + duration + '` to the queue') 
 
    }); 
 
} 
 

 

 
function playMusic (id, message) { 
 
    console.log('function playMusic is running') 
 
    voiceChannel = message.member.voiceChannel; 
 

 
    voiceChannel.join().then(function (connection) { 
 
    stream = ytdl("https://youtube.com/watch?v=" + id, { 
 
     filter: 'audioonly' 
 
    }); 
 
    console.log("https://youtube.com/watch?v=" + id) 
 
    skipReq = 0; 
 
    skippers = []; 
 

 
    dispatcher = connection.playStream(stream); 
 
    }); 
 
} 
 

 
function getID(str, cb) { 
 
    console.log('function getID is running') 
 
    if (isYoutube(str)) { 
 
    cb(getYouTubeID(str)); 
 
    } else { 
 
    search_video(str, function (id) { 
 
     cb(id); 
 
    }); 
 
    } 
 
} 
 

 
function add_to_queue(strID) { 
 
    console.log('function add_to_queue is running') 
 
    if (isYoutube(strID)) { 
 
    queue.pish(getYouTubeID(strID)); 
 
    } else { 
 
    queue.push(strID); 
 
    } 
 
} 
 

 
function search_video(query, callback) { 
 
    console.log('function search_video is running') 
 
    request("https://www.googleapis.com/youtube/v3/search?part=id&type=video&q=" + encodeURIComponent(query) + "&key=" + yt_api_key, function(error, response, body) { 
 
     //console.log(body) 
 
     //console.log(body.items[0]) 
 
    }); 
 

 
};

+0

Pouvez-vous s'il vous plaît mettre à jour votre structure de dossiers – kgangadhar

+0

S'il vous plaît poster où vos fichiers se trouvent, c'est là que 'MusicBot' est situé par rapport à' settings.json' – OArnarsson

Répondre

0

Si les deux index.js et settings.json sont dans le même dossier, puis selon votre journal des erreurs u besoin de changer la chemin pour settings.json de .\settings.json à .\MusicBot\settings.json