对于 inAudioChannel(),GuildVoiceState 总是 returns false

GuildVoiceState always returns false for inAudioChannel()

我想不通为什么 JDA 没有 return 给定调用的正确对象。

我这样创建实例:

public class CatBot extends ListenerAdapter {

private static final String TOKEN = "temporary";
private static final Logger LOGGER = LoggerFactory.getLogger(CatBot.class);

public static void main(String[] args) throws LoginException
{
    JDABuilder.createLight(TOKEN)
            .enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_VOICE_STATES)
            .addEventListeners(new CatBot())
            .setActivity(Activity.playing("Here for the Sel"))
            .setStatus(OnlineStatus.ONLINE)
            .enableCache(CacheFlag.VOICE_STATE)
            .build();
}

@Override
public void onMessageReceived(MessageReceivedEvent messageEvent) {
    Message message = messageEvent.getMessage();
    User author =  message.getAuthor();
    String content = message.getContentRaw();
    Guild guild = messageEvent.getGuild();

    // avoid spam by filtering bots
    if (messageEvent.getAuthor().isBot())
        return;

    // We only want to handle messages in Guilds
    if (!messageEvent.isFromGuild()) {
        return;
    }

    // handling the case of messages that aren't in the command list
    if (!content.equals("!elcat")){
        MessageChannel channel = messageEvent.getChannel();
        channel.sendMessage("<@" + author.getId() + "> OH NO YOU'RE A DOG").queue();
    } else {
        Member member = messageEvent.getMember();
        GuildVoiceState voiceState = member.getVoiceState();
        LOGGER.info("Someone used the cat command {}", member);
            if (voiceState.inAudioChannel()) {
                AudioChannel channel = voiceState.getChannel();
                connectTo(guild, channel);
            }
        LOGGER.info("Seems like I can't get into the voice channel");
    }


}

当我的机器人收到命令时,它应该检查用户是否在语音通道中以连接到该通道并说“嗨”。这应该发生在 else 块中。 目前,代码停在这一行:

if (voiceState.inAudioChannel())

我试过调试,voiceState对象总是被实例化,但最多的信息是false/empty。

文档说要能够使用“GuildVoiceState”,我需要启用“CacheFlag.VOICE_STATE”,但无论我做什么,“inAudioChannel”总是 returns错误的。 授予机器人管理员权限并在短信上对其进行测试并且有效。

有什么想法吗?

语音状态缓存直接链接到成员缓存。由于您使用 createLight 禁用了所有成员缓存,因此附加到这些成员的语音状态也将无法正确缓存。

您需要通过 setMemberCachePolicy(MemberCachePolicy.VOICE) 在您的 JDABuilder 实例上启用语音成员缓存。