use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project cjda-util by RainbowDashLabs.
the class CommandHub method updateCommands.
private void updateCommands() {
log.info("Updating slash commands.");
for (var language : localizer.languages()) {
log.info("Creating command data for {} language", language.getCode());
List<CommandData> localizedCommandData = new ArrayList<>();
for (var command : new HashSet<>(commands.values())) {
localizedCommandData.add(command.getCommandData(localizer, language));
}
commandData.put(language, localizedCommandData);
}
for (var command : new HashSet<>(commands.values())) {
if (command.subCommands() != null) {
log.info("Registering command {} with {} subcommands", command.command(), command.subCommands().length);
} else {
log.info("Registering command {}.", command.command());
}
}
for (JDA shard : shardManager.getShards()) {
try {
shard.awaitReady();
log.info("Shard {} is ready.", shard.getShardInfo().getShardId());
} catch (InterruptedException e) {
// ignore
}
}
if (useSlashGlobalCommands) {
var baseShard = shardManager.getShards().get(0);
log.info("Removing guild commands");
for (JDA shard : shardManager.getShards()) {
for (Guild guild : shard.getGuilds()) {
guild.updateCommands().complete();
}
}
log.info("Updating global slash commands.");
baseShard.updateCommands().addCommands(commandData.get(localizer.defaultLanguage())).complete();
return;
}
var baseShard = shardManager.getShards().get(0);
log.info("Removing global slash commands and using guild commands.");
baseShard.updateCommands().complete();
for (var shard : shardManager.getShards()) {
for (var guild : shard.getGuilds()) {
refreshGuildCommands(guild);
}
}
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method registerInteractions.
public void registerInteractions(JDA jda) throws Exception {
this.registerSlashCommands();
this.registerContextCommands();
for (Guild guild : jda.getGuilds()) {
Set<CommandData> commands = new HashSet<>();
commands.addAll(this.getGuildSlashCommandData(guild));
commands.addAll(this.getGuildContextCommandData(guild));
guild.updateCommands().addCommands(commands).queue();
}
Set<CommandData> commands = new HashSet<>();
commands.addAll(this.getGlobalSlashCommandData());
commands.addAll(this.getGlobalContextCommandData());
jda.updateCommands().addCommands(commands).queue();
this.registerCommandPrivileges(jda);
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method getGlobalContextCommandData.
/**
* Gets all Global Context commands registered in {@link InteractionHandler#registerContextCommands()} and
* returns their {@link CommandData} as a List.
*
* @throws Exception If an error occurs.
*/
private Set<CommandData> getGlobalContextCommandData() throws Exception {
Set<CommandData> commands = new HashSet<>();
for (Class<? extends BaseContextCommand> contextCommandClass : this.globalContexts) {
BaseContextCommand instance = (BaseContextCommand) this.getClassInstance(null, contextCommandClass);
CommandData data = this.getContextCommandData(instance, contextCommandClass);
if (data != null) {
commands.add(data);
}
}
DIH4JDALogger.info("[*] Queuing Global Context Commands", DIH4JDALogger.Type.CONTEXT_COMMANDS_QUEUED);
return commands;
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Essentials by drtshock.
the class InteractionControllerImpl method registerCommand.
@Override
public void registerCommand(InteractionCommand command) throws InteractionException {
if (command.isDisabled()) {
throw new InteractionException("The given command has been disabled!");
}
if (commandMap.containsKey(command.getName())) {
throw new InteractionException("A command with that name is already registered!");
}
if (!initialBatchRegistration) {
if (jda.isDebug()) {
logger.info("Marked guild command for batch registration: " + command.getName());
}
batchRegistrationQueue.put(command.getName(), command);
return;
}
final CommandData data = new CommandData(command.getName(), command.getDescription());
if (command.getArguments() != null) {
for (final InteractionCommandArgument argument : command.getArguments()) {
data.addOption(OptionType.valueOf(argument.getType().name()), argument.getName(), argument.getDescription(), argument.isRequired());
}
}
jda.getGuild().upsertCommand(data).queue(success -> {
commandMap.put(command.getName(), command);
if (jda.isDebug()) {
logger.info("Registered guild command " + success.getName() + " with id " + success.getId());
}
}, failure -> {
if (failure instanceof ErrorResponseException && ((ErrorResponseException) failure).getErrorResponse() == ErrorResponse.MISSING_ACCESS) {
logger.severe(tl("discordErrorCommand"));
return;
}
logger.log(Level.SEVERE, "Error while registering command", failure);
});
}
use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project JDA by DV8FromTheWorld.
the class CommandListUpdateActionImpl method addCommands.
@Nonnull
@Override
public CommandListUpdateAction addCommands(@Nonnull Collection<? extends CommandData> commands) {
Checks.noneNull(commands, "Command");
int newSlash = 0, newUser = 0, newMessage = 0;
for (CommandData command : commands) {
switch(command.getType()) {
case SLASH:
newSlash++;
break;
case MESSAGE:
newMessage++;
break;
case USER:
newUser++;
break;
}
}
Checks.check(slash + newSlash <= Commands.MAX_SLASH_COMMANDS, "Cannot have more than %d slash commands! Try using subcommands instead.", Commands.MAX_SLASH_COMMANDS);
Checks.check(user + newUser <= Commands.MAX_USER_COMMANDS, "Cannot have more than %d user context commands!", Commands.MAX_USER_COMMANDS);
Checks.check(message + newMessage <= Commands.MAX_MESSAGE_COMMANDS, "Cannot have more than %d message context commands!", Commands.MAX_MESSAGE_COMMANDS);
Checks.checkUnique(Stream.concat(commands.stream(), this.commands.stream()).map(c -> c.getType() + " " + c.getName()), "Cannot have multiple commands of the same type with identical names. " + "Name: \"%s\" with type %s appeared %d times!", (count, value) -> {
String[] tuple = value.split(" ", 2);
return new Object[] { tuple[1], tuple[0], count };
});
slash += newSlash;
user += newUser;
message += newMessage;
this.commands.addAll(commands);
return this;
}
Aggregations