1

So I have a discord bot where I am trying to send a message every hour. I got that part of it working using the scheduled executor service. It starts when I say !futureson

I want to be able to stop it when I send !futuresoff

However, I cannot get this to work properly. Here is my code:

    String[] args = event.getMessage().getContentRaw().split(" ");
    if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
       Runnable futRun = new Runnable() {
       public void run() {
        EmbedBuilder futuresBot = new EmbedBuilder();
        futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());
   
        try {
    futuresBot.addField("**S&P 500**", getSPY(), false);
    futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
    futuresBot.addField("**DOW**", getDOW(), false);
    if (getSPY().contains("+")) {
    futuresBot.setColor(green);
    } 
    else {
    futuresBot.setColor(red);
    }
        }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
   
   
        futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
        event.getChannel().sendMessage(futuresBot.build()).queue();
   
       }
      
    };
    
    exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);
    
    
    }
    if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("Futures bot off.").queue();
    exec.shutdownNow();
    }

I have tried this a few different ways, but I can't get it to work properly. It is getting to the !futuresoff part because it sends the Futures bot off message. I just want it to be able to send the message once an hour when the !futureson has been sent and not when !futuresoff is sent. I also tried ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(0);

I would appreciate any help or someone that could point me in the right direction.

Juice
  • 23
  • 3
  • 2
    seems that you will need to use cancel, cf.duplicate https://stackoverflow.com/q/14889143/1827276 – boly38 Aug 01 '20 at 21:43

1 Answers1

1

First you should save the result of scheduleAtFixedRate() inside your Listener class.

ScheduledFuture scheduledFuture = null;

Then you can use cancel() on this object to stop it. You also should test if it is null before scheduling and test if it is set before canceling:

String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase("!futuresOn")) {
    Color red = new Color(255, 0, 0);
    Color green = new Color(0, 204, 0);
    Runnable futRun = new Runnable() {
        public void run() {
            EmbedBuilder futuresBot = new EmbedBuilder();
            futuresBot.setAuthor("Futures Bot", null, event.getAuthor().getAvatarUrl());

            try {
                futuresBot.addField("**S&P 500**", getSPY(), false);
                futuresBot.addField("**NASDAQ**", getNASDAQ(), false);
                futuresBot.addField("**DOW**", getDOW(), false);
                if (getSPY().contains("+")) {
                    futuresBot.setColor(green);
                }
                else {
                    futuresBot.setColor(red);
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            futuresBot.setFooter("Cantina Capital", event.getGuild().getIconUrl());
            event.getChannel().sendMessage(futuresBot.build()).queue();

        }

    };

    if (scheduledFuture == null)
        scheduledFuture = exec.scheduleAtFixedRate(futRun, 0, 1, TimeUnit.HOURS);


}
if (args[0].equalsIgnoreCase("!futuresoff")) {
    event.getChannel().sendMessage("Futures bot off.").queue();
    if (scheduledFuture != null) {
        scheduledFuture.cancel(true);
        scheduledFuture = null;
    }
}
    ```
Tobias Miosczka
  • 213
  • 2
  • 8