use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData 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);
}
}
use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project TechDiscordBot by TechsCode-Team.
the class ModulesManager method load.
public void load() {
TechDiscordBot.getJDA().updateCommands().queue();
CommandListUpdateAction commands = TechDiscordBot.getGuild().updateCommands();
for (Class<?> each : ProjectUtil.getClasses("me.TechsCode.TechDiscordBot.module")) {
if (CommandModule.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
try {
CommandModule module = (CommandModule) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
if (module.getName() == null)
continue;
cmdModules.add(module);
CommandData cmdData = new CommandData(module.getName(), module.getDescription() == null ? "No description set." : module.getDescription()).addOptions(module.getOptions()).setDefaultEnabled(module.getCommandPrivileges().length == 0);
commands.addCommands(cmdData);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
} else if (Module.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
try {
Module module = (Module) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
module.enable();
modules.add(module);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}
}
commands.addCommands(new CommandData("ticket", "Manage tickets.").addSubcommands(new SubcommandData("add", "Add a member to a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to add.", true)), new SubcommandData("remove", "Remove a member from a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to remove.", true)), new SubcommandData("transcript", "Force make a ticket transcript."), new SubcommandData("close", "Close a ticket.").addOptions(new OptionData(OptionType.STRING, "reason", "Reason to close the ticket. (Optional)")))).queue(cmds -> {
cmds.forEach(command -> {
CommandPrivilege[] privilege = cmdModules.stream().filter(c -> c.getName().equals(command.getName())).map(CommandModule::getCommandPrivileges).findFirst().orElse(new CommandPrivilege[] {});
if (privilege.length > 0)
TechDiscordBot.getGuild().updateCommandPrivilegesById(command.getId(), Arrays.asList(privilege)).queue();
});
});
TechDiscordBot.getJDA().addEventListener(modules.toArray());
TechDiscordBot.getJDA().addEventListener(cmdModules.toArray());
Runtime.getRuntime().addShutdownHook(new Thread(() -> modules.forEach(Module::onDisable)));
}
Aggregations