1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* eslint-disable complexity */
import {ChatInputCommandInteraction, GuildMember} from 'discord.js';
import {URL} from 'node:url';
import {inject, injectable} from 'inversify';
import shuffle from 'array-shuffle';
import {TYPES} from '../types.js';
import GetSongs from '../services/get-songs.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.js';
import KeyValueCacheProvider from './key-value-cache.js';
import {ONE_HOUR_IN_SECONDS} from '../utils/constants.js';
@injectable()
export default class AddQueryToQueue {
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({
query,
addToFrontOfQueue,
shuffleAdditions,
shouldSplitChapters,
skipCurrentTrack,
interaction,
}: {
query: string;
addToFrontOfQueue: boolean;
shuffleAdditions: boolean;
shouldSplitChapters: boolean;
skipCurrentTrack: boolean;
interaction: ChatInputCommandInteraction;
}): Promise<void> {
const guildId = interaction.guild!.id;
const player = this.playerManager.get(guildId);
const wasPlayingSong = player.getCurrent() !== null;
const [targetVoiceChannel] = getMemberVoiceChannel(interaction.member as GuildMember) ?? getMostPopularVoiceChannel(interaction.guild!);
const settings = await getGuildSettings(guildId);
const {playlistLimit, queueAddResponseEphemeral} = settings;
await interaction.deferReply({ephemeral: queueAddResponseEphemeral});
let newSongs: SongMetadata[] = [];
let extraMsg = '';
// Test if it's a complete URL
try {
const url = new URL(query);
const YOUTUBE_HOSTS = [
'www.youtube.com',
'youtu.be',
'youtube.com',
'music.youtube.com',
'www.music.youtube.com',
];
if (YOUTUBE_HOSTS.includes(url.host)) {
// YouTube source
if (url.searchParams.get('list')) {
// YouTube playlist
newSongs.push(...await this.getSongs.youtubePlaylist(url.searchParams.get('list')!, shouldSplitChapters));
} else {
const songs = await this.getSongs.youtubeVideo(url.href, shouldSplitChapters);
if (songs) {
newSongs.push(...songs);
} else {
throw new Error('that doesn\'t exist');
}
}
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(query, playlistLimit, shouldSplitChapters);
if (totalSongs > playlistLimit) {
extraMsg = `a random sample of ${playlistLimit} songs was taken`;
}
if (totalSongs > playlistLimit && nSongsNotFound !== 0) {
extraMsg += ' and ';
}
if (nSongsNotFound !== 0) {
if (nSongsNotFound === 1) {
extraMsg += '1 song was not found';
} else {
extraMsg += `${nSongsNotFound.toString()} songs were not found`;
}
}
newSongs.push(...convertedSongs);
} else {
const song = await this.getSongs.httpLiveStream(query);
if (song) {
newSongs.push(song);
} else {
throw new Error('that doesn\'t exist');
}
}
} catch (_: unknown) {
// Not a URL, must search YouTube
const songs = await this.getSongs.youtubeVideoSearch(query, shouldSplitChapters);
if (songs) {
newSongs.push(...songs);
} else {
throw new Error('that doesn\'t exist');
}
}
if (newSongs.length === 0) {
throw new Error('no songs found');
}
if (shuffleAdditions) {
newSongs = shuffle(newSongs);
}
if (this.config.ENABLE_SPONSORBLOCK) {
newSongs = await Promise.all(newSongs.map(this.skipNonMusicSegments.bind(this)));
}
newSongs.forEach(song => {
player.add({
...song,
addedInChannelId: interaction.channel!.id,
requestedBy: interaction.member!.user.id,
}, {immediate: addToFrontOfQueue ?? false});
});
const firstSong = newSongs[0];
let statusMsg = '';
if (player.voiceConnection === null) {
await player.connect(targetVoiceChannel);
// Resume / start playback
await player.play();
if (wasPlayingSong) {
statusMsg = 'resuming playback';
}
await interaction.editReply({
embeds: [buildPlayingMessageEmbed(player)],
});
} else if (player.status === STATUS.IDLE) {
// Player is idle, start playback instead
await player.play();
}
if (skipCurrentTrack) {
try {
await player.forward(1);
} catch (_: unknown) {
throw new Error('no song to skip to');
}
}
// Build response message
if (statusMsg !== '') {
if (extraMsg === '') {
extraMsg = statusMsg;
} else {
extraMsg = `${statusMsg}, ${extraMsg}`;
}
}
if (extraMsg !== '') {
extraMsg = ` (${extraMsg})`;
}
if (newSongs.length === 1) {
await interaction.editReply(`u betcha, **${firstSong.title}** added to the${addToFrontOfQueue ? ' front of the' : ''} queue${skipCurrentTrack ? 'and current track skipped' : ''}${extraMsg}`);
} else {
await interaction.editReply(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${skipCurrentTrack ? 'and current track skipped' : ''}${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;
}
}
}
|