java—使用jda在不一致消息中创建倒计时计时器

ldfqzlk8  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(404)

我试图在jda中创建一个倒计时计时器。不过,我认为我做得不对。输入的原始命令将不断被编辑以显示正在进行的倒计时。倒计时的最大时间为48小时,如for循环中所示。以下是我的代码:

@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    super.onGuildMessageReceived(event);

    //!countdown hours:minutes:seconds
    if (event.getMessage().getContentRaw().startsWith("!countdown")) {
        String timebuilder = "";
        char[] chararr = event.getMessage().getContentRaw().toCharArray();

        for (int i = 11; i < chararr.length; i++) {
            timebuilder += chararr[i];
        }
    //  event.getChannel().sendMessage(timebuilder).queue();
        int hour = Integer.parseInt(timebuilder.substring(0, 1));
        int minute = Integer.parseInt(timebuilder.substring(3, 4));
        int second = Integer.parseInt(timebuilder.substring(6, 7));

        for (int i = 0; i < 172800; i++ ) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 0 0 0
            if (hour == 0 && minute == 0 && second == 0) {
                event.getMessage().editMessage("00:00:00");
                event.getChannel().sendMessage("Countdown complete!").queue();
                break;
            }
            // 0 1 1
            else if (hour == 0 && minute > 0 && second > 0) {
                minute--;
                second--;
                event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
            }
            //0 1 0
            else if (hour == 0 && minute > 0 && second == 0) {
                minute--;
                second = 59;
                event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
            }
            //1 0 0 
            else if (hour > 0 && minute == 0 && second == 0) {
                second = 59;
                minute = 59;
                hour--;
                event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
            }
            //0 0 1 //1 0 1 //1 1 1 
            else if ((hour == 0 && minute == 0 && second > 0) || (hour > 0 && minute == 0 && second > 0) || (hour > 0 && minute > 0 && second > 0)) {
                second--;
                event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();
            }

            //1 1 0
            else if (hour > 0 && minute > 0 && second == 0) {
                minute--;
                event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();
            }

        }
    }
}

似乎有更简单的方法。用定时器或simpledateformat之类的,但我不知道那会是什么。由于某些原因,我的邮件没有使用 .editMessage() 方法。
输出:

!countdown 01:02:02 //user keyed discord bot command

对于预期输出,“!倒计时01:02:02“应使用更新的倒计时持续编辑消息。如下图所示:
预期产量:

!countdown 01:02:02 //user keyed discord bot command
01:02:01 //original command
01:02:00 //original command
01:01:59 //original command

etc...
np8igboo

np8igboo1#

你应该能够使用定时器和定时器任务,定时器是用来安排定时器任务。计时器用于在线程的后台安排任务,如下所示:

TimerTask task = new TimerTask(){
    public void run(){
        System.out.println("Code to be executed");
    }
};

Timer timer = new Timer();
// The delay period is calculated in milliseconds iirc
timer.schedule(task, 5*1000);

相关问题