You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为JDA命令的finishDate选项配置MM/dd/yyyy HH格式自动补全?

解决JDA中lottery命令finishDate参数自动补全问题

你当前的代码已经将finishDate参数设置为支持自动补全(最后一个true参数),但JDA并不会自动生成指定日期格式的补全建议,需要通过自定义自动补全监听器来实现这个需求,具体步骤如下:

1. 注册自动补全监听器

在初始化JDA的代码中,添加自定义的自动补全监听器:

jda.addEventListener(new LotteryAutoCompleteListener());

2. 实现自动补全监听器类

创建监听器类,针对lottery命令的finishDate参数生成符合MM/dd/yyyy HH格式的补全建议。示例中会生成当前日期及次日的几个整点时间选项,你可以根据需求调整生成逻辑:

import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.Command;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class LotteryAutoCompleteListener extends ListenerAdapter {
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy HH");

    @Override
    public void onAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
        // 只处理lottery命令的finishDate参数
        if (!event.getName().equals("lottery") || !event.getFocusedOption().getName().equals("finishDate")) {
            return;
        }

        List<Command.Choice> suggestions = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        // 生成当前时间后3个整点的选项
        for (int i = 0; i < 3; i++) {
            LocalDateTime targetTime = now.plusHours(i).withMinute(0).withSecond(0);
            suggestions.add(new Command.Choice(targetTime.format(DATE_FORMATTER), targetTime.format(DATE_FORMATTER)));
        }

        // 生成次日的3个整点选项
        LocalDateTime tomorrow = now.plusDays(1).withHour(9).withMinute(0).withSecond(0);
        for (int i = 0; i < 3; i++) {
            LocalDateTime targetTime = tomorrow.plusHours(i);
            suggestions.add(new Command.Choice(targetTime.format(DATE_FORMATTER), targetTime.format(DATE_FORMATTER)));
        }

        // 发送补全建议
        event.replyChoices(suggestions).queue();
    }
}

3. 验证效果

当用户在Discord中输入/lottery命令并聚焦到finishDate参数时,就会看到你自定义的MM/dd/yyyy HH格式的日期时间选项,直接选择即可完成输入。

内容的提问来源于stack exchange,提问作者Loso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 00:55:34