use of net.dv8tion.jda.api.interactions.commands.Command in project JDA by DV8FromTheWorld.
the class CommandData method fromData.
/**
* Parses the provided serialization back into an CommandData instance.
* <br>This is the reverse function for {@link CommandData#toData()}.
*
* @param object
* The serialized {@link DataObject} representing the command
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the serialized object is missing required fields
* @throws IllegalArgumentException
* If any of the values are failing the respective checks such as length
*
* @return The parsed CommandData instance, which can be further configured through setters
*
* @see SlashCommandData#fromData(DataObject)
* @see Commands#fromList(Collection)
*/
@Nonnull
static CommandData fromData(@Nonnull DataObject object) {
Checks.notNull(object, "DataObject");
String name = object.getString("name");
Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
if (commandType != Command.Type.SLASH)
return new CommandDataImpl(commandType, name);
return SlashCommandData.fromData(object);
}
use of net.dv8tion.jda.api.interactions.commands.Command in project JDA by DV8FromTheWorld.
the class SlashCommandData method fromData.
/**
* Parses the provided serialization back into a SlashCommandData instance.
* <br>This is the reverse function for {@link SlashCommandData#toData()}.
*
* @param object
* The serialized {@link DataObject} representing the command
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the serialized object is missing required fields
* @throws IllegalArgumentException
* If any of the values are failing the respective checks such as length
*
* @return The parsed SlashCommandData instance, which can be further configured through setters
*
* @see CommandData#fromData(DataObject)
* @see Commands#fromList(Collection)
*/
@Nonnull
static SlashCommandData fromData(@Nonnull DataObject object) {
Checks.notNull(object, "DataObject");
String name = object.getString("name");
Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
if (commandType != Command.Type.SLASH)
throw new IllegalArgumentException("Cannot convert command of type " + commandType + " to SlashCommandData!");
String description = object.getString("description");
DataArray options = object.optArray("options").orElseGet(DataArray::empty);
CommandDataImpl command = new CommandDataImpl(name, description);
options.stream(DataArray::getObject).forEach(opt -> {
OptionType type = OptionType.fromKey(opt.getInt("type"));
switch(type) {
case SUB_COMMAND:
command.addSubcommands(SubcommandData.fromData(opt));
break;
case SUB_COMMAND_GROUP:
command.addSubcommandGroups(SubcommandGroupData.fromData(opt));
break;
default:
command.addOptions(OptionData.fromData(opt));
}
});
return command;
}
use of net.dv8tion.jda.api.interactions.commands.Command in project discord-bot-reddit-java by Glaxier0.
the class RetrievePermissionCommand method deleteCommandsFromGuild.
private void deleteCommandsFromGuild(Guild guild) throws IllegalStateException {
RestAction<List<Command>> guildCommands = guild.retrieveCommands();
List<Command> commandList = guildCommands.complete();
for (Command command : commandList) {
command.delete();
}
guild.updateCommands().queue();
}
use of net.dv8tion.jda.api.interactions.commands.Command in project BotCommands by freya022.
the class ApplicationCommandsUpdater method thenAcceptGuild.
private void thenAcceptGuild(List<Command> commands, @NotNull Guild guild) {
for (Command command : commands) {
context.getRegistrationListeners().forEach(l -> l.onGuildSlashCommandRegistered(this.guild, command));
}
this.commands.clear();
this.commands.addAll(commands);
try {
Files.write(commandsCachePath, ApplicationCommandsCache.getCommandsBytes(allCommandData), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
LOGGER.error("An exception occurred while temporarily saving guild ({} ({})) commands in '{}'", guild.getName(), guild.getId(), commandsCachePath.toAbsolutePath(), e);
}
if (!LOGGER.isTraceEnabled())
return;
final StringBuilder sb = new StringBuilder("Updated " + commands.size() + " / " + allCommandData.size() + " (" + context.getApplicationCommandsView().size() + ") commands for ");
sb.append(guild.getName()).append(" :\n");
appendCommands(commands, sb);
LOGGER.trace(sb.toString().trim());
}
use of net.dv8tion.jda.api.interactions.commands.Command in project BotCommands by freya022.
the class ApplicationCommandsUpdater method thenAcceptGlobal.
private void thenAcceptGlobal(List<Command> commands) {
for (Command command : commands) {
context.getRegistrationListeners().forEach(l -> l.onGlobalSlashCommandRegistered(command));
}
try {
Files.write(commandsCachePath, ApplicationCommandsCache.getCommandsBytes(allCommandData), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
LOGGER.error("An exception occurred while temporarily saving {} commands in '{}'", guild == null ? "global" : String.format("guild '%s' (%s)", guild.getName(), guild.getId()), commandsCachePath.toAbsolutePath(), e);
}
if (!LOGGER.isTraceEnabled())
return;
final StringBuilder sb = new StringBuilder("Updated global commands:\n");
appendCommands(commands, sb);
LOGGER.trace(sb.toString().trim());
}
Aggregations