<commit_before>const Ytdl = index.Ytdl;

exports.isPlaying = {};
exports.guildMusicInfo = {};
exports.guildQueue = {};
exports.noPlay = {};

exports.stopMusic = function(guild, reason) {
	var connection = guild.voiceConnection;
	if (!connection) return false;
	var voiceChannel = connection.channel;
	var player = connection.player;
	if (!player) return false;
	var dispatcher = player.dispatcher;
	if (!dispatcher) return false;
	exports.isPlaying[guild.id] = false;
	dispatcher.end(reason);
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	guildMusicInfo.activeSong = null;
	guildMusicInfo.activeAuthor = null;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = false;
	return true;
};

exports.clearQueue = function(guild) {
	exports.guildQueue[guild.id] = [];
	return exports.stopMusic(guild);
};

exports.chooseRandomSong = function(guild, autoPlaylist, lastId) {
	var autoSongs = autoPlaylist.songs;
	var newSong = autoSongs[Util.getRandomInt(0, autoSongs.length-1)];
	var songData = exports.formatSong(newSong[0], false);
	var songInfo = [songData, newSong[1]];
	var songId = songData.id;
	if (autoSongs.length > 1 && songId == lastId) {
		return exports.chooseRandomSong(guild, autoPlaylist, lastId);
	} else {
		return songInfo;
	}
};

exports.playRealSong = function(newSong, guild, channel, doPrint) {
	console.log("Playing Real Song");
	if (doPrint == null) doPrint = true;
	var songData = newSong[0];
	var author = newSong[1];
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	guildMusicInfo.activeSong = songData;
	guildMusicInfo.activeAuthor = author;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = false;
	exports.streamAudio(songData, guild, channel);
	if (doPrint) Util.sendDescEmbed(channel, "Playing " + songData.title, "Added by " + Util.safeEveryone(author.toString()), Util.makeEmbedFooter(author), null, 0x00E676);
};

exports.playNextQueue = function(guild, channel, doPrint) {
	console.log("\nCalled Playing Next Queue");
	if (doPrint == null) doPrint = true;
	var guildQueue = exports.guildQueue[guild.id];
	var autoPlaylist = Data.guildGet(guild, Data.playlist);
	console.log("guildQueue");
	console.log(guildQueue.length);
	console.log("-------Playing Next Queue---------\n");
	if (guildQueue.length > 0) {
		var newSong = guildQueue[0];
		exports.playRealSong(newSong, guild, channel, doPrint);
	} else if (autoPlaylist.hasOwnProperty("songs") && autoPlaylist.songs.length > 0) {
		console.log("Playing Next Queue, Playing Next Auto");
		exports.playNextAuto(guild, channel, true);
	} else {
		exports.stopMusic(guild);
	}
};

exports.playNextAuto = function(guild, channel, doPrint) {
	var autoPlaylist = Data.guildGet(guild, Data.playlist);
	if (!autoPlaylist.hasOwnProperty("songs") || autoPlaylist.songs.length === 0) {
		exports.stopMusic(guild);
		return;
	}
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	var lastId = "";
	if (guildMusicInfo.isAuto === true) lastId = guildMusicInfo.activeSong.id;
	var newSong = exports.chooseRandomSong(guild, autoPlaylist, lastId);
	var songData = newSong[0];
	var author = newSong[1];
	/*var newSongNum = songNum+1;
	if (newSongNum >= autoSongs.length) newSongNum = 0;
	autoPlaylist.songNum = newSongNum;
	Data.guildSaveData(Data.playlist);*/
	console.log("Playing Next Auto");
	guildMusicInfo.activeSong = songData;
	guildMusicInfo.activeAuthor = author;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = true;
	exports.streamAudio(songData, guild, channel);
	if (doPrint) Util.sendDescEmbed(channel, "[Auto-Playlist-Started]", "Playing " + songData.title, Util.makeEmbedFooter(null), null, 0x00E676);
};

