Search in sources :

Example 1 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class HelpCommand method showCommandHelp.

private void showCommandHelp() {
    getManager().getCommand(getContext(), getCommandInput()).ifPresentOrElse(command -> {
        String prefix;
        GuildSpecification specification = getContext().getGuildContext().getSpecification();
        String setPrefix = specification.getPrefix();
        String botName = specification.getBotName();
        if (!Strings.isNullOrEmpty(setPrefix)) {
            prefix = setPrefix;
        } else if (!Strings.isNullOrEmpty(botName)) {
            prefix = botName + " ";
        } else {
            prefix = PrefixProperty.DEFAULT_FALLBACK + " ";
        }
        char argumentPrefix = ArgumentPrefixProperty.getForCurrentContext().getArgumentPrefix();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle("Command " + command.getIdentifier() + ":");
        String descriptionFormat = command.getDescription();
        String descriptionText = String.format(descriptionFormat, prefix, argumentPrefix);
        embedBuilder.setDescription(descriptionText);
        Guild guild = getContext().getGuild();
        Optional<AccessConfiguration> accessConfiguration = Aiode.get().getSecurityManager().getAccessConfiguration(command.getPermissionTarget(), guild);
        if (accessConfiguration.isPresent()) {
            String title = "Available to roles: ";
            String text;
            List<Role> roles = accessConfiguration.get().getRoles(guild);
            if (!roles.isEmpty()) {
                text = StringList.create(roles, Role::getName).toSeparatedString(", ");
            } else {
                text = "Guild owner and administrator roles only";
            }
            embedBuilder.addField(title, text, false);
        }
        ArgumentController argumentController = command.getArgumentController();
        if (argumentController.hasArguments()) {
            embedBuilder.addField("__Arguments__", "Keywords that alter the command behavior or define a search scope.", false);
            argumentController.getArguments().values().stream().sorted(Comparator.comparing(CommandArgument::getIdentifier)).forEach(argument -> embedBuilder.addField(argumentPrefix + argument.getIdentifier(), String.format(argument.getDescription(), prefix, argumentPrefix), false));
        }
        List<XmlElement> examples = command.getCommandContribution().query(tagName("example")).collect();
        if (!examples.isEmpty()) {
            embedBuilder.addField("__Examples__", "Practical usage examples for this command.", false);
            for (XmlElement example : examples) {
                String exampleText = String.format(example.getTextContent(), prefix, argumentPrefix);
                String titleText = String.format(example.getAttribute("title").getValue(), prefix, argumentPrefix);
                embedBuilder.addField(titleText, exampleText, false);
            }
        }
        sendMessage(embedBuilder);
    }, () -> {
        throw new InvalidCommandException(String.format("No command found for '%s'", getCommandInput()));
    });
}
Also used : CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) Guild(net.dv8tion.jda.api.entities.Guild) Role(net.dv8tion.jda.api.entities.Role) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) XmlElement(net.robinfriedli.jxp.api.XmlElement) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 2 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PermissionCommand method getSelectedTargets.

