Improve registered commands caching
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Discord's API is slow in updating and registering new commands, so we set up a runnable to periodically check.
This commit is contained in:
parent
b015fddf3c
commit
163619a7f8
@ -37,7 +37,7 @@ public class Configuration
|
||||
private static final String botName = "HidekoBot";
|
||||
private static final Color botColor = Color.PINK;
|
||||
|
||||
private static final List<Command> registeredCommands = new ArrayList<>();
|
||||
private static List<Command> registeredCommands = new ArrayList<>();
|
||||
|
||||
private final static String defaultInviteLink =
|
||||
"https://discord.com/api/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8";
|
||||
@ -184,7 +184,7 @@ public class Configuration
|
||||
.sorted(Comparator.comparing(Command::getName))
|
||||
.toList();
|
||||
|
||||
registeredCommands.addAll(tempList);
|
||||
registeredCommands = new ArrayList<>(tempList);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,6 +11,7 @@ import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener;
|
||||
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
||||
import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter;
|
||||
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
||||
import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask;
|
||||
import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
import wtf.beatrice.hidekobot.utils.SlashCommandUtil;
|
||||
@ -90,7 +91,7 @@ public class HidekoBot
|
||||
|
||||
// update slash commands (delayed)
|
||||
final boolean finalForceUpdateCommands = forceUpdateCommands;
|
||||
Executors.newSingleThreadScheduledExecutor().schedule(() ->
|
||||
Executors.newSingleThreadScheduledExecutor().schedule(() -> // todo: try-with-resources
|
||||
SlashCommandUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS);
|
||||
|
||||
// set the bot's status
|
||||
@ -114,11 +115,11 @@ public class HidekoBot
|
||||
}
|
||||
|
||||
// start scheduled runnables
|
||||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
ExpiredMessageTask task = new ExpiredMessageTask();
|
||||
int initDelay = 5;
|
||||
int periodicDelay = 5;
|
||||
scheduler.scheduleAtFixedRate(task, initDelay, periodicDelay, TimeUnit.SECONDS);
|
||||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // todo: try-with-resources
|
||||
ExpiredMessageTask expiredMessageTask = new ExpiredMessageTask();
|
||||
scheduler.scheduleAtFixedRate(expiredMessageTask, 5, 5, TimeUnit.SECONDS); //every 5 seconds
|
||||
CommandsUpdateTask commandsUpdateTask = new CommandsUpdateTask();
|
||||
scheduler.scheduleAtFixedRate(commandsUpdateTask, 10, 300, TimeUnit.SECONDS); //every 5 minutes
|
||||
|
||||
// register shutdown interrupt signal listener for proper shutdown.
|
||||
Signal.handle(new Signal("INT"), signal -> shutdown());
|
||||
|
@ -0,0 +1,25 @@
|
||||
package wtf.beatrice.hidekobot.runnables;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import wtf.beatrice.hidekobot.Configuration;
|
||||
import wtf.beatrice.hidekobot.HidekoBot;
|
||||
import wtf.beatrice.hidekobot.utils.Logger;
|
||||
|
||||
public class CommandsUpdateTask implements Runnable {
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
public CommandsUpdateTask()
|
||||
{
|
||||
logger = new Logger(getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if(Configuration.isVerbose()) logger.log("Refreshing commands cache...");
|
||||
JDA instance = HidekoBot.getAPI();
|
||||
if(instance == null) return;
|
||||
Configuration.setRegisteredCommands(instance.retrieveCommands().complete());
|
||||
if(Configuration.isVerbose()) logger.log("Commands cache refreshed!");
|
||||
}
|
||||
}
|
@ -125,6 +125,9 @@ public class SlashCommandUtil
|
||||
}
|
||||
|
||||
// cache the registered commands.
|
||||
// note that if this is the first time the bot runs after updating commands,
|
||||
// this will probably still return the previous configuration because the discord api
|
||||
// needs to propagate. this is why we also set up a command updater task (ExpiredMessageTask).
|
||||
Configuration.setRegisteredCommands(jdaInstance.retrieveCommands().complete());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user