Skip to content

Commit 8c2a58f

Browse files
authored
Merge pull request #472 from danthe1st/scheduled-tasks
add command for running scheduled tasks
2 parents 9f51321 + c40b82f commit 8c2a58f

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package net.discordjug.javabot.systems.staff_commands;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import org.springframework.scheduling.config.ScheduledTaskHolder;
8+
9+
import net.discordjug.javabot.data.config.BotConfig;
10+
import net.discordjug.javabot.util.Responses;
11+
import net.dv8tion.jda.api.Permission;
12+
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
13+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
14+
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
15+
import net.dv8tion.jda.api.interactions.commands.Command;
16+
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
17+
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
18+
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
19+
import net.dv8tion.jda.api.interactions.commands.OptionType;
20+
import net.dv8tion.jda.api.interactions.commands.build.Commands;
21+
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
22+
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
23+
import xyz.dynxsty.dih4jda.util.AutoCompleteUtils;
24+
25+
/**
26+
* This command allows manually executing scheduled tasks.
27+
*/
28+
public class RunScheduledTaskCommand extends SlashCommand implements AutoCompletable{
29+
30+
private ScheduledTaskHolder taskHolder;
31+
32+
/**
33+
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
34+
* @param botConfig the configuration of the bot
35+
* @param taskHolder A Spring object managing scheduled tasks
36+
*/
37+
public RunScheduledTaskCommand(BotConfig botConfig, ScheduledTaskHolder taskHolder) {
38+
setCommandData(Commands.slash("run-task", "(ADMIN ONLY) Run scheduled tasks")
39+
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
40+
.setGuildOnly(true)
41+
.addOption(OptionType.STRING, "name", "Class name of the task", true, true)
42+
);
43+
setRequiredUsers(botConfig.getSystems().getAdminConfig().getAdminUsers());
44+
45+
this.taskHolder = taskHolder;
46+
47+
48+
}
49+
50+
@Override
51+
public void execute(SlashCommandInteractionEvent event) {
52+
String name = event.getOption("name", "", OptionMapping::getAsString);
53+
if (name.isEmpty()) {
54+
Responses.replyMissingArguments(event).queue();
55+
return;
56+
}
57+
taskHolder
58+
.getScheduledTasks()
59+
.stream()
60+
.filter(r -> name.equals(r.toString()))
61+
.findAny()
62+
.ifPresentOrElse(r -> {
63+
event.deferReply(true).queue();
64+
try {
65+
r.getTask().getRunnable().run();
66+
Responses.success(event.getHook(), "Task successful", "Task was executed successfully").queue();
67+
//CHECKSTYLE:OFF This is a handler for all sort of failures that could possibly happen
68+
}catch (RuntimeException e) {
69+
//CHECKSTYLE:ON
70+
Responses.error(event, "Task failed with an exception", e.getClass().getName() + (e.getMessage() == null ? "" : ": "+e.getMessage()));
71+
}
72+
}, () -> {
73+
Responses.error(event, "Cannot find task `%s`", name).queue();
74+
});
75+
}
76+
77+
@Override
78+
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
79+
List<Choice> choices = taskHolder
80+
.getScheduledTasks()
81+
.stream()
82+
.map(r -> new Command.Choice(r.toString(), r.toString()))
83+
.collect(Collectors.toCollection(ArrayList::new));
84+
event.replyChoices(AutoCompleteUtils.filterChoices(event, choices)).queue();
85+
}
86+
}

0 commit comments

Comments
 (0)