This commit is contained in:
KGT1 2020-09-24 13:37:17 +02:00
parent e019f4c6e5
commit 0fe1c80f88
3 changed files with 48 additions and 61 deletions

View File

@ -3,9 +3,6 @@ const Discord = require("discord.js");
const { const {
checkJellyfinItemIDRegex checkJellyfinItemIDRegex
} = require("./util"); } = require("./util");
const {
getAudioDispatcher
} = require("./dispachermanager");
const { const {
hmsToSeconds hmsToSeconds
} = require("./util"); } = require("./util");
@ -20,7 +17,7 @@ var isSummendByPlay = false;
// random Color of the Jellyfin Logo Gradient // random Color of the Jellyfin Logo Gradient
function getRandomDiscordColor () { function getRandomDiscordColor () {
let random = Math.random(); const random = Math.random();
function randomNumber (b, a) { function randomNumber (b, a) {
return Math.floor(random * Math.pow(Math.pow((b - a), 2), 1 / 2)) + (b > a ? a : b); return Math.floor(random * Math.pow(Math.pow((b - a), 2), 1 / 2)) + (b > a ? a : b);
} }
@ -186,7 +183,7 @@ function handleChannelMessage (message) {
} }
} else if (message.content.startsWith(CONFIG["discord-prefix"] + "skip")) { } else if (message.content.startsWith(CONFIG["discord-prefix"] + "skip")) {
try { try {
playbackmanager.nextTrack() playbackmanager.nextTrack();
} catch (error) { } catch (error) {
const errorMessage = getDiscordEmbedError(error); const errorMessage = getDiscordEmbedError(error);
message.channel.send(errorMessage); message.channel.send(errorMessage);

View File

@ -7,8 +7,7 @@ const {
ticksToSeconds ticksToSeconds
} = require("./util"); } = require("./util");
// this whole thing should be a class but its probably too late now.
//this whole thing should be a class but its probably too late now.
var currentPlayingPlaylist; var currentPlayingPlaylist;
var currentPlayingPlaylistIndex; var currentPlayingPlaylistIndex;
@ -20,20 +19,20 @@ var _seek;
const jellyfinClientManager = require("./jellyfinclientmanager"); const jellyfinClientManager = require("./jellyfinclientmanager");
function streamURLbuilder(itemID, bitrate) { function streamURLbuilder (itemID, bitrate) {
// so the server transcodes. Seems appropriate as it has the source file. // so the server transcodes. Seems appropriate as it has the source file.
const supportedCodecs = "opus"; const supportedCodecs = "opus";
const supportedContainers = "ogg,opus"; const supportedContainers = "ogg,opus";
return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`; return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`;
} }
function startPlaying(voiceconnection = discordclientmanager.getDiscordClient().user.client.voice.connections.first(), itemIDPlaylist = currentPlayingPlaylist, playlistIndex = currentPlayingPlaylistIndex, seekTo, disconnectOnFinish = _disconnectOnFinish) { function startPlaying (voiceconnection = discordclientmanager.getDiscordClient().user.client.voice.connections.first(), itemIDPlaylist = currentPlayingPlaylist, playlistIndex = currentPlayingPlaylistIndex, seekTo, disconnectOnFinish = _disconnectOnFinish) {
isPaused = false; isPaused = false;
currentPlayingPlaylist = itemIDPlaylist; currentPlayingPlaylist = itemIDPlaylist;
currentPlayingPlaylistIndex = playlistIndex; currentPlayingPlaylistIndex = playlistIndex;
_disconnectOnFinish = disconnectOnFinish; _disconnectOnFinish = disconnectOnFinish;
_seek = seekTo * 1000; _seek = seekTo * 1000;
async function playasync() { async function playasync () {
const url = streamURLbuilder(itemIDPlaylist[playlistIndex], voiceconnection.channel.bitrate); const url = streamURLbuilder(itemIDPlaylist[playlistIndex], voiceconnection.channel.bitrate);
setAudioDispatcher(voiceconnection.play(url, { setAudioDispatcher(voiceconnection.play(url, {
seek: seekTo seek: seekTo
@ -52,16 +51,13 @@ function startPlaying(voiceconnection = discordclientmanager.getDiscordClient().
getAudioDispatcher().on("finish", () => { getAudioDispatcher().on("finish", () => {
if (currentPlayingPlaylist.length < playlistIndex) { if (currentPlayingPlaylist.length < playlistIndex) {
console.log("PLAYLIST END")
if (disconnectOnFinish) { if (disconnectOnFinish) {
stop(voiceconnection, currentPlayingPlaylist[playlistIndex - 1]); stop(voiceconnection, currentPlayingPlaylist[playlistIndex - 1]);
}else { } else {
stop(undefined, currentPlayingPlaylist[playlistIndex - 1]); stop(undefined, currentPlayingPlaylist[playlistIndex - 1]);
} }
} else { } else {
startPlaying(voiceconnection, itemIDPlaylist, currentPlayingPlaylistIndex+1, 0) startPlaying(voiceconnection, itemIDPlaylist, currentPlayingPlaylistIndex + 1, 0);
} }
}); });
} }
@ -72,7 +68,7 @@ function startPlaying(voiceconnection = discordclientmanager.getDiscordClient().
/** /**
* @param {Number} toSeek - where to seek in ticks * @param {Number} toSeek - where to seek in ticks
*/ */
function seek(toSeek = 0) { function seek (toSeek = 0) {
if (getAudioDispatcher()) { if (getAudioDispatcher()) {
startPlaying(undefined, undefined, undefined, ticksToSeconds(toSeek), _disconnectOnFinish); startPlaying(undefined, undefined, undefined, ticksToSeconds(toSeek), _disconnectOnFinish);
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload()); jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
@ -81,35 +77,32 @@ function seek(toSeek = 0) {
} }
} }
function nextTrack(){ function nextTrack () {
//console.log(currentPlayingPlaylistIndex + 1, currentPlayingPlaylist.length); // console.log(currentPlayingPlaylistIndex + 1, currentPlayingPlaylist.length);
if(!(currentPlayingPlaylist)){ if (!(currentPlayingPlaylist)) {
throw Error("There is currently nothing playing"); throw Error("There is currently nothing playing");
}else if(currentPlayingPlaylistIndex + 1 >= currentPlayingPlaylist.length){ } else if (currentPlayingPlaylistIndex + 1 >= currentPlayingPlaylist.length) {
throw Error("This is the Last song"); throw Error("This is the Last song");
} }
startPlaying(undefined, undefined, currentPlayingPlaylistIndex + 1, 0, _disconnectOnFinish) startPlaying(undefined, undefined, currentPlayingPlaylistIndex + 1, 0, _disconnectOnFinish);
} }
function previousTrack(){ function previousTrack () {
if(ticksToSeconds(getPostitionTicks())<10){ if (ticksToSeconds(getPostitionTicks()) < 10) {
console.log(currentPlayingPlaylistIndex , currentPlayingPlaylist.length); if (!(currentPlayingPlaylist)) {
if(!(currentPlayingPlaylist)){
throw Error("There is currently nothing playing"); throw Error("There is currently nothing playing");
}else if(currentPlayingPlaylistIndex -1 < 0){ } else if (currentPlayingPlaylistIndex - 1 < 0) {
startPlaying(undefined, undefined, currentPlayingPlaylistIndex, 0, _disconnectOnFinish) startPlaying(undefined, undefined, currentPlayingPlaylistIndex, 0, _disconnectOnFinish);
throw Error("This is the First song"); throw Error("This is the First song");
} }
startPlaying(undefined, undefined, currentPlayingPlaylistIndex - 1, 0, _disconnectOnFinish) startPlaying(undefined, undefined, currentPlayingPlaylistIndex - 1, 0, _disconnectOnFinish);
} }
} }
/** /**
* @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from * @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from
*/ */
function stop(disconnectVoiceConnection, itemId = getItemId()) { function stop (disconnectVoiceConnection, itemId = getItemId()) {
console.log("im getting called");
console
isPaused = true; isPaused = true;
if (disconnectVoiceConnection) { if (disconnectVoiceConnection) {
disconnectVoiceConnection.disconnect(); disconnectVoiceConnection.disconnect();
@ -126,20 +119,20 @@ function stop(disconnectVoiceConnection, itemId = getItemId()) {
clearInterval(progressInterval); clearInterval(progressInterval);
} }
function pause() { function pause () {
isPaused = true; isPaused = true;
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload()); jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
getAudioDispatcher().pause(true); getAudioDispatcher().pause(true);
} }
function resume() { function resume () {
isPaused = false; isPaused = false;
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload()); jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
getAudioDispatcher().resume(); getAudioDispatcher().resume();
} }
function playPause() { function playPause () {
if(!(getAudioDispatcher())){ if (!(getAudioDispatcher())) {
throw Error("There is nothing Playing right now!"); throw Error("There is nothing Playing right now!");
} }
if (getAudioDispatcher().paused) { if (getAudioDispatcher().paused) {
@ -149,35 +142,34 @@ function playPause() {
} }
} }
function getPostitionTicks() { function getPostitionTicks () {
// this is very sketchy but i dont know how else to do it // this is very sketchy but i dont know how else to do it
console.log((_seek + getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000);
return (_seek + getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000; return (_seek + getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000;
} }
function getPlayMethod() { function getPlayMethod () {
// TODO figure out how to figure this out // TODO figure out how to figure this out
return "Transcode"; return "Transcode";
} }
function getRepeatMode() { function getRepeatMode () {
if(isRepeat){ if (isRepeat) {
return "RepeatOne"; return "RepeatOne";
}else{ } else {
return "RepeatNone"; return "RepeatNone";
} }
} }
function getPlaylistItemId() { function getPlaylistItemId () {
return getItemId(); return getItemId();
} }
function getPlaySessionId() { function getPlaySessionId () {
// i think its just a number which you dont need to retrieve but need to report // i think its just a number which you dont need to retrieve but need to report
return "ae2436edc6b91b11d72aeaa67f84e0ea"; return "ae2436edc6b91b11d72aeaa67f84e0ea";
} }
function getNowPLayingQueue() { function getNowPLayingQueue () {
return [{ return [{
Id: getItemId(), Id: getItemId(),
// as I curently dont support Playlists // as I curently dont support Playlists
@ -185,23 +177,23 @@ function getNowPLayingQueue() {
}]; }];
} }
function getCanSeek() { function getCanSeek () {
return true; return true;
} }
function getIsMuted() { function getIsMuted () {
return false; return false;
} }
function getVolumeLevel() { function getVolumeLevel () {
return 100; return 100;
} }
function getItemId() { function getItemId () {
return currentPlayingPlaylist[currentPlayingPlaylistIndex]; return currentPlayingPlaylist[currentPlayingPlaylistIndex];
} }
function getIsPaused() { function getIsPaused () {
// AudioDispacker Paused is to slow // AudioDispacker Paused is to slow
if (isPaused === undefined) { if (isPaused === undefined) {
@ -211,11 +203,11 @@ function getIsPaused() {
return isPaused; return isPaused;
} }
function setIsRepeat(arg){ function setIsRepeat (arg) {
isRepeat=arg; isRepeat = arg;
} }
function getProgressPayload() { function getProgressPayload () {
const payload = { const payload = {
CanSeek: getCanSeek(), CanSeek: getCanSeek(),
IsMuted: getIsMuted(), IsMuted: getIsMuted(),

View File

@ -1,6 +1,6 @@
const jellyfinClientManager = require("./jellyfinclientmanager"); const jellyfinClientManager = require("./jellyfinclientmanager");
const playbackmanager = require("./playbackmanager"); const playbackmanager = require("./playbackmanager");
const { ticksToSeconds }= require("./util"); const { ticksToSeconds } = require("./util");
function openSocket () { function openSocket () {
jellyfinClientManager.getJellyfinClient().openWebSocket(); jellyfinClientManager.getJellyfinClient().openWebSocket();
@ -12,7 +12,6 @@ function openSocket () {
} }
); );
jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => { jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => {
console.log(data);
if (data.MessageType === "Play") { if (data.MessageType === "Play") {
if (data.Data.PlayCommand === "PlayNow") { if (data.Data.PlayCommand === "PlayNow") {
playbackmanager.startPlaying(undefined, data.Data.ItemIds, data.Data.StartIndex || 0, 0, false); playbackmanager.startPlaying(undefined, data.Data.ItemIds, data.Data.StartIndex || 0, 0, false);
@ -23,8 +22,8 @@ function openSocket () {
} else if (data.Data.Command === "Stop") { } else if (data.Data.Command === "Stop") {
playbackmanager.stop(); playbackmanager.stop();
} else if (data.Data.Command === "Seek") { } else if (data.Data.Command === "Seek") {
//because the server sends seek an privious track at same time so i have to do timing // because the server sends seek an privious track at same time so i have to do timing
setTimeout(async()=>{playbackmanager.seek(data.Data.SeekPositionTicks);},20) setTimeout(async () => { playbackmanager.seek(data.Data.SeekPositionTicks); }, 20);
} else if (data.Data.Command === "NextTrack") { } else if (data.Data.Command === "NextTrack") {
try { try {
playbackmanager.nextTrack(); playbackmanager.nextTrack();
@ -32,12 +31,11 @@ function openSocket () {
console.error(error); console.error(error);
} }
} else if (data.Data.Command === "PreviousTrack") { } else if (data.Data.Command === "PreviousTrack") {
try{ try {
console.log(ticksToSeconds(playbackmanager.getPostitionTicks())<10,ticksToSeconds(playbackmanager.getPostitionTicks()),` (${playbackmanager.getPostitionTicks()})`," < ",10) if (ticksToSeconds(playbackmanager.getPostitionTicks()) < 10) {
if(ticksToSeconds(playbackmanager.getPostitionTicks())<10){
playbackmanager.previousTrack(); playbackmanager.previousTrack();
} }
}catch(error){ } catch (error) {
console.error(error); console.error(error);
} }
} }