Skip to content

Commit

Permalink
chore: Add slash command support to plugin commands. And fix help des…
Browse files Browse the repository at this point in the history
…criptions.
  • Loading branch information
DxsSucuk committed Jan 24, 2025
1 parent 9aab005 commit 81e2f22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/main/java/de/presti/ree6/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
Expand Down Expand Up @@ -185,6 +186,17 @@ private String convertOptionsToString(List<OptionData> list) {
return stringBuilder.toString();
}

/**
* Method used to add all Commands as SlashCommand on Discord.
*
* @param shardManager the Shard manager.
*/
public void addSlashCommand(ShardManager shardManager) {
for (JDA jda : shardManager.getShards()){
addSlashCommand(jda);
}
}

/**
* Method used to add all Commands as SlashCommand on Discord.
*
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/de/presti/ree6/commands/impl/info/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.SQLSession;
import de.presti.ree6.utils.data.RegExUtil;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
Expand Down Expand Up @@ -97,11 +98,13 @@ public void sendHelpInformation(String categoryString, CommandEvent commandEvent

SQLSession.getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getIdLong(), "chatprefix").subscribe(setting -> {
for (ICommand cmd : Main.getInstance().getCommandManager().getCommands().stream().filter(command -> command.getClass().getAnnotation(Command.class).category() == category).toList()) {
Command commandAnnotation = cmd.getClass().getAnnotation(Command.class);
boolean isValidDescription = commandAnnotation.description().matches(RegExUtil.ALLOWED_LANGUAGE_PATHS);
end.append("``")
.append(setting.get().getStringValue())
.append(cmd.getClass().getAnnotation(Command.class).name())
.append(commandAnnotation.name())
.append("``\n")
.append(commandEvent.getResource(cmd.getClass().getAnnotation(Command.class).description()))
.append(isValidDescription ? commandEvent.getResource(commandAnnotation.description()) : commandAnnotation.description())
.append("\n\n");
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,13 @@ public static void main(String[] args) {
log.info("Starting all Plugins.");
getInstance().getPluginManager().startPlugins();

boolean shouldUpdateCommands = false;
log.info("Registering all Commands of plugins.");
for (PluginWrapper plugin : getInstance().getPluginManager().getPlugins()) {
if (plugin.getPluginState() != PluginState.STARTED) continue;
List<ICommand> commands = getInstance().getPluginManager().getExtensions(ICommand.class, plugin.getPluginId());
log.info("Found {} commands in {}.", commands.size(), plugin.getPluginId());
if (!shouldUpdateCommands && !commands.isEmpty()) shouldUpdateCommands = true;
commands.forEach(command -> {
try {
getInstance().getCommandManager().addCommand(command);
Expand All @@ -394,6 +396,11 @@ public static void main(String[] args) {
}
});
}

if (shouldUpdateCommands) {
getInstance().getCommandManager().addSlashCommand(BotWorker.getShardManager());
log.info("Readding SlashCommands because of Plugin commands.");
}
}

// Create checker Thread.
Expand Down

0 comments on commit 81e2f22

Please sign in to comment.