use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method registerCommandPrivileges.
/**
* Registers all Command Privileges.
*
* @param jda The {@link JDA} instance.
*/
private void registerCommandPrivileges(JDA jda) {
for (Guild guild : jda.getGuilds()) {
Map<String, Set<CommandPrivilege>> privileges = new HashMap<>();
guild.retrieveCommands().queue(commands -> {
for (Command command : commands) {
if (privileges.containsKey(command.getId()))
continue;
Optional<SlashCommandInteraction> interactionOptional = this.slashCommandIndex.keySet().stream().filter(p -> p.equals(command.getName()) || p.split("/")[0].equals(command.getName())).map(slashCommandIndex::get).filter(p -> p.getPrivileges() != null && p.getPrivileges().length > 0).findFirst();
if (interactionOptional.isPresent()) {
SlashCommandInteraction interaction = interactionOptional.get();
if (interaction.getBaseClass().getSuperclass().equals(GlobalSlashCommand.class)) {
DIH4JDALogger.error(String.format("Can not register command privileges for global command %s (%s).", command.getName(), interaction.getBaseClass().getSimpleName()));
continue;
}
privileges.put(command.getId(), new HashSet<>(Arrays.asList(interaction.getPrivileges())));
DIH4JDALogger.info(String.format("[%s] Registered privileges for command %s: %s", guild.getName(), command.getName(), Arrays.stream(interaction.getPrivileges()).map(CommandPrivilege::toData).collect(Collectors.toList())), DIH4JDALogger.Type.COMMAND_PRIVILEGE_REGISTERED);
}
if (privileges.isEmpty())
continue;
guild.updateCommandPrivileges(privileges).queue();
}
});
}
}
use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method getSubcommandData.
/**
* Gets all {@link SubcommandData} from the given array of {@link Subcommand} classes.
*
* @param command The base command's instance.
* @param subClasses All sub command classes.
* @param subGroupName The Subcommand Group's name. (if available)
* @param guild The current guild (if available)
* @return The new {@link CommandListUpdateAction}.
* @throws Exception If an error occurs.
*/
private Set<SubcommandData> getSubcommandData(BaseSlashCommand command, Class<? extends Subcommand>[] subClasses, @Nullable String subGroupName, @Nullable Guild guild) throws Exception {
Set<SubcommandData> subDataList = new HashSet<>();
for (Class<? extends Subcommand> sub : subClasses) {
Subcommand instance = (Subcommand) this.getClassInstance(guild, sub);
if (instance.getSubcommandData() == null) {
DIH4JDALogger.warn(String.format("Class %s is missing SubcommandData. It will be ignored.", sub.getName()));
continue;
}
String commandPath;
if (subGroupName == null) {
commandPath = buildCommandPath(command.getCommandData().getName(), instance.getSubcommandData().getName());
} else {
commandPath = buildCommandPath(command.getCommandData().getName(), subGroupName, instance.getSubcommandData().getName());
}
slashCommandIndex.put(commandPath, new SlashCommandInteraction((ISlashCommand) instance, sub, command.getCommandPrivileges()));
DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", commandPath), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
subDataList.add(instance.getSubcommandData());
}
return subDataList;
}
use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction 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;
}
Aggregations