exports.streamAudio = function(streamId, guild, channel) {
	var connection = guild.voiceConnection;
	if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");

	var oldPlayer = connection.player;

	if (oldPlayer) {
		var oldDispatcher = oldPlayer.dispatcher;
		if (oldDispatcher) {
			console.log("Ended previous!");
			oldDispatcher.end("NewStreamAudio");
		}
	}

	var songId = songData.id;
	var isFile = songData.isFile;

	setTimeout(function() {
		connection = guild.voiceConnection;
		if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");

		var voiceChannel = connection.channel;

		console.log("Streaming Audio: " + streamId);
		
		const streamOptions = {seek: 0, volume: 0.2};
		var dispatcher;
		
		if (!isFile) {
			const stream = Ytdl(streamId, {filter: 'audioonly'});
			dispatcher = connection.playStream(stream, streamOptions);
		} else {
			dispatcher = connection.playFile("/var/files/VaeBot/resources/music/" + streamId + ".mp3");
		}

		exports.isPlaying[guild.id] = true;

		dispatcher.on("error", error => {
			console.log("StreamError: " + error);
		});

		dispatcher.on("end", reason => {
			console.log("Track Ended: " + reason);
			if (reason == "Stream is not generating quickly enough.") {
				if (exports.isPlaying[guild.id]) {
					console.log("Track Ended, Starting Next: " + reason);
					var guildMusicInfo = exports.guildMusicInfo[guild.id];
					var guildQueue = exports.guildQueue[guild.id];

					if (guildQueue.length > 0) {
						var songData = guildQueue[0][0];
						var songId = songData.id;
						if (songId == streamId) guildQueue.splice(0, 1);
					}

					if (voiceChannel.members.size > 1) {
						var autoPlaylist = Data.guildGet(guild, Data.playlist);
						if (guildQueue.length === 0 && guildMusicInfo.isAuto === true) {
							console.log("Track Ended, Playing Next Auto");
							exports.playNextAuto(guild, channel);
						} else {
							exports.playNextQueue(guild, channel, true);
						}
					} else {
						exports.stopMusic(guild);
						Util.print(channel, "Auto stopping audio | No users in voice");
					}
				}
			}
		});
	}, 1000);
};

