Add /loop support for individual songs (#707)

This commit is contained in:
Cleo 2022-06-25 20:20:45 -04:00 committed by GitHub
parent eb07ac6d22
commit 6cdfefe174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 4 deletions

View file

@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- `/loop` command that plays the current song on loop
## [2.0.4] - 2022-05-16
### Fixed

34
src/commands/loop.ts Normal file
View file

@ -0,0 +1,34 @@
import {ChatInputCommandInteraction} from 'discord.js';
import {TYPES} from '../types.js';
import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player.js';
import Command from '.';
import {SlashCommandBuilder} from '@discordjs/builders';
import {STATUS} from '../services/player';
@injectable()
export default class implements Command {
public readonly slashCommand = new SlashCommandBuilder()
.setName('loop')
.setDescription('toggle looping the current song');
public requiresVC = true;
private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager;
}
public async execute(interaction: ChatInputCommandInteraction): Promise<void> {
const player = this.playerManager.get(interaction.guild!.id);
if (player.status === STATUS.IDLE) {
throw new Error('no song to loop!');
}
player.loopCurrentSong = !player.loopCurrentSong;
await interaction.reply((player.loopCurrentSong ? 'looped :)' : 'stopped looping :('));
}
}

View file

@ -37,6 +37,7 @@ import Unskip from './commands/unskip.js';
import ThirdParty from './services/third-party.js';
import FileCacheProvider from './services/file-cache.js';
import KeyValueCacheProvider from './services/key-value-cache.js';
import Loop from './commands/loop';
const container = new Container();
@ -79,6 +80,7 @@ container.bind<SpotifyAPI>(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingleton
Next,
Stop,
Unskip,
Loop,
].forEach(command => {
container.bind<Command>(TYPES.Command).to(command).inSingletonScope();
});

View file

@ -60,6 +60,7 @@ export default class {
public voiceConnection: VoiceConnection | null = null;
public status = STATUS.PAUSED;
public guildId: string;
public loopCurrentSong = false;
private queue: QueuedSong[] = [];
private queuePosition = 0;
@ -69,7 +70,6 @@ export default class {
private lastSongURL = '';
private positionInSeconds = 0;
private readonly fileCache: FileCacheProvider;
private disconnectTimer: NodeJS.Timeout | null = null;
@ -92,6 +92,7 @@ export default class {
this.pause();
}
this.loopCurrentSong = false;
this.voiceConnection.destroy();
this.audioPlayer?.stop();
@ -523,6 +524,11 @@ export default class {
private async onAudioPlayerIdle(_oldState: AudioPlayerState, newState: AudioPlayerState): Promise<void> {
// Automatically advance queued song at end
if (this.loopCurrentSong && newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) {
await this.seek(0);
return;
}
if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) {
await this.forward(1);
}

View file

@ -46,8 +46,8 @@ const getPlayerUI = (player: Player) => {
const button = player.status === STATUS.PLAYING ? '⏹️' : '▶️';
const progressBar = getProgressBar(15, position / song.length);
const elapsedTime = song.isLive ? 'live' : `${prettyTime(position)}/${prettyTime(song.length)}`;
return `${button} ${progressBar} \`[${elapsedTime}]\` 🔉`;
const loop = player.loopCurrentSong ? '🔁' : '';
return `${button} ${progressBar} \`[${elapsedTime}]\` 🔉 ${loop}`;
};
export const buildPlayingMessageEmbed = (player: Player): EmbedBuilder => {
@ -119,7 +119,7 @@ export const buildQueueEmbed = (player: Player, page: number): EmbedBuilder => {
}
message
.setTitle(player.status === STATUS.PLAYING ? 'Now Playing' : 'Queued songs')
.setTitle(player.status === STATUS.PLAYING ? `Now Playing ${player.loopCurrentSong ? '(loop on)' : ''}` : 'Queued songs')
.setColor(player.status === STATUS.PLAYING ? 'DarkGreen' : 'NotQuiteBlack')
.setDescription(description)
.addFields([{name: 'In queue', value: getQueueInfo(player), inline: true}, {