mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-06-27 17:22:42 +02:00
Add SponsorBlock support (#1013)
This commit is contained in:
parent
8e08919206
commit
a27598c50a
7 changed files with 379 additions and 344 deletions
|
@ -5,15 +5,32 @@ import {inject, injectable} from 'inversify';
|
|||
import shuffle from 'array-shuffle';
|
||||
import {TYPES} from '../types.js';
|
||||
import GetSongs from '../services/get-songs.js';
|
||||
import {SongMetadata, STATUS} from './player.js';
|
||||
import {MediaSource, SongMetadata, STATUS} from './player.js';
|
||||
import PlayerManager from '../managers/player.js';
|
||||
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
|
||||
import {getMemberVoiceChannel, getMostPopularVoiceChannel} from '../utils/channels.js';
|
||||
import {getGuildSettings} from '../utils/get-guild-settings.js';
|
||||
import {SponsorBlock} from 'sponsorblock-api';
|
||||
import Config from './config';
|
||||
import KeyValueCacheProvider from './key-value-cache';
|
||||
import {ONE_HOUR_IN_SECONDS} from '../utils/constants';
|
||||
|
||||
@injectable()
|
||||
export default class AddQueryToQueue {
|
||||
constructor(@inject(TYPES.Services.GetSongs) private readonly getSongs: GetSongs, @inject(TYPES.Managers.Player) private readonly playerManager: PlayerManager) {
|
||||
private readonly sponsorBlock?: SponsorBlock;
|
||||
private sponsorBlockDisabledUntil?: Date;
|
||||
private readonly sponsorBlockTimeoutDelay;
|
||||
private readonly cache: KeyValueCacheProvider;
|
||||
|
||||
constructor(@inject(TYPES.Services.GetSongs) private readonly getSongs: GetSongs,
|
||||
@inject(TYPES.Managers.Player) private readonly playerManager: PlayerManager,
|
||||
@inject(TYPES.Config) private readonly config: Config,
|
||||
@inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider) {
|
||||
this.sponsorBlockTimeoutDelay = config.SPONSORBLOCK_TIMEOUT;
|
||||
this.sponsorBlock = config.ENABLE_SPONSORBLOCK
|
||||
? new SponsorBlock('muse-sb-integration') // UserID matters only for submissions
|
||||
: undefined;
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public async addToQueue({
|
||||
|
@ -118,6 +135,10 @@ export default class AddQueryToQueue {
|
|||
newSongs = shuffle(newSongs);
|
||||
}
|
||||
|
||||
if (this.config.ENABLE_SPONSORBLOCK) {
|
||||
newSongs = await Promise.all(newSongs.map(this.skipNonMusicSegments.bind(this)));
|
||||
}
|
||||
|
||||
newSongs.forEach(song => {
|
||||
player.add({
|
||||
...song,
|
||||
|
@ -167,4 +188,66 @@ export default class AddQueryToQueue {
|
|||
await interaction.editReply(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async skipNonMusicSegments(song: SongMetadata) {
|
||||
if (!this.sponsorBlock
|
||||
|| (this.sponsorBlockDisabledUntil && new Date() < this.sponsorBlockDisabledUntil)
|
||||
|| song.source !== MediaSource.Youtube
|
||||
|| !song.url) {
|
||||
return song;
|
||||
}
|
||||
|
||||
try {
|
||||
const segments = await this.cache.wrap(
|
||||
async () => this.sponsorBlock?.getSegments(song.url, ['music_offtopic']),
|
||||
{
|
||||
key: song.url, // Value is too short for hashing
|
||||
expiresIn: ONE_HOUR_IN_SECONDS,
|
||||
},
|
||||
) ?? [];
|
||||
const skipSegments = segments
|
||||
.sort((a, b) => a.startTime - b.startTime)
|
||||
.reduce((acc: Array<{startTime: number; endTime: number}>, {startTime, endTime}) => {
|
||||
const previousSegment = acc[acc.length - 1];
|
||||
// If segments overlap merge
|
||||
if (previousSegment && previousSegment.endTime > startTime) {
|
||||
acc[acc.length - 1].endTime = endTime;
|
||||
} else {
|
||||
acc.push({startTime, endTime});
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
const intro = skipSegments[0];
|
||||
const outro = skipSegments.at(-1);
|
||||
if (outro && outro?.endTime >= song.length - 2) {
|
||||
song.length -= outro.endTime - outro.startTime;
|
||||
}
|
||||
|
||||
if (intro?.startTime <= 2) {
|
||||
song.offset = Math.floor(intro.endTime);
|
||||
song.length -= song.offset;
|
||||
}
|
||||
|
||||
return song;
|
||||
} catch (e) {
|
||||
if (!(e instanceof Error)) {
|
||||
console.error('Unexpected event occurred while fetching skip segments : ', e);
|
||||
return song;
|
||||
}
|
||||
|
||||
if (!e.message.includes('404')) {
|
||||
// Don't log 404 response, it just means that there are no segments for given video
|
||||
console.warn(`Could not fetch skip segments for "${song.url}" :`, e);
|
||||
}
|
||||
|
||||
if (e.message.includes('504')) {
|
||||
// Stop fetching SponsorBlock data when servers are down
|
||||
this.sponsorBlockDisabledUntil = new Date(new Date().getTime() + (this.sponsorBlockTimeoutDelay * 60_000));
|
||||
}
|
||||
|
||||
return song;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue