使用JDA开发Discord斜杠命令后出现应用未响应提示问题
解决Discord斜杠命令发送Embed后提示“The application did not respond”的问题
Discord的斜杠命令有3秒响应时限,你的代码只是向频道发送了消息,但没有对SlashCommandInteractionEvent本身进行响应,所以Discord会判定应用未响应,弹出提示。
方案1:直接用交互事件回复(推荐)
把发送消息的逻辑改成通过event.reply()实现,这样既完成了Discord要求的交互响应,又能正常发送Embed和按钮:
@Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { String roles = String.valueOf(event.getMember().getRoles()); if (event.getName().equals("whosthatpokemon")) { Color relaxo = new Color(44, 106, 124); EmbedBuilder embed = new EmbedBuilder(); embed.setColor(relaxo); embed.setTitle("Who's that Pokemon?"); embed.setDescription("Who is it?"); embed.setImage("http://cdn.poll-maker.com/7-375248/snorlax.png?sz=1200-000000100053"); // 替换原频道发送逻辑,用交互事件直接回复 event.replyEmbeds(embed.build()).setActionRow(sendButtons()).queue(); } } private static java.util.List<Button> sendButtons() { List<Button> buttons = new ArrayList<>(); buttons.add(Button.danger("Snorlax", "Snorlax")); buttons.add(Button.success("Pickachu", "Pickachu")); buttons.add(Button.primary("Bulbasaur", "Bulbasaur")); return buttons; }
方案2:延迟响应(适合耗时操作场景)
如果你的命令需要执行查询数据库、调用API这类耗时任务,可以先调用deferReply()告知Discord“正在处理”,避免超时,之后再编辑回复内容:
@Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { String roles = String.valueOf(event.getMember().getRoles()); if (event.getName().equals("whosthatpokemon")) { // 先发送延迟响应,避免Discord判定超时 event.deferReply().queue(); // 这里可以插入耗时操作逻辑 Color relaxo = new Color(44, 106, 124); EmbedBuilder embed = new EmbedBuilder(); embed.setColor(relaxo); embed.setTitle("Who's that Pokemon?"); embed.setDescription("Who is it?"); embed.setImage("http://cdn.poll-maker.com/7-375248/snorlax.png?sz=1200-000000100053"); // 编辑延迟后的回复,发送Embed和按钮 event.getHook().editOriginalEmbeds(embed.build()).setActionRow(sendButtons()).queue(); } } private static java.util.List<Button> sendButtons() { List<Button> buttons = new ArrayList<>(); buttons.add(Button.danger("Snorlax", "Snorlax")); buttons.add(Button.success("Pickachu", "Pickachu")); buttons.add(Button.primary("Bulbasaur", "Bulbasaur")); return buttons; }
内容的提问来源于stack exchange,提问作者LazyBlu
相关产品推荐
相关产品推荐

