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
|
import {AutocompleteInteraction, CommandInteraction, GuildMember} from 'discord.js';
import {URL} from 'url';
import {Except} from 'type-fest';
import {SlashCommandBuilder} from '@discordjs/builders';
import shuffle from 'array-shuffle';
import {inject, injectable} from 'inversify';
import Spotify from 'spotify-web-api-node';
import Command from '.';
import {TYPES} from '../types.js';
import {QueuedSong, STATUS} from '../services/player.js';
import PlayerManager from '../managers/player.js';
import {getMostPopularVoiceChannel, getMemberVoiceChannel} from '../utils/channels.js';
import errorMsg from '../utils/error-msg.js';
import GetSongs from '../services/get-songs.js';
import {prisma} from '../utils/db.js';
import ThirdParty from '../services/third-party.js';
import getYouTubeAndSpotifySuggestionsFor from '../utils/get-youtube-and-spotify-suggestions-for.js';
import KeyValueCacheProvider from '../services/key-value-cache.js';
import {ONE_HOUR_IN_SECONDS} from '../utils/constants.js';
@injectable()
export default class implements Command {
public readonly slashCommand = new SlashCommandBuilder()
.setName('play')
// TODO: make sure verb tense is consistent between all command descriptions
.setDescription('play a song or resume playback')
.addStringOption(option => option
.setName('query')
.setDescription('YouTube URL, Spotify URL, or search query')
.setAutocomplete(true))
.addBooleanOption(option => option
.setName('immediate')
.setDescription('adds track to the front of the queue'))
.addBooleanOption(option => option
.setName('shuffle')
.setDescription('shuffles the input if it\'s a playlist'));
public requiresVC = true;
private readonly playerManager: PlayerManager;
private readonly getSongs: GetSongs;
private readonly spotify: Spotify;
private readonly cache: KeyValueCacheProvider;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Services.GetSongs) getSongs: GetSongs, @inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider) {
this.playerManager = playerManager;
this.getSongs = getSongs;
this.spotify = thirdParty.spotify;
this.cache = cache;
}
// eslint-disable-next-line complexity
public async execute(interaction: CommandInteraction): Promise<void> {
const [targetVoiceChannel] = getMemberVoiceChannel(interaction.member as GuildMember) ?? getMostPopularVoiceChannel(interaction.guild!);
const settings = await prisma.setting.findUnique({where: {guildId: interaction.guild!.id}});
if (!settings) {
throw new Error('Could not find settings for guild');
}
const {playlistLimit} = settings;
const player = this.playerManager.get(interaction.guild!.id);
const wasPlayingSong = player.getCurrent() !== null;
const query = interaction.options.getString('query');
if (!query) {
if (player.status === STATUS.PLAYING) {
await interaction.reply({content: errorMsg('already playing, give me a song name'), ephemeral: true});
return;
}
// Must be resuming play
if (!wasPlayingSong) {
await interaction.reply({content: errorMsg('nothing to play'), ephemeral: true});
return;
}
await player.connect(targetVoiceChannel);
await player.play();
await interaction.reply('the stop-and-go light is now green');
return;
}
const addToFrontOfQueue = interaction.options.getBoolean('immediate');
const shuffleAdditions = interaction.options.getBoolean('shuffle');
let newSongs: Array<Except<QueuedSong, 'addedInChannelId'>> = [];
let extraMsg = '';
await interaction.deferReply();
// 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')!));
} else {
// Single video
const song = await this.getSongs.youtubeVideo(url.href);
if (song) {
newSongs.push(song);
} else {
await interaction.editReply(errorMsg('that doesn\'t exist'));
return;
}
}
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(query, playlistLimit);
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);
}
} catch (_: unknown) {
// Not a URL, must search YouTube
const song = await this.getSongs.youtubeVideoSearch(query);
if (song) {
newSongs.push(song);
} else {
await interaction.editReply(errorMsg('that doesn\'t exist'));
return;
}
}
if (newSongs.length === 0) {
await interaction.editReply(errorMsg('no songs found'));
return;
}
if (shuffleAdditions) {
newSongs = shuffle(newSongs);
}
newSongs.forEach(song => {
player.add({...song, addedInChannelId: interaction.channel?.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';
}
}
// 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${extraMsg}`);
} else {
await interaction.editReply(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
}
}
public async handleAutocompleteInteraction(interaction: AutocompleteInteraction): Promise<void> {
const query = interaction.options.getString('query')?.trim();
if (!query || query.length === 0) {
return interaction.respond([]);
}
const suggestions = await this.cache.wrap(
getYouTubeAndSpotifySuggestionsFor,
query,
this.spotify,
10,
{
expiresIn: ONE_HOUR_IN_SECONDS,
key: `autocomplete:${query}`,
});
await interaction.respond(suggestions);
}
}
|