【发布时间】:2021-11-15 04:35:19
【问题描述】:
我正在尝试编写一个 Discord 机器人,以使用 YTDL 在 YouTube 上搜索歌曲,然后在 Discord 语音频道中播放歌曲。
我能够从保存在我电脑上的文件中播放一首歌曲,所以我知道它可以正常工作并且音量很好。每当我尝试将 YTDL 结果传递给音频播放器时,我都会收到错误消息。如果我直接尝试视频 URL,我不会收到错误消息,但仍然没有播放任何内容。
这是我的播放器代码:
const ytdl = require('ytdl-core-discord');
const ytSearch = require('yt-search');
const { createAudioPlayer, createAudioResource, joinVoiceChannel, AudioResource, StreamType } = require('@discordjs/voice');
module.exports = {
name: 'play',
description: 'joins and plays music',
async execute(message, args) {
const voiceChannel = message.member.voice.channel;
//if command is empty
if(!args.length) return message.channel.send('You need to provide a URL!');
//If user is in VC, join same channel
if (voiceChannel) {
const player = createAudioPlayer();
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
}).subscribe(player);
//Search for vid and return first result
const vidFind = async (query) =>{
const vidRes = await ytSearch(query);
return (vidRes.videos.length > 1) ? vidRes.videos[0]:null;
}
const video = await vidFind(args.join(''));
//If there is a video result, save it as an audio resource and play it through audio player
if(video){
const stream = ytdl(video.url, {filter: "audioonly"})
const resource = createAudioResource(stream);
player.play(resource);
}
} else {
message.channel.send('You need to be in a VC first!');
}
}
}
这是我得到的错误:
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of WebmDemuxer
at new NodeError (node:internal/errors:371:5)
at _write (node:internal/streams/writable:312:13)
at Socket.Writable.write (node:internal/streams/writable:334:10)
at Duplexify.ondata (node:internal/streams/readable:754:22)
at Duplexify.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Duplexify.Readable.push (node:internal/streams/readable:228:10)
at node:internal/streams/duplexify:180:13
at runMicrotasks (<anonymous>) {
code: 'ERR_INVALID_ARG_TYPE'
我曾尝试使用creatReadStream 函数,但我发现它只适用于本地文件。我曾尝试查看其他 Discord v13 机器人,但它们似乎根本不使用 createAudioResource,而是创建自己的自定义函数来创建音频资源。
任何帮助都将不胜感激!
【问题讨论】:
标签: javascript node.js discord discord.js