javascript - node-ytdl-core miniget 错误:状态码:410
我目前正在制作一个 Discord.js 音乐机器人。我使用 ytdl-core 来获取视频信息(例如 URL、标题、作者名字、年龄限制 等)。当我遇到有年龄限制的视频时,我希望通过某种方式绕过年龄限制(这是更好的选择)或者直接返回。问题是,在第一次调用 ytdl.getInfo() 函数时,如果视频有年龄限制,它会给我一个错误。在此函数调用之前,我无法知道视频是否有限制。
这是完整的错误信息:
Error: Status code: 410
at ClientRequest.<anonymous> (SOME_PATH\node_modules\miniget\src\index.ts:255:19)
at Object.onceWrapper (node:events:514:26)
at ClientRequest.emit (node:events:394:28)
at HTTPParser.parserOnIncomingClient (node:_http_client:621:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
at TLSSocket.socketOnData (node:_http_client:487:22)
at TLSSocket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:312:12)
at readableAddChunk (node:internal/streams/readable:287:9)
at TLSSocket.Readable.push (node:internal/streams/readable:226:10)
这是代码(用 TypeScript 编写):
import {
AudioPlayerStatus,
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
StreamType,
} from "@discordjs/voice";
import { Command, Song } from "../../Interfaces";
import ytdl from "ytdl-core";
import { Message, VoiceChannel } from "discord.js";
export const command: Command = {
name: "play",
description: "",
usage: "play",
category: "Music",
run: async (client, message, args) => {
const channel = message.member.voice?.channel;
if (!channel)
return message.channel.send(
"You are not in a voice channel. Please join a voice channel and try again."
);
const query = args.join(" ");
if (ytdl.validateURL(query)) {
let { age_restricted, author, thumbnails, title, video_url } = (
await ytdl.getInfo(query)
).videoDetails;
if (age_restricted) return message.reply("Age restricted video");
let song: Song = {
title,
url: video_url,
thumbnail: thumbnails[thumbnails.length - 1].url,
author: author.name,
};
play(message, <VoiceChannel>channel, song);
}
},
};
function play(message: Message, channel: VoiceChannel, song: Song) {
const player = createAudioPlayer();
const resource = createAudioResource(
ytdl(song.url, {
filter: "audioonly",
requestOptions: {
headers: {
cookie: "MY_COOKIE"
// Tried for this https://github.com/fent/node-ytdl-core/blob/master/example/cookies.js
}
}
}),
{
inputType: StreamType.Arbitrary,
}
);
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
player.play(resource);
connection.subscribe(player);
player.on(AudioPlayerStatus.Idle, () => connection.destroy());
}