private Set<? extends PermissionTarget> getSelectedTargets() {
    Set<? extends PermissionTarget> selectedRootTargets = getSelectedRootTargets();
    if (!argumentSet("argument")) {
        return selectedRootTargets;
    }
    String argumentValue;
    if (argumentSet("all")) {
        argumentValue = getArgumentValueOrElse("argument", getCommandInput());
    } else {
        argumentValue = getArgumentValueWithTypeOrElse("argument", String.class, null);
    }
    if (argumentValue == null) {
        return getAllArguments(selectedRootTargets);
    }
    List<String> argumentIdentifiers = COMMA_SPLITTER.splitToList(argumentValue);
    if (argumentIdentifiers.isEmpty()) {
        return getAllArguments(selectedRootTargets);
    }
    Set<CommandArgument> selectedArguments = Sets.newHashSet();
    for (String argumentIdentifier : argumentIdentifiers) {
        for (PermissionTarget selectedRootTarget : selectedRootTargets) {
            if (selectedRootTarget instanceof CommandContribution) {
                CommandContribution commandContribution = (CommandContribution) selectedRootTarget;
                CommandArgument argument = commandContribution.getArgument(argumentIdentifier);
                if (argument != null) {
                    selectedArguments.add(argument);
                } else {
                    throw new InvalidCommandException(String.format("No such argument '%s' on command '%s'.", argumentIdentifier, commandContribution.getIdentifier()));
                }
            } else {
                throw new InvalidCommandException(String.format("Cannot find argument '%s' on permission target '%s' as it is not a command.", argumentIdentifier, selectedRootTarget.getFullPermissionTargetIdentifier()));
            }
        }
    }
    return selectedArguments;
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 3 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PermissionCommand method deleteCustomPermissionTarget.

private void deleteCustomPermissionTarget() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    String identifier = getCommandInput();
    CommandContext context = getContext();
    Session session = context.getSession();
    Optional<? extends PermissionTarget> existingPermissionTarget = securityManager.getPermissionTarget(identifier);
    if (existingPermissionTarget.isEmpty()) {
        throw new InvalidCommandException(String.format("No such permission target '%s'.", identifier));
    }
    PermissionTarget permissionTarget = existingPermissionTarget.get();
    if (!(permissionTarget instanceof CustomPermissionTarget)) {
        throw new InvalidCommandException(String.format("Permission target '%s' cannot be deleted as it is not a custom target.", identifier));
    }
    CustomPermissionTarget customPermissionTarget = (CustomPermissionTarget) permissionTarget;
    invoke(() -> {
        Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(customPermissionTarget, context.getGuild());
        if (accessConfiguration.isPresent()) {
            for (GrantedRole role : accessConfiguration.get().getRoles()) {
                session.delete(role);
            }
            session.delete(accessConfiguration);
        }
        session.delete(customPermissionTarget);
    });
}
Also used : CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) Session(org.hibernate.Session) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 4 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PermissionCommand method getSelectedRoles.

private Set<Role> getSelectedRoles(String argument) {
    String roleString;
    if (argumentSet("all")) {
        roleString = getArgumentValueOrElse(argument, getCommandInput());
    } else {
        roleString = getArgumentValue(argument);
    }
    Guild guild = getContext().getGuild();
    Set<Role> selectedRoles = Sets.newHashSet();
    List<String> roles = COMMA_SPLITTER.splitToList(roleString);
    for (String role : roles) {
        List<Role> rolesByName = guild.getRolesByName(role, true);
        if (rolesByName.isEmpty()) {
            throw new InvalidCommandException("No such role " + role);
        }
        selectedRoles.addAll(rolesByName);
    }
    if (selectedRoles.isEmpty()) {
        throw new InvalidCommandException("No roles selected. Either use the $to argument or, if the $all argument is used, provide the roles as command input.");
    }
    return selectedRoles;
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Guild(net.dv8tion.jda.api.entities.Guild)

Example 5 with InvalidCommandException

use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.

the class PermissionCommand method createCustomPermissionTarget.

private void createCustomPermissionTarget() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    String identifier = getCommandInput();
    CommandContext context = getContext();
    Session session = context.getSession();
    Optional<? extends PermissionTarget> existingPermissionTarget = securityManager.getPermissionTarget(identifier);
    if (existingPermissionTarget.isPresent()) {
        throw new InvalidCommandException(String.format("Permission target '%s' already exists.", identifier));
    }
    Guild guild = context.getGuild();
    User user = context.getUser();
    CustomPermissionTarget customPermissionTarget = new CustomPermissionTarget(identifier, guild, user);
    invoke(() -> session.persist(customPermissionTarget));
}
Also used : CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) User(net.dv8tion.jda.api.entities.User) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Guild(net.dv8tion.jda.api.entities.Guild) Session(org.hibernate.Session)

Aggregations

InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)47 Guild (net.dv8tion.jda.api.entities.Guild)12 Session (org.hibernate.Session)11 CommandContext (net.robinfriedli.aiode.command.CommandContext)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 StringList (net.robinfriedli.stringlist.StringList)9 List (java.util.List)8 Playlist (net.robinfriedli.aiode.entities.Playlist)8 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)7 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)6 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)5 Collection (java.util.Collection)5 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)5 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)5 IOException (java.io.IOException)4 User (net.dv8tion.jda.api.entities.User)4 AudioManager (net.robinfriedli.aiode.audio.AudioManager)4 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)4 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)4 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)4