繁体   English   中英

无法读取未定义的属性(读取“打开”)

[英]Cannot read properties of undefined (reading 'on')

当我尝试在频道上播放音乐并使用命令加入和播放时,我收到错误消息: connection.subscribe(player) ReferenceError: connection is not defined机器人加入语音聊天但尝试播放歌曲时出现错误。 我的 discord.js 版本是 13.7.0。 可能我只是在错误的地方定义了连接,但我不知道具体在哪里。 我将不胜感激任何帮助。

 const ytdl = require('ytdl-core'); const ytSearch = require('yt-search'); const { joinVoiceChannel, createAudioPlayer, createAudioResource, VoiceConnectionStatus } = require('@discordjs/voice'); const message = require('../events/guild/message'); const queue = new Map(); // queue (message.guild.id, queue_constructor object { voice channel, text channel, connection, song[]}); module.exports = { name: 'play', aliases: ['skip', 'stop'], description: 'Advanced music bot', async execute(message, args, cmd, client, discord) { const voice_channel = message.member.voice.channel; if (!voice_channel) return message.channel.send('You need to be in a channel to execute this command'); const permissions = voice_channel.permissionsFor(message.client.user); if (!permissions.has('CONNECT')) return message.channel.send('You dont have permission to do that'); if (!permissions.has('SPEAK')) return message.channel.send('You dont have permission to do that'); const server_queue = queue.get(message.guild.id); if (cmd === 'play') { if (!args.length) return message.channel.send('You need to send the second argument'); let song = {}; if (ytdl.validateURL(args[0])) { const song_info = await ytdl.getInfo(args[0]); song = { title: song_title.videoDetails.title, url: song_info.videoDetails.video_url } } else { //If the video is not a URL then use keywords to find that video. const video_finder = async (query) => { const videoResult = await ytSearch(query); return (videoResult.videos.length > 1) ? videoResult.videos[0] : null; } const video = await video_finder(args.join(' ')); if (video) { song = { title: video.title, url: video.url } } else { message.channel.send('Error finding your video'); } } if (!server_queue) { const queue_constructor = { voice_channel: voice_channel, text_channel: message.channel, connection: null, songs: [] } queue.set(message.guild.id, queue_constructor); queue_constructor.songs.push(song); try { const connection = await joinVoiceChannel({ channelId: message.member.voice.channel.id, guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator }) video_player(message.guild, queue_constructor.songs[0]); } catch (err) { queue.delete(message.guild.id); message.channel.send('There was an error connecting'); throw err; } } else { server_queue.songs.push(song); return message.channel.send(`<:seelio:811951350660595772> **${song.title}** added to queue`); } } else if (cmd === 'skip') skip_song(message, server_queue); else if (cmd === 'stop') stop_song(message, server_queue); } } const video_player = async (guild, song) => { const song_queue = queue.get(guild.id); if (!song) { song_queue.voice_channel.leave(); queue.delete(guild.id); return; } const stream = ytdl(song.url, { filter: 'audioonly' }); const resource = createAudioResource(stream); const player = createAudioPlayer(); connection.subscribe(player) player.play(resource) .on('idle', () => { song_queue.songs.shift(); video_player(guild, song_queue.songs[0]); }); await song_queue.text_channel.send(`👍 Now Playing **${song.title}**`) }

这是有问题的片段:

const video_player = async (guild, song) => {
    const song_queue = queue.get(guild.id);

    if (!song) {
        song_queue.voice_channel.leave();
        queue.delete(guild.id);
        return;
    }
    const stream = ytdl(song.url, { filter: 'audioonly' });

    const resource = createAudioResource(stream);
    const player = createAudioPlayer();
    connection.subscribe(player)
    player.play(resource)
    .on('idle', () => {
        song_queue.songs.shift();
        video_player(guild, song_queue.songs[0]);
    });
await song_queue.text_channel.send(`👍 Now Playing **${song.title}**`)
}

connection是在代码的早期部分中定义的,并且该变量在当前范围内不存在。 您可以做的一件事是创建一个名为connection的全局变量并为其分配一个值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM