Search in sources :

Example 11 with InvalidCommandException

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

the class PresetCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    if (argumentSet("delete")) {
        Preset preset = SearchEngine.searchPreset(session, getCommandInput());
        if (preset == null) {
            throw new InvalidCommandException(String.format("No preset found for '%s'", getCommandInput()));
        }
        invoke(() -> session.delete(preset));
    } else if (getCommandInput().isBlank()) {
        List<Preset> presets = getQueryBuilderFactory().find(Preset.class).build(session).getResultList();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (presets.isEmpty()) {
            embedBuilder.setDescription("No presets saved");
        } else {
            EmbedTable table = new EmbedTable(embedBuilder);
            table.addColumn("Name", presets, Preset::getName);
            table.addColumn("Preset", presets, Preset::getPreset);
            table.build();
        }
        sendMessage(embedBuilder);
    } else {
        String presetString = getCommandInput();
        String name = getArgumentValue("as");
        Preset existingPreset = SearchEngine.searchPreset(getContext().getSession(), name);
        if (existingPreset != null) {
            throw new InvalidCommandException("Preset " + name + " already exists");
        }
        Preset preset = new Preset(name, presetString, getContext().getGuild(), getContext().getUser());
        String testString = presetString.contains("%s") ? name + " test" : name;
        AbstractCommand abstractCommand = preset.instantiateCommand(getManager(), getContext(), testString);
        CommandParser commandParser = new CommandParser(abstractCommand, ArgumentPrefixProperty.getForCurrentContext());
        commandParser.parse();
        abstractCommand.verify();
        invoke(() -> session.persist(preset));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Preset(net.robinfriedli.aiode.entities.Preset) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) List(java.util.List) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) Session(org.hibernate.Session) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser)

Example 12 with InvalidCommandException

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

the class RenameCommand method doRun.

@Override
public void doRun() {
    String name = getCommandInput();
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    if (name.length() < 1 || name.length() > 32) {
        throw new InvalidCommandException("Length should be 1 - 32 characters");
    }
    RENAME_SYNC.execute(guild.getIdLong(), () -> {
        invoke(() -> context.getGuildContext().setBotName(name));
        try {
            guild.getSelfMember().modifyNickname(name).complete();
            couldChangeNickname = true;
        } catch (InsufficientPermissionException ignored) {
            couldChangeNickname = false;
        }
    });
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Guild(net.dv8tion.jda.api.entities.Guild)

Example 13 with InvalidCommandException

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

the class AbstractCommand method run.

/**
 * Run a custom command like you would enter it to discord, this includes commands and presets but excludes scripts
 * to avoid recursion.
 * This method is mainly used in groovy scripts.
 *
 * @param command the command string
 */
public void run(String command) {
    StaticSessionProvider.consumeSession(session -> {
        CommandContext fork = context.fork(command, session);
        AbstractCommand abstractCommand = commandManager.instantiateCommandForContext(fork, session, false).orElseThrow(() -> new InvalidCommandException("No command found for input"));
        ExecutionContext oldExecutionContext = ExecutionContext.Current.get();
        ExecutionContext.Current.set(fork);
        try {
            commandManager.getInterceptorChainWithoutScripting().intercept(abstractCommand);
        } finally {
            if (oldExecutionContext != null) {
                ExecutionContext.Current.set(oldExecutionContext);
            } else {
                ThreadContext.Current.drop(ExecutionContext.class);
            }
        }
    });
}
Also used : ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException)

Example 14 with InvalidCommandException

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

the class AbstractCommand method splitInlineArgument.

@Deprecated
protected Pair<String, String> splitInlineArgument(String part, String argument) {
    StringList words = StringList.createWithRegex(part, " ");
    List<Integer> positions = words.findPositionsOf("$" + argument, true);
    if (positions.isEmpty()) {
        throw new InvalidCommandException("Expected inline argument: " + argument);
    }
    int position = positions.get(0);
    if (position == 0 || position == words.size() - 1) {
        throw new InvalidCommandException("No input before or after inline argument: " + argument);
    }
    String left = words.subList(0, position).toSeparatedString(" ");
    String right = words.subList(position + 1, words.size()).toSeparatedString(" ");
    if (left.isBlank() || right.isBlank()) {
        throw new InvalidCommandException("No input before or after inline argument: " + argument);
    }
    return Pair.of(left.trim(), right.trim());
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList)

Example 15 with InvalidCommandException

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

the class CreateCommand method doRun.

@Override
public void doRun() {
    Session session = getContext().getSession();
    Playlist existingPlaylist = SearchEngine.searchLocalList(session, getCommandInput());
    if (existingPlaylist != null) {
        throw new InvalidCommandException("Playlist " + getCommandInput() + " already exists");
    }
    Playlist playlist = new Playlist(getCommandInput(), getContext().getUser(), getContext().getGuild());
    invoke(() -> session.persist(playlist));
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) 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