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
|
import {AutocompleteInteraction, CommandInteraction} from 'discord.js';
import {URL} from 'url';
import {SlashCommandBuilder} from '@discordjs/builders';
import {inject, injectable} from 'inversify';
import Spotify from 'spotify-web-api-node';
import Command from '.';
import {TYPES} from '../types.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';
import AddQueryToQueue from '../services/add-query-to-queue.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('add track to the front of the queue'))
.addBooleanOption(option => option
.setName('shuffle')
.setDescription('shuffle the input if you\'re adding multiple tracks'));
public requiresVC = true;
private readonly spotify: Spotify;
private readonly cache: KeyValueCacheProvider;
private readonly addQueryToQueue: AddQueryToQueue;
constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue) {
this.spotify = thirdParty.spotify;
this.cache = cache;
this.addQueryToQueue = addQueryToQueue;
}
// eslint-disable-next-line complexity
public async execute(interaction: CommandInteraction): Promise<void> {
await this.addQueryToQueue.addToQueue({
interaction,
query: interaction.options.getString('query')!.trim(),
addToFrontOfQueue: interaction.options.getBoolean('immediate') ?? false,
shuffleAdditions: interaction.options.getBoolean('shuffle') ?? false,
});
}
public async handleAutocompleteInteraction(interaction: AutocompleteInteraction): Promise<void> {
const query = interaction.options.getString('query')?.trim();
if (!query || query.length === 0) {
await interaction.respond([]);
return;
}
try {
// Don't return suggestions for URLs
// eslint-disable-next-line no-new
new URL(query);
await interaction.respond([]);
return;
} catch {}
const suggestions = await this.cache.wrap(
getYouTubeAndSpotifySuggestionsFor,
query,
this.spotify,
10,
{
expiresIn: ONE_HOUR_IN_SECONDS,
key: `autocomplete:${query}`,
});
await interaction.respond(suggestions);
}
}
|