fix(Player): get correct info for youtube video links
This commit is contained in:
parent
9a8db67b44
commit
aecb61f174
4 changed files with 26 additions and 2 deletions
|
@ -1,7 +1,8 @@
|
|||
require("dotenv").config();
|
||||
const { Client, GuildMember } = require("discord.js");
|
||||
const config = require("./config");
|
||||
const { Player, QueryType, QueueRepeatMode } = require("discord-player");
|
||||
// const { Player, QueryType, QueueRepeatMode } = require("discord-player");
|
||||
const { Player, QueryType, QueueRepeatMode } = require("../../");
|
||||
|
||||
const client = new Client({
|
||||
intents: ["GUILD_VOICE_STATES", "GUILD_MESSAGES", "GUILDS"]
|
||||
|
|
|
@ -9,6 +9,7 @@ import YouTube from "youtube-sr";
|
|||
import { Util } from "./utils/Util";
|
||||
import Spotify from "spotify-url-info";
|
||||
import { PlayerError, ErrorStatusCode } from "./Structures/PlayerError";
|
||||
import { getInfo as ytdlGetInfo } from "ytdl-core";
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import { Client as SoundCloud } from "soundcloud-scraper";
|
||||
|
@ -220,6 +221,25 @@ class Player extends EventEmitter<PlayerEvents> {
|
|||
|
||||
const qt = options.searchEngine === QueryType.AUTO ? QueryResolver.resolve(query) : options.searchEngine;
|
||||
switch (qt) {
|
||||
case QueryType.YOUTUBE_VIDEO: {
|
||||
const info = await ytdlGetInfo(query).catch(Util.noop);
|
||||
if (!info) return { playlist: null, tracks: [] };
|
||||
|
||||
const track = new Track(this, {
|
||||
title: info.videoDetails.title,
|
||||
description: info.videoDetails.description,
|
||||
author: info.videoDetails.author?.name,
|
||||
url: info.videoDetails.video_url,
|
||||
requestedBy: options.requestedBy as User,
|
||||
thumbnail: Util.last(info.videoDetails.thumbnails)?.url,
|
||||
views: parseInt(info.videoDetails.viewCount.replace(/[^0-9]/g, "")) || 0,
|
||||
duration: Util.buildTimeCode(Util.parseMS(parseInt(info.videoDetails.lengthSeconds) * 1000)),
|
||||
source: "youtube",
|
||||
raw: info
|
||||
});
|
||||
|
||||
return { playlist: null, tracks: [track] };
|
||||
}
|
||||
case QueryType.YOUTUBE_SEARCH: {
|
||||
const videos = await YouTube.search(query, {
|
||||
type: "video"
|
||||
|
@ -237,6 +257,7 @@ class Player extends EventEmitter<PlayerEvents> {
|
|||
thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"),
|
||||
views: m.views,
|
||||
duration: m.durationFormatted,
|
||||
source: "youtube",
|
||||
raw: m
|
||||
});
|
||||
});
|
||||
|
|
|
@ -216,6 +216,7 @@ export interface ExtractorModelData {
|
|||
* - ARBITRARY
|
||||
* - REVERBNATION
|
||||
* - YOUTUBE_SEARCH
|
||||
* - YOUTUBE_VIDEO
|
||||
* - SOUNDCLOUD_SEARCH
|
||||
* @typedef {string} QueryType
|
||||
*/
|
||||
|
@ -234,6 +235,7 @@ export enum QueryType {
|
|||
ARBITRARY = "arbitrary",
|
||||
REVERBNATION = "reverbnation",
|
||||
YOUTUBE_SEARCH = "youtube_search",
|
||||
YOUTUBE_VIDEO = "youtube_video",
|
||||
SOUNDCLOUD_SEARCH = "soundcloud_search"
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ class QueryResolver {
|
|||
if (SoundcloudValidateURL(query, "track")) return QueryType.SOUNDCLOUD_TRACK;
|
||||
if (SoundcloudValidateURL(query, "playlist") || query.includes("/sets/")) return QueryType.SOUNDCLOUD_PLAYLIST;
|
||||
if (YouTube.isPlaylist(query)) return QueryType.YOUTUBE_PLAYLIST;
|
||||
if (validateID(query) || validateURL(query)) return QueryType.YOUTUBE_SEARCH;
|
||||
if (validateID(query) || validateURL(query)) return QueryType.YOUTUBE_VIDEO;
|
||||
if (spotifySongRegex.test(query)) return QueryType.SPOTIFY_SONG;
|
||||
if (spotifyPlaylistRegex.test(query)) return QueryType.SPOTIFY_PLAYLIST;
|
||||
if (spotifyAlbumRegex.test(query)) return QueryType.SPOTIFY_ALBUM;
|
||||
|
|
Loading…
Reference in a new issue