Search code examples
javacountdowndiscord-jda

Creating a countdown timer in a Discord message using JDA


I am trying to create a countdown timer in JDA. I do not think I am doing it right though. The original command entered would be constantly edited to show the countdown in progress. The countdown would have a max time of 48 hours as seen in the for loop. Here is the code I have:

@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();
            }
            
        }
    }
}

It seems like there would be an easier way to do this. With Timer or SimpleDateFormat or something but I just don't know what that would be. For some reason my message is not being edited with the .editMessage() method.

Output:

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

For the expected output, the "!countdown 01:02:02" message should be continually edited with the updated countdown. As shown below:

Expected output:

!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...

Solution

  • You should be able to use a Timer and a TimerTask, the Timer is used to schedule the Timer Task. Timers are used to schedule tasks in the background of a thread like so:

    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);