Skip to content

Added subcommands #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v5.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions core/src/main/java/me/koply/kcommando/KCommando.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class KCommando {

public final Integration integration;
private final KInitializer initializer;

public KCommando(Integration integration) {
this.integration = integration;
this.initializer = new KInitializer(this);
Expand All @@ -28,7 +29,8 @@ public KCommando(Integration integration, Class<? extends KInitializer> customIn
try {
Constructor<? extends KInitializer> constructor = customInitializer.getDeclaredConstructor(KCommando.class);
temp = constructor.newInstance(this);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException e) {
Kogger.warn("Initializer field cannot set to the custom initializer class. KCommando will use the default KInitializer.");
temp = new KInitializer(this);
}
Expand All @@ -51,7 +53,7 @@ public KCommando build() {
return this;
}

public void registerObject(Object...customInstances) {
public void registerObject(Object... customInstances) {
for (Object customInstance : customInstances) {
if (verbose) Kogger.info("Registering a custom instance named as " + customInstance.getClass().getName());
initializer.registerClass(customInstance);
Expand All @@ -71,7 +73,7 @@ public Set<Long> getOwnerIds() {
return ownerIds;
}

public KCommando setOwners(long...ids) {
public KCommando setOwners(long... ids) {
for (long id : ids) {
ownerIds.add(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@
public @interface HandleSlash {

String name();

String desc() default "-";

Option[] options() default @Option(type = OptionType.UNKNOWN, name = DefaultConstants.DEFAULT_TEXT);

boolean enabledInDms() default false;

boolean global() default false;

long[] guildId() default 0;

boolean subCommand() default false;

String parentGroup() default "";

}
Binary file added jda-integration/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

public class JDAIntegration extends Integration {

public final JDA api;
private final List<CommandDataImpl> registeredSubCommandGroups = new ArrayList<>();

public JDAIntegration(JDA api) {
super(api.getSelfUser().getIdLong());
this.api = api;
Expand All @@ -49,13 +56,19 @@ public void registerButtonClickHandler(ButtonClickHandler handler) {
api.addEventListener(new ButtonListener(handler));
}

public void addSubCommandGroup(CommandDataImpl group) {
registeredSubCommandGroups.add(group);
}

@Override
public void registerSlashCommand(SlashBox box) {
HandleSlash info = box.info;

String name = info.name();
String desc = info.desc();
boolean isglobal = info.global();
boolean subCommand = info.subCommand();
String parent = info.parentGroup();

Option[] options = info.options();
OptionData[] optionDatas = new OptionData[options.length];
Expand Down Expand Up @@ -88,24 +101,50 @@ public void registerSlashCommand(SlashBox box) {

boolean guildOnly = !info.enabledInDms();

CommandData data = new CommandDataImpl(name, desc)
.setGuildOnly(guildOnly)
.addOptions(rolledOptionDatas);
CommandData commandData;
SubcommandData subcommandData;

if (subCommand) {
Optional<CommandDataImpl> registeredGroup = registeredSubCommandGroups.stream()
.filter(registered -> registered.getName().equals(parent))
.findFirst();

if (registeredGroup.isPresent()) {
CommandDataImpl found = registeredGroup.get();

subcommandData = new SubcommandData(name, desc);
found.addSubcommands(subcommandData);

commandData = found;
} else {
throw new NoSuchElementException("No group with that name is registered. Please register that group first!");
}
} else {
commandData = new CommandDataImpl(name, desc)
.setGuildOnly(guildOnly)
.addOptions(rolledOptionDatas);
}

box.getPerm().ifPresent(perm ->
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Util.getPermissions(perm.value())))
commandData.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Util.getPermissions(perm.value())))
);

long[] guildIds = info.guildId();
if (guildIds[0] == 0) {
if (KCommando.verbose) Kogger.info("The SlashCommand that named as '" + name + "' is upserted as global command.");
api.upsertCommand(data).queue();
if (KCommando.verbose)
Kogger.info("The SlashCommand that named as '" + name + "' is upserted as global command.");
api.upsertCommand(commandData).queue();
} else for (long guildId : guildIds) {
Guild guild = api.getGuildById(guildId);
if (guild != null) {
guild.upsertCommand(data).queue();
} if (KCommando.verbose) {
Kogger.warn("Guild not found for Slash Command named as " + name);
guild.upsertCommand(commandData).queue();
if (KCommando.verbose) {
Kogger.info("The SlashCommand that named as '" + name + "' is upserted as a guild command for guild '" + guildId + "'");
}
} else {
if (KCommando.verbose) {
Kogger.warn("Guild not found for Slash Command named as " + name);
}
}
}
}
Expand Down