Skip to content

Commit

Permalink
Added admin function: move
Browse files Browse the repository at this point in the history
  • Loading branch information
htilly committed Dec 17, 2024
1 parent 8a2d1b1 commit 19c6e34
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions helpTextAdmin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
`debug` : show debug info for Spotify, Node and Sonos
`flush` : flush the current queue
`remove` *number* : removes the track in the queue
`move` *number* *number* : move the given track to desired position in the queue
`setvolume` *number* : sets volume
`play` : play track
`stop` : stop life
Expand Down
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ function processInput(text, channel, userName) {
case 'say':
_tts(input, channel)
break
case 'move':
_moveTrackAdmin(input, channel, userName)
break
default:
}
}
Expand Down Expand Up @@ -890,6 +893,54 @@ function _vote(input, channel, userName) {
});
}

async function _moveTrackAdmin(input, channel, userName) {
if (channel !== global.adminChannel) {
_slackMessage('You do not have permission to move tracks.', channel);
return;
}

if (input.length < 3) {
_slackMessage('Please provide both the track number and the desired position.', channel);
return;
}

const trackNb = parseInt(input[1], 10); // Use the original input value
const desiredPosition = parseInt(input[2], 10); // Use the original input value

if (isNaN(trackNb) || isNaN(desiredPosition)) {
_slackMessage('Invalid track number or desired position.', channel);
return;
}

logger.info(`_moveTrackAdmin: Moving track ${trackNb} to position ${desiredPosition}`);

try {
const queue = await sonos.getQueue();
logger.info('Current queue: ' + JSON.stringify(queue, null, 2));

const track = queue.items.find(item => item.id.split('/')[1] === String(trackNb + 1)); // Adjust for 1-based index
if (!track) {
_slackMessage(`Track number ${trackNb} not found in the queue.`, channel);
return;
}

const currentTrackPosition = parseInt(track.id.split('/')[1], 10);
logger.info('Current track position: ' + currentTrackPosition);

// Define the parameters
const startingIndex = currentTrackPosition; // Current position of the track
const numberOfTracks = 1; // Moving one track
const insertBefore = desiredPosition + 1; // Adjust for 1-based index

// Move the track to the desired position using reorderTracksInQueue
await sonos.reorderTracksInQueue(startingIndex, numberOfTracks, insertBefore, 0);
logger.info(`Moved track ${trackNb} to position ${desiredPosition}`);
_slackMessage(`Moved track ${trackNb} to position ${desiredPosition}`, channel);
} catch (err) {
logger.error('Error occurred: ' + err);
_slackMessage('Error moving track. Please try again later.', channel);
}
}


/**
Expand Down

0 comments on commit 19c6e34

Please sign in to comment.