use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.
the class ApplicationCommandsUpdater method computeSlashCommands.
private void computeSlashCommands(List<ApplicationCommandInfo> guildApplicationCommands) {
guildApplicationCommands.stream().filter(a -> a instanceof SlashCommandInfo).map(a -> (SlashCommandInfo) a).distinct().forEachOrdered(info -> {
final CommandPath notLocalizedPath = info.getPath();
try {
final LocalizedCommandData localizedCommandData = LocalizedCommandData.of(context, guild, info);
// Put localized option names in order to resolve them when called
final List<OptionData> localizedMethodOptions = getLocalizedMethodOptions(info, localizedCommandData);
if (guild != null) {
info.putLocalizedOptions(guild.getIdLong(), localizedMethodOptions.stream().map(OptionData::getName).collect(Collectors.toList()));
}
localizedBaseNameToBaseName.put(localizedCommandData.getLocalizedPath().getName(), notLocalizedPath.getName());
final CommandPath localizedPath = localizedCommandData.getLocalizedPath();
final String description = localizedCommandData.getLocalizedDescription();
Checks.check(localizedPath.getNameCount() == notLocalizedPath.getNameCount(), "Localized path does not have the same name count as the not-localized path");
final boolean isDefaultEnabled = isDefaultEnabled(info);
if (localizedPath.getNameCount() == 1) {
// Standard command
final SlashCommandData rightCommand = Commands.slash(localizedPath.getName(), description);
map.put(Command.Type.SLASH, localizedPath, rightCommand);
rightCommand.addOptions(localizedMethodOptions);
rightCommand.setDefaultEnabled(isDefaultEnabled);
} else if (localizedPath.getNameCount() == 2) {
Checks.notNull(localizedPath.getSubname(), "Subcommand name");
final SlashCommandData commandData = (SlashCommandData) map.computeIfAbsent(Command.Type.SLASH, localizedPath, x -> {
final SlashCommandData tmpData = Commands.slash(localizedPath.getName(), "No description (base name)");
tmpData.setDefaultEnabled(isDefaultEnabled);
return tmpData;
});
// Subcommand of a command
final SubcommandData subcommandData = new SubcommandData(localizedPath.getSubname(), description);
subcommandData.addOptions(localizedMethodOptions);
commandData.addSubcommands(subcommandData);
} else if (localizedPath.getNameCount() == 3) {
Checks.notNull(localizedPath.getGroup(), "Command group name");
Checks.notNull(localizedPath.getSubname(), "Subcommand name");
final SubcommandGroupData groupData = getSubcommandGroup(Command.Type.SLASH, localizedPath, x -> {
final SlashCommandData commandData = Commands.slash(localizedPath.getName(), "No description (base name)");
commandData.setDefaultEnabled(isDefaultEnabled);
return commandData;
});
final SubcommandData subcommandData = new SubcommandData(localizedPath.getSubname(), description);
subcommandData.addOptions(localizedMethodOptions);
groupData.addSubcommands(subcommandData);
} else {
throw new IllegalStateException("A slash command with more than 4 path components got registered");
}
context.addApplicationCommandAlternative(localizedPath, Command.Type.SLASH, info);
if (!info.isOwnerRequired()) {
if (ownerOnlyCommands.contains(notLocalizedPath.getName())) {
LOGGER.warn("Non owner-only command '{}' is registered as a owner-only command because of another command with the same base name '{}'", notLocalizedPath, notLocalizedPath.getName());
}
}
if (info.isOwnerRequired()) {
if (info.isGuildOnly()) {
ownerOnlyCommands.add(notLocalizedPath.getName());
} else {
LOGGER.warn("Owner-only command '{}' cannot be owner-only as it is a global command", notLocalizedPath);
}
}
} catch (Exception e) {
throw new RuntimeException("An exception occurred while processing command '" + notLocalizedPath + "' at " + Utils.formatMethodShort(info.getMethod()), e);
}
});
}
use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.
the class PaginatorCommand method run.
@JDASlashCommand(name = "paginator")
public void run(GuildSlashEvent event) {
final List<EmbedBuilder> embedBuilders = new ArrayList<>();
// Let's suppose you generated embeds like in JDA-U, so you'd have a collection of embeds to present
for (int i = 0; i < 5; i++) {
embedBuilders.add(new EmbedBuilder().setTitle("Page #" + (i + 1)));
}
final Paginator paginator = new PaginatorBuilder().setConstraints(InteractionConstraints.ofUsers(event.getUser())).useDeleteButton(false).setMaxPages(5).setPaginatorSupplier((instance, messageBuilder, components, page) -> embedBuilders.get(page).build()).build();
// You must send the paginator as a message
event.reply(paginator.get()).setEphemeral(true).queue();
}
use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.
the class ComponentsBotMain method main.
public static void main(String[] args) {
try {
final CommonMain.CommonStuff commonStuff = CommonMain.start();
final JDA jda = commonStuff.getJDA();
// Instantiate the database needed for components
final ComponentsDB componentsDB = new ComponentsDB(commonStuff.getConfig());
// Build the command framework:
// Prefix: !
// Owner: User with the ID 222046562543468545
// Commands package: com.freya02.bot.componentsbot.commands
CommandsBuilder.newBuilder(222046562543468545L).textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix("!")).setComponentManager(new DefaultComponentManager(componentsDB::getConnection)).build(jda, // Registering listeners is taken care of by the lib
"com.freya02.bot.componentsbot.commands");
} catch (Exception e) {
LOGGER.error("Unable to start the bot", e);
System.exit(-1);
}
}
use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.
the class Main method main.
public static void main(String[] args) {
try {
// Make sure that the file Config.json exists under src/main/java/resources/com/freya02/bot/Config.json and has a valid token inside
final Config config = Config.readConfig();
// Set up JDA
final JDA jda = JDABuilder.createLight(config.getToken()).setActivity(Activity.playing("Prefix is " + config.getPrefix())).build().awaitReady();
// Print some information about the bot
LOGGER.info("Bot connected as {}", jda.getSelfUser().getAsTag());
LOGGER.info("The bot is present on these guilds :");
for (Guild guild : jda.getGuildCache()) {
LOGGER.info("\t- {} ({})", guild.getName(), guild.getId());
}
// Instantiate the database needed for components
final ComponentsDB componentsDB = new ComponentsDB(config);
// Build the command framework:
// Prefix: configured in Config.json
// Owner: configured in Config.json
// Commands package: com.freya02.bot.commands
CommandsBuilder.newBuilder(config.getOwnerId()).textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(config.getPrefix())).setComponentManager(new DefaultComponentManager(componentsDB::getConnection)).build(jda, // Registering listeners is taken care of by the lib
"com.freya02.bot.commands");
} catch (IOException | InterruptedException | LoginException | SQLException e) {
LOGGER.error("Unable to start the bot", e);
System.exit(-1);
}
}
use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.
the class ApplicationCommandsUpdater method getSubcommandGroup.
// I am aware that the type is always Command.Type#SLASH, still use a parameter to mimic how ApplicationCommandMap functions and for future proof uses
@SuppressWarnings("SameParameterValue")
@NotNull
private SubcommandGroupData getSubcommandGroup(Command.Type type, CommandPath path, Function<String, CommandData> baseCommandSupplier) {
if (path.getGroup() == null)
throw new IllegalArgumentException("Group component of command path is null at '" + path + "'");
final SlashCommandData data = (SlashCommandData) map.computeIfAbsent(type, path, baseCommandSupplier);
final CommandPath parent = path.getParent();
if (parent == null)
throw new IllegalStateException("A command path with less than 3 components was passed to #getSubcommandGroup");
return subcommandGroupDataMap.computeIfAbsent(parent.getFullPath(), s -> {
final SubcommandGroupData groupData = new SubcommandGroupData(path.getGroup(), "No description (group)");
data.addSubcommandGroups(groupData);
return groupData;
});
}
Aggregations