我正在尝试通过Discord.js/voice和ytdl播放YT音频。我有这个代码(在play命令中)
...
const voiceChannel = message.member.voice.channel;
// console.log(args, voiceChannel)
if (!voiceChannel)
return message.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}
const songInfo = await ytdl.getInfo(args[0]);
const song = {
title: songInfo.videoDetails.title,
url: songInfo.videoDetails.video_url,
};
console.log(song)
let conn = await voice.joinVoiceChannel({
channelId: voiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false
})
let conn2= voice.getVoiceConnection(voiceChannel.id)
console.log(conn2)
const stream = ytdl(song.url)
conn2.subscribe(stream)
...机器人拥有所有的烫发,在我所在的通道中正确加入,但在conn2.subscribe(stream)行给出了错误:
TypeError: Cannot read properties of undefined (reading 'subscribe')
at Object.execute (%mypath%\musicbot2\commands\play.js:39:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)我找不到关于这个错误的任何东西。如何正确播放音乐/音频?在这种情况下,使用ytdl,也可以使用其他站点和包。
谢谢
发布于 2021-10-19 16:28:18
getVoiceConnection函数接受一个行会id,而不是一个渠道id。
将其更改为以下内容应该是可行的:
let conn2 = voice.getVoiceConnection(message.guild.id)https://stackoverflow.com/questions/69633282
复制相似问题