这很容易做到。您需要做的就是抓住机器人的voiceChannel和用户的voiceChannel(如果他在一个)并检查它们是否相同。
您可以在下面找到一些示例代码。试一试,让我知道它是怎么回事。
async run(message, args) { // If the client isn't in a voiceChannel, don't execute any other code if(!message.guild.voiceConnection) { return; } // Get the user's voiceChannel (if he is in one) let userVoiceChannel = message.member.voiceChannel; // Return from the code if the user isn't in a voiceChannel if (!userVoiceChannel) { return; } // Get the client's voiceConnection let clientVoiceConnection = message.guild.voiceConnection; // Compare the voiceChannels if (userVoiceChannel === clientVoiceConnection.channel) { // The client and user are in the same voiceChannel, the client can disconnect clientVoiceConnection.disconnect(); message.channel.send('Client has disconnected!'); } else { // The client and user are NOT in the same voiceChannel message.channel.send('You can only execute this command if you share the same voiceChannel as the client!'); } }