gpt4 book ai didi

javascript - 如何定位不和谐机器人连接的语音聊天

转载 作者:行者123 更新时间:2023-12-04 22:48:05 27 4
gpt4 key购买 nike

我正在制作一个通过语音识别激活的不和谐机器人,我现在一开始就让他加入语音 channel (正在工作),我试图发出命令让他离开。

const commando = require('discord.js-commando');

class LeaveChannelCommand extends commando.Command
{
constructor(client){!
super(client,{
name: 'leave',
group: 'music',
memberName: 'leave',
description: 'leaves a voice channel'
});
}
async run(message, args)
{
if(message.guild.voiceConnection)
{
message.guild.voiceConnection.disconnect();
}
else
{
message.channel.sendMessage("seccessfully left")
}
}
}

module.exports = LeaveChannelCommand;


现在你可以在服务器的任何地方输入 !leave 并且机器人离开,
我想让他只能从同一个语音 channel 控制,
我应该怎么办

最佳答案

这很容易做到。您需要做的就是抓取机器人的语音 channel 和用户的语音 channel (如果他在一个)并检查它们是否相同。

您可以在下面找到一些示例代码。试一试,让我知道它是怎么回事。

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!');
}
}

关于javascript - 如何定位不和谐机器人连接的语音聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com