Fix auto translate search function

This commit is contained in:
Max Isom 2020-10-24 13:11:29 -04:00
parent 7a2cdc20de
commit e103282cd7
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880
2 changed files with 6 additions and 15 deletions

View file

@ -4,13 +4,11 @@ import {toSeconds, parse} from 'iso8601-duration';
import got from 'got'; import got from 'got';
import spotifyURI from 'spotify-uri'; import spotifyURI from 'spotify-uri';
import Spotify from 'spotify-web-api-node'; import Spotify from 'spotify-web-api-node';
import ytsr from 'ytsr';
import YouTube from 'youtube.ts'; import YouTube from 'youtube.ts';
import pLimit from 'p-limit'; import pLimit from 'p-limit';
import uniqueRandomArray from 'unique-random-array'; import uniqueRandomArray from 'unique-random-array';
import {QueuedSong, QueuedPlaylist} from '../services/player'; import {QueuedSong, QueuedPlaylist} from '../services/player';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {parseTime} from '../utils/time';
@injectable() @injectable()
export default class { export default class {
@ -183,23 +181,16 @@ export default class {
return [songs as QueuedSong[], nSongsNotFound, originalNSongs]; return [songs as QueuedSong[], nSongsNotFound, originalNSongs];
} }
private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise<QueuedSong | null> { private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise<QueuedSong | null> {
try { try {
const {items} = await ytsr(`"${track.name}" "${track.artists[0].name}" offical`, {limit: 5}); const {items} = await this.youtube.videos.search({q: `"${track.name}" "${track.artists[0].name}"`, maxResults: 10});
const video = items.find(item => item.type === 'video'); const videoResult = items[0]; // Items.find(item => item.type === 'video');
if (!video || video.type !== 'video') { if (!videoResult) {
throw new Error('No video found for query.'); throw new Error('No video found for query.');
} }
return { return await this.youtubeVideo(videoResult.id.videoId);
title: video.title,
artist: track.artists[0].name,
length: video.duration ? parseTime(video.duration) : 0,
url: video.link,
playlist,
isLive: video.live
};
} catch (_: unknown) { } catch (_: unknown) {
return null; return null;
} }

View file

@ -83,7 +83,7 @@ export default class {
} }
const stream = await this.getStream(currentSong.url, {seek: positionSeconds}); const stream = await this.getStream(currentSong.url, {seek: positionSeconds});
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus', bitrate: 'auto'});
this.attachListeners(); this.attachListeners();
this.startTrackingPosition(positionSeconds); this.startTrackingPosition(positionSeconds);