Configurable voice channel leave behavior (#514)

Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
Johannes Vääräkangas 2022-02-12 04:05:02 +02:00 committed by GitHub
parent 8e5b3cfa43
commit 4dbb55a721
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 11 deletions

View file

@ -4,7 +4,7 @@ import {Except} from 'type-fest';
import shuffle from 'array-shuffle';
import {TYPES} from '../types.js';
import GetSongs from '../services/get-songs.js';
import {QueuedSong} from './player.js';
import {QueuedSong, STATUS} from './player.js';
import PlayerManager from '../managers/player.js';
import {prisma} from '../utils/db.js';
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
@ -131,6 +131,9 @@ export default class AddQueryToQueue {
await interaction.editReply({
embeds: [buildPlayingMessageEmbed(player)],
});
} else if (player.status === STATUS.IDLE) {
// Player is idle, start playback instead
await player.play();
}
// Build response message

View file

@ -8,6 +8,7 @@ import shuffle from 'array-shuffle';
import {AudioPlayer, AudioPlayerStatus, createAudioPlayer, createAudioResource, joinVoiceChannel, StreamType, VoiceConnection, VoiceConnectionStatus} from '@discordjs/voice';
import FileCacheProvider from './file-cache.js';
import debug from '../utils/debug.js';
import {prisma} from '../utils/db.js';
export interface QueuedPlaylist {
title: string;
@ -29,6 +30,7 @@ export interface QueuedSong {
export enum STATUS {
PLAYING,
PAUSED,
IDLE,
}
export interface PlayerEvents {
@ -50,6 +52,7 @@ export default class {
private positionInSeconds = 0;
private readonly fileCache: FileCacheProvider;
private disconnectTimer: NodeJS.Timeout | null = null;
constructor(fileCache: FileCacheProvider, guildId: string) {
this.fileCache = fileCache;
@ -131,6 +134,12 @@ export default class {
throw new Error('Queue empty.');
}
// Cancel any pending idle disconnection
if (this.disconnectTimer) {
clearInterval(this.disconnectTimer);
this.disconnectTimer = null;
}
// Resume from paused state
if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) {
if (this.audioPlayer) {
@ -206,12 +215,31 @@ export default class {
async forward(skip: number): Promise<void> {
this.manualForward(skip);
console.log(this.getCurrent());
try {
if (this.getCurrent() && this.status !== STATUS.PAUSED) {
await this.play();
} else {
this.status = STATUS.PAUSED;
this.disconnect();
this.audioPlayer?.stop();
this.status = STATUS.IDLE;
const settings = await prisma.setting.findUnique({where: {guildId: this.guildId}});
if (!settings) {
throw new Error('Could not find settings for guild');
}
const {secondsToWaitAfterQueueEmpties} = settings;
if (secondsToWaitAfterQueueEmpties !== 0) {
this.disconnectTimer = setTimeout(() => {
// Make sure we are not accidentally playing
// when disconnecting
if (this.status === STATUS.IDLE) {
this.disconnect();
}
}, secondsToWaitAfterQueueEmpties * 1000);
}
}
} catch (error: unknown) {
this.queuePosition--;