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.