【发布时间】:2022-01-26 15:25:46
【问题描述】:
我一辈子都想不通为什么 JDA 没有为给定的调用返回正确的对象。
我以这种方式创建实例:
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对象总是被实例化,但大部分信息是假的/空的。
文档说要能够使用“GuildVoiceState”,我需要启用“CacheFlag.VOICE_STATE”,但无论我做什么,“inAudioChannel”总是返回 false。 授予机器人管理员权限,并在短信上对其进行了测试,这很有效。
有什么想法吗?
【问题讨论】:
-
你是如何构建你的 JDA 实例的?
-
我更新了代码示例。我基本上遵循文档中的 EchoPlayer 示例。
标签: java discord-jda