use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project triumph-cmds by TriumphTeam.
the class SlashCommand method asCommandData.
@NotNull
public SlashCommandData asCommandData() {
final SlashCommandData commandData = Commands.slash(name, description);
commandData.setDefaultEnabled(enabledRoles.isEmpty());
if (isDefault) {
final SlashSubCommand<S> subCommand = getDefaultSubCommand();
// Should never be null.
if (subCommand == null)
throw new CommandRegistrationException("Could not find default subcommand");
commandData.addOptions(subCommand.getJdaOptions());
return commandData;
}
final List<SubcommandData> subData = subCommands.entrySet().stream().map(entry -> new SubcommandData(entry.getKey(), entry.getValue().getDescription()).addOptions(entry.getValue().getJdaOptions())).collect(Collectors.toList());
commandData.addSubcommands(subData);
return commandData;
}
use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method getBaseCommandData.
/**
* Gets the complete {@link SlashCommandData} (including Subcommands & Subcommand Groups) of a single {@link BaseSlashCommand}.
*
* @param command The base command's instance.
* @param commandClass The base command's class.
* @param guild The current guild (if available)
* @return The new {@link CommandListUpdateAction}.
* @throws Exception If an error occurs.
*/
private SlashCommandData getBaseCommandData(@NotNull BaseSlashCommand command, Class<? extends BaseSlashCommand> commandClass, @Nullable Guild guild) throws Exception {
if (command.getCommandData() == null) {
DIH4JDALogger.warn(String.format("Class %s is missing CommandData. It will be ignored.", commandClass.getName()));
return null;
}
SlashCommandData commandData = command.getCommandData();
if (command.getSubcommandGroups() != null) {
commandData.addSubcommandGroups(this.getSubcommandGroupData(command, guild));
}
if (command.getSubcommands() != null) {
commandData.addSubcommands(this.getSubcommandData(command, command.getSubcommands(), null, guild));
}
if (command.getSubcommandGroups() == null && command.getSubcommands() == null) {
slashCommandIndex.put(buildCommandPath(commandData.getName()), new SlashCommandInteraction((ISlashCommand) command, commandClass, command.getCommandPrivileges()));
DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", command.getCommandData().getName()), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
}
return commandData;
}
use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project Bean by Xirado.
the class CommandsRoute method handle.
@Override
public Object handle(Request request, Response response) throws Exception {
List<SlashCommand> commands = Bean.getInstance().isDebug() ? Bean.getInstance().getInteractionCommandHandler().getRegisteredGuildCommands().get(DEV_GUILD_ID).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList() : Bean.getInstance().getInteractionCommandHandler().getRegisteredSlashCommands();
DataArray commandArray = DataArray.empty();
for (SlashCommand command : commands) {
DataObject commandObject = DataObject.empty();
SlashCommandData slashCommandData = command.getData();
DataArray options = DataArray.empty();
DataArray subCommands = DataArray.empty();
for (OptionData option : slashCommandData.getOptions()) {
DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
options.add(optionObject);
}
for (SubcommandData subcommandData : slashCommandData.getSubcommands()) {
DataObject subCommandObject = DataObject.empty();
DataArray subCommandOptions = DataArray.empty();
for (OptionData option : subcommandData.getOptions()) {
DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
subCommandOptions.add(optionObject);
}
subCommandObject.put("name", subcommandData.getName()).put("description", subcommandData.getDescription());
subCommandObject.put("options", subCommandOptions);
subCommands.add(subCommandObject);
}
commandObject.put("name", slashCommandData.getName()).put("description", slashCommandData.getDescription()).put("options", options).put("sub_commands", subCommands);
commandArray.add(commandObject);
}
return commandArray.toString();
}
use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project Discord-Core-Bot-Apple by demodude4u.
the class DiscordBot method buildUpdateCommands.
// Hold my beer
@SuppressWarnings("unchecked")
private void buildUpdateCommands(CommandListUpdateAction updateCommands) {
Map<String, Object> root = new LinkedHashMap<>();
for (SlashCommandDefinition command : commandSlash.values()) {
String[] pathSplit = command.getPath().split("/");
Map<String, Object> group = root;
for (int i = 0; i < pathSplit.length; i++) {
String name = pathSplit[i];
if (i == pathSplit.length - 1) {
group.put(name, command);
} else {
Object subGroup = group.get(name);
if (subGroup == null) {
group.put(name, subGroup = new LinkedHashMap<String, Object>());
}
group = (Map<String, Object>) subGroup;
}
}
}
for (Entry<String, Object> rootEntry : root.entrySet()) {
SlashCommandData commandData;
if (rootEntry.getValue() instanceof SlashCommandDefinition) {
SlashCommandDefinition commandDefinition = (SlashCommandDefinition) rootEntry.getValue();
commandData = Commands.slash(rootEntry.getKey(), commandDefinition.getDescription());
for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
commandData = commandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
}
} else {
Map<String, Object> sub = (Map<String, Object>) rootEntry.getValue();
commandData = Commands.slash(rootEntry.getKey(), sub.keySet().stream().collect(Collectors.joining(", ")));
for (Entry<String, Object> subEntry : sub.entrySet()) {
if (subEntry.getValue() instanceof SlashCommandDefinition) {
SlashCommandDefinition commandDefinition = (SlashCommandDefinition) subEntry.getValue();
SubcommandData subcommandData = new SubcommandData(subEntry.getKey(), commandDefinition.getDescription());
for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
}
commandData = commandData.addSubcommands(subcommandData);
} else {
Map<String, SlashCommandDefinition> subSub = (Map<String, SlashCommandDefinition>) subEntry.getValue();
SubcommandGroupData subcommandGroupData = new SubcommandGroupData(subEntry.getKey(), subSub.keySet().stream().collect(Collectors.joining(", ")));
for (Entry<String, SlashCommandDefinition> subSubEntry : subSub.entrySet()) {
SubcommandData subcommandData = new SubcommandData(subSubEntry.getKey(), subSubEntry.getValue().getDescription());
for (SlashCommandOptionDefinition option : subSubEntry.getValue().getOptions()) {
subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
}
subcommandGroupData = subcommandGroupData.addSubcommands(subcommandData);
}
commandData = commandData.addSubcommandGroups(subcommandGroupData);
}
}
}
updateCommands = updateCommands.addCommands(commandData);
}
for (MessageCommandDefinition commandDefinition : commandMessage.values()) {
CommandData commandData = Commands.message(commandDefinition.getName());
updateCommands = updateCommands.addCommands(commandData);
}
}
Aggregations