mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-08-05 23:43:30 +02:00
parent
da72cd708b
commit
59cbc8b474
12 changed files with 317 additions and 190 deletions
|
@ -12,6 +12,7 @@ import errorMsg from '../utils/error-msg.js';
|
|||
import Command from '.';
|
||||
import GetSongs from '../services/get-songs.js';
|
||||
import {prisma} from '../utils/db.js';
|
||||
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
|
||||
|
||||
@injectable()
|
||||
export default class implements Command {
|
||||
|
@ -57,143 +58,154 @@ export default class implements Command {
|
|||
const res = new LoadingMessage(msg.channel as TextChannel);
|
||||
await res.start();
|
||||
|
||||
const player = this.playerManager.get(msg.guild!.id);
|
||||
|
||||
const wasPlayingSong = player.getCurrent() !== null;
|
||||
|
||||
if (args.length === 0) {
|
||||
if (player.status === STATUS.PLAYING) {
|
||||
await res.stop(errorMsg('already playing, give me a song name'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Must be resuming play
|
||||
if (!wasPlayingSong) {
|
||||
await res.stop(errorMsg('nothing to play'));
|
||||
return;
|
||||
}
|
||||
|
||||
await player.connect(targetVoiceChannel);
|
||||
await player.play();
|
||||
|
||||
await res.stop('the stop-and-go light is now green');
|
||||
return;
|
||||
}
|
||||
|
||||
const addToFrontOfQueue = args[args.length - 1] === 'i' || args[args.length - 1] === 'immediate';
|
||||
const shuffleAdditions = args[args.length - 1] === 's' || args[args.length - 1] === 'shuffle';
|
||||
|
||||
let newSongs: Array<Except<QueuedSong, 'addedInChannelId'>> = [];
|
||||
let extraMsg = '';
|
||||
|
||||
// Test if it's a complete URL
|
||||
try {
|
||||
const url = new URL(args[0]);
|
||||
const player = this.playerManager.get(msg.guild!.id);
|
||||
|
||||
const YOUTUBE_HOSTS = [
|
||||
'www.youtube.com',
|
||||
'youtu.be',
|
||||
'youtube.com',
|
||||
'music.youtube.com',
|
||||
'www.music.youtube.com',
|
||||
];
|
||||
const wasPlayingSong = player.getCurrent() !== null;
|
||||
|
||||
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 res.stop(errorMsg('that doesn\'t exist'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
|
||||
const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0], playlistLimit);
|
||||
|
||||
if (totalSongs > playlistLimit) {
|
||||
extraMsg = `a random sample of ${playlistLimit} songs was taken`;
|
||||
if (args.length === 0) {
|
||||
if (player.status === STATUS.PLAYING) {
|
||||
await res.stop(errorMsg('already playing, give me a song name'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (totalSongs > playlistLimit && nSongsNotFound !== 0) {
|
||||
extraMsg += ' and ';
|
||||
// Must be resuming play
|
||||
if (!wasPlayingSong) {
|
||||
await res.stop(errorMsg('nothing to play'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (nSongsNotFound !== 0) {
|
||||
if (nSongsNotFound === 1) {
|
||||
extraMsg += '1 song was not found';
|
||||
} else {
|
||||
extraMsg += `${nSongsNotFound.toString()} songs were not found`;
|
||||
}
|
||||
}
|
||||
await player.connect(targetVoiceChannel);
|
||||
await player.play();
|
||||
|
||||
newSongs.push(...convertedSongs);
|
||||
}
|
||||
} catch (_: unknown) {
|
||||
// Not a URL, must search YouTube
|
||||
const query = addToFrontOfQueue ? args.slice(0, args.length - 1).join(' ') : args.join(' ');
|
||||
await Promise.all([
|
||||
res.stop('the stop-and-go light is now green'),
|
||||
msg.channel.send({embeds: [buildPlayingMessageEmbed(player)]}),
|
||||
]);
|
||||
|
||||
const song = await this.getSongs.youtubeVideoSearch(query);
|
||||
|
||||
if (song) {
|
||||
newSongs.push(song);
|
||||
} else {
|
||||
await res.stop(errorMsg('that doesn\'t exist'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (newSongs.length === 0) {
|
||||
await res.stop(errorMsg('no songs found'));
|
||||
return;
|
||||
}
|
||||
const addToFrontOfQueue = args[args.length - 1] === 'i' || args[args.length - 1] === 'immediate';
|
||||
const shuffleAdditions = args[args.length - 1] === 's' || args[args.length - 1] === 'shuffle';
|
||||
|
||||
if (shuffleAdditions) {
|
||||
newSongs = shuffle(newSongs);
|
||||
}
|
||||
let newSongs: Array<Except<QueuedSong, 'addedInChannelId' | 'requestedBy'>> = [];
|
||||
let extraMsg = '';
|
||||
|
||||
newSongs.forEach(song => {
|
||||
player.add({...song, addedInChannelId: msg.channel.id}, {immediate: addToFrontOfQueue});
|
||||
});
|
||||
// Test if it's a complete URL
|
||||
try {
|
||||
const url = new URL(args[0]);
|
||||
|
||||
const firstSong = newSongs[0];
|
||||
const YOUTUBE_HOSTS = [
|
||||
'www.youtube.com',
|
||||
'youtu.be',
|
||||
'youtube.com',
|
||||
'music.youtube.com',
|
||||
'www.music.youtube.com',
|
||||
];
|
||||
|
||||
let statusMsg = '';
|
||||
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 (player.voiceConnection === null) {
|
||||
await player.connect(targetVoiceChannel);
|
||||
if (song) {
|
||||
newSongs.push(song);
|
||||
} else {
|
||||
await res.stop(errorMsg('that doesn\'t exist'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
|
||||
const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0], playlistLimit);
|
||||
|
||||
// Resume / start playback
|
||||
await player.play();
|
||||
if (totalSongs > playlistLimit) {
|
||||
extraMsg = `a random sample of ${playlistLimit} songs was taken`;
|
||||
}
|
||||
|
||||
if (wasPlayingSong) {
|
||||
statusMsg = 'resuming playback';
|
||||
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 query = addToFrontOfQueue ? args.slice(0, args.length - 1).join(' ') : args.join(' ');
|
||||
|
||||
const song = await this.getSongs.youtubeVideoSearch(query);
|
||||
|
||||
if (song) {
|
||||
newSongs.push(song);
|
||||
} else {
|
||||
await res.stop(errorMsg('that doesn\'t exist'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build response message
|
||||
if (statusMsg !== '') {
|
||||
if (extraMsg === '') {
|
||||
extraMsg = statusMsg;
|
||||
if (newSongs.length === 0) {
|
||||
await res.stop(errorMsg('no songs found'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (shuffleAdditions) {
|
||||
newSongs = shuffle(newSongs);
|
||||
}
|
||||
|
||||
newSongs.forEach(song => {
|
||||
player.add({...song, addedInChannelId: msg.channel.id, requestedBy: msg.author.id}, {immediate: addToFrontOfQueue});
|
||||
});
|
||||
|
||||
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 msg.channel.send({embeds: [buildPlayingMessageEmbed(player)]});
|
||||
}
|
||||
|
||||
// Build response message
|
||||
if (statusMsg !== '') {
|
||||
if (extraMsg === '') {
|
||||
extraMsg = statusMsg;
|
||||
} else {
|
||||
extraMsg = `${statusMsg}, ${extraMsg}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (extraMsg !== '') {
|
||||
extraMsg = ` (${extraMsg})`;
|
||||
}
|
||||
|
||||
if (newSongs.length === 1) {
|
||||
await res.stop(`u betcha, **${firstSong.title}** added to the${addToFrontOfQueue ? ' front of the' : ''} queue${extraMsg}`);
|
||||
} else {
|
||||
extraMsg = `${statusMsg}, ${extraMsg}`;
|
||||
await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (extraMsg !== '') {
|
||||
extraMsg = ` (${extraMsg})`;
|
||||
}
|
||||
|
||||
if (newSongs.length === 1) {
|
||||
await res.stop(`u betcha, **${firstSong.title}** added to the${addToFrontOfQueue ? ' front of the' : ''} queue${extraMsg}`);
|
||||
} else {
|
||||
await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
|
||||
} catch (error) {
|
||||
await res.stop();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue