Connection.play 不是函数

Connection.play is not a function

我只是尝试在 javascript 中创建 discord bot,我希望,当有人加入语音聊天时,bot 也会加入。但是当我尝试时,bot 会加入,但我会得到一个错误。我从这一天开始,只是为了好玩。如果谁有什么东西,请告诉我,我是初学者。

const Discord = require('discord.js');
const client = new Discord.Client();

const prefix = 'more '

client.once('ready', () => {
    console.log('Hello ')
});



client.on('message', async message =>{

    //if(!message.content.startsWith(prefix) || message.author.bot) return;

    if (message.content === '%play') {
        // Join the same voice channel of the author of the message
        if (message.member.voice.channel) {
            const connection = await message.member.voice.channel.join();
            // Play audio, see below
            const dispatcher = connection.play('sound.mp3');
            dispatcher.on('finish', () => message.member.voice.channel.leave());
        }};
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'play'){
        message.channel.send('%play');
        return;
    }



});


client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => { // Listeing to the voiceStateUpdate event
    if (newVoiceState.channel) { // The member connected to a channel.
        const connection = newVoiceState.channel.join();
            // Play audio, see below
            const dispatcher = connection.play('sound.mp3');
            dispatcher.on('finish', () => newVoiceState.channel.leave());
    } else if (oldVoiceState.channel) { // The member disconnected from a channel.
        console.log(`${oldVoiceState.member.user.tag} disconnected from ${oldVoiceState.channel.name}.`)
    };
});

client.login('NjI2ODQ0NDIzOTY3MzQyNjAz.XY0AXA.WEghPj9EZkLFPblsORUhVgvNkYI');

这是错误:

TypeError: newVoiceState.channel.play is not a function
    at Client.client.on (C:\Users\Admin\Desktop\DiscordBot\main.js:41:54)
    at Client.emit (events.js:198:13)
    at VoiceStateUpdate.handle (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:825:20)

感谢您的帮助。

您需要在这段代码中添加await关键字:const connection = newVoiceState.channel.join();,它会变成const connection = await newVoiceState.channel.join();。这应该工作!另一种方法是在连接函数上使用 .then,因为它 returns 是一个承诺。在这里阅读更多相关信息: https://discord.js.org/#/docs/main/stable/class/VoiceChannel?scrollTo=join