exports.playFile = function(name, guild, channel) {
	var connection = guild.voiceConnection;
	if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");
	var voiceChannel = connection.channel;<commit_msg>Fixed music bug #6<commit_after>const Ytdl = index.Ytdl;

exports.isPlaying = {};
exports.guildMusicInfo = {};
exports.guildQueue = {};
exports.noPlay = {};

exports.stopMusic = function(guild, reason) {
	var connection = guild.voiceConnection;
	if (!connection) return false;
	var voiceChannel = connection.channel;
	var player = connection.player;
	if (!player) return false;
	var dispatcher = player.dispatcher;
	if (!dispatcher) return false;
	exports.isPlaying[guild.id] = false;
	dispatcher.end(reason);
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	guildMusicInfo.activeSong = null;
	guildMusicInfo.activeAuthor = null;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = false;
	return true;
};

exports.clearQueue = function(guild) {
	exports.guildQueue[guild.id] = [];
	return exports.stopMusic(guild);
};

exports.chooseRandomSong = function(guild, autoPlaylist, lastId) {
	var autoSongs = autoPlaylist.songs;
	var newSong = autoSongs[Util.getRandomInt(0, autoSongs.length-1)];
	var songData = exports.formatSong(newSong[0], false);
	var songInfo = [songData, newSong[1]];
	var songId = songData.id;
	if (autoSongs.length > 1 && songId == lastId) {
		return exports.chooseRandomSong(guild, autoPlaylist, lastId);
	} else {
		return songInfo;
	}
};

exports.playRealSong = function(newSong, guild, channel, doPrint) {
	console.log("Playing Real Song");
	if (doPrint == null) doPrint = true;
	var songData = newSong[0];
	var author = newSong[1];
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	guildMusicInfo.activeSong = songData;
	guildMusicInfo.activeAuthor = author;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = false;
	exports.streamAudio(songData, guild, channel);
	if (doPrint) Util.sendDescEmbed(channel, "Playing " + songData.title, "Added by " + Util.safeEveryone(author.toString()), Util.makeEmbedFooter(author), null, 0x00E676);
};

exports.playNextQueue = function(guild, channel, doPrint) {
	console.log("\nCalled Playing Next Queue");
	if (doPrint == null) doPrint = true;
	var guildQueue = exports.guildQueue[guild.id];
	var autoPlaylist = Data.guildGet(guild, Data.playlist);
	console.log("guildQueue");
	console.log(guildQueue.length);
	console.log("-------Playing Next Queue---------\n");
	if (guildQueue.length > 0) {
		var newSong = guildQueue[0];
		exports.playRealSong(newSong, guild, channel, doPrint);
	} else if (autoPlaylist.hasOwnProperty("songs") && autoPlaylist.songs.length > 0) {
		console.log("Playing Next Queue, Playing Next Auto");
		exports.playNextAuto(guild, channel, true);
	} else {
		exports.stopMusic(guild);
	}
};

exports.playNextAuto = function(guild, channel, doPrint) {
	var autoPlaylist = Data.guildGet(guild, Data.playlist);
	if (!autoPlaylist.hasOwnProperty("songs") || autoPlaylist.songs.length === 0) {
		exports.stopMusic(guild);
		return;
	}
	var guildMusicInfo = exports.guildMusicInfo[guild.id];
	var lastId = "";
	if (guildMusicInfo.isAuto === true) lastId = guildMusicInfo.activeSong.id;
	var newSong = exports.chooseRandomSong(guild, autoPlaylist, lastId);
	var songData = newSong[0];
	var author = newSong[1];
	/*var newSongNum = songNum+1;
	if (newSongNum >= autoSongs.length) newSongNum = 0;
	autoPlaylist.songNum = newSongNum;
	Data.guildSaveData(Data.playlist);*/
	console.log("Playing Next Auto");
	guildMusicInfo.activeSong = songData;
	guildMusicInfo.activeAuthor = author;
	guildMusicInfo.voteSkips = [];
	guildMusicInfo.isAuto = true;
	exports.streamAudio(songData, guild, channel);
	if (doPrint) Util.sendDescEmbed(channel, "[Auto-Playlist-Started]", "Playing " + songData.title, Util.makeEmbedFooter(null), null, 0x00E676);
};

exports.streamAudio = function(songData, guild, channel) {
	var connection = guild.voiceConnection;
	if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");

	var oldPlayer = connection.player;

	if (oldPlayer) {
		var oldDispatcher = oldPlayer.dispatcher;
		if (oldDispatcher) {
			console.log("Ended previous!");
			oldDispatcher.end("NewStreamAudio");
		}
	}

	var songId = songData.id;
	var isFile = songData.isFile;

	setTimeout(function() {
		connection = guild.voiceConnection;
		if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");

		var voiceChannel = connection.channel;

		console.log("Streaming Audio: " + songId);
		
		const streamOptions = {seek: 0, volume: 0.2};
		var dispatcher;
		
		if (!isFile) {
			const stream = Ytdl(songId, {filter: 'audioonly'});
			dispatcher = connection.playStream(stream, streamOptions);
		} else {
			dispatcher = connection.playFile("/var/files/VaeBot/resources/music/" + songId + ".mp3");
		}

		exports.isPlaying[guild.id] = true;

		dispatcher.on("error", error => {
			console.log("StreamError: " + error);
		});

		dispatcher.on("end", reason => {
			console.log("Track Ended: " + reason);
			if (reason == "Stream is not generating quickly enough.") {
				if (exports.isPlaying[guild.id]) {
					console.log("Track Ended, Starting Next: " + reason);
					var guildMusicInfo = exports.guildMusicInfo[guild.id];
					var guildQueue = exports.guildQueue[guild.id];

					if (guildQueue.length > 0) {
						var songData = guildQueue[0][0];
						var songId = songData.id;
						if (songId == songId) guildQueue.splice(0, 1);
					}

					if (voiceChannel.members.size > 1) {
						var autoPlaylist = Data.guildGet(guild, Data.playlist);
						if (guildQueue.length === 0 && guildMusicInfo.isAuto === true) {
							console.log("Track Ended, Playing Next Auto");
							exports.playNextAuto(guild, channel);
						} else {
							exports.playNextQueue(guild, channel, true);
						}
					} else {
						exports.stopMusic(guild);
						Util.print(channel, "Auto stopping audio | No users in voice");
					}
				}
			}
		});
	}, 1000);
};

exports.playFile = function(name, guild, channel) {
	var connection = guild.voiceConnection;
	if (connection == null) return Util.commandFailed(channel, "System", "Bot is not connected to a Voice Channel");
	var voiceChannel = connection.channel;
{'fraction_non_alphanumeric': 0.14013940139401393, 'fraction_numerical': 0.006970069700697007, 'mean_word_length': 4.5740402193784275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3470203', 'n_tokens_mistral': 4531, 'n_tokens_neox': 3997, 'n_words': 882}