Search in sources :

Example 1 with CommandContext

use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.

the class CommandListener method startCommandExecution.

private void startCommandExecution(String namePrefix, Message message, Guild guild, GuildContext guildContext, Session session, GuildMessageReceivedEvent event) {
    ThreadExecutionQueue queue = executionQueueManager.getForGuild(guild);
    String commandBody = message.getContentDisplay().substring(namePrefix.length()).trim();
    CommandContext commandContext = new CommandContext(event, guildContext, hibernateComponent.getSessionFactory(), spotifyApiBuilder, commandBody);
    ExecutionContext.Current.set(commandContext.fork());
    try {
        Optional<AbstractCommand> commandInstance = commandManager.instantiateCommandForContext(commandContext, session);
        commandInstance.ifPresent(command -> commandManager.runCommand(command, queue));
    } catch (UserException e) {
        EmbedBuilder embedBuilder = e.buildEmbed();
        messageService.sendTemporary(embedBuilder.build(), commandContext.getChannel());
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) CommandContext(net.robinfriedli.aiode.command.CommandContext) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) UserException(net.robinfriedli.aiode.exceptions.UserException)

Example 2 with CommandContext

use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.

the class PermissionCommand method listPermissions.

private void listPermissions() {
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    GuildContext guildContext = context.getGuildContext();
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    List<PermissionTarget> targets = Lists.newArrayList();
    for (PermissionTarget.TargetType targetType : PermissionTarget.TargetType.values()) {
        if (!targetType.isChildTargetOnly()) {
            Set<? extends PermissionTarget> permissionTargetsInCategory = targetType.getAllPermissionTargetsInCategory();
            permissionTargetsInCategory.stream().sorted(Comparator.comparingInt((ToIntFunction<PermissionTarget>) permissionTarget -> permissionTarget.getPermissionTypeCategory().getSorting()).thenComparing(PermissionTarget::getPermissionTargetIdentifier)).forEach(permissionTarget -> {
                List<PermissionTarget> flattenedPermissionHierarchy = Lists.newArrayList();
                LinkedList<List<PermissionTarget>> stack = new LinkedList<>();
                stack.push(flattenedPermissionHierarchy);
                flattenPermissionTargetHierarchy(permissionTarget, stack, securityManager, guild, true);
                targets.addAll(flattenedPermissionHierarchy);
            });
        }
    }
    PermissionListPaginationWidget permissionListPaginationWidget = new PermissionListPaginationWidget(guildContext.getWidgetRegistry(), guild, context.getChannel(), targets, securityManager);
    permissionListPaginationWidget.initialise();
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) Guild(net.dv8tion.jda.api.entities.Guild) LinkedList(java.util.LinkedList) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PermissionListPaginationWidget(net.robinfriedli.aiode.command.widget.widgets.PermissionListPaginationWidget) ToIntFunction(java.util.function.ToIntFunction) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with CommandContext

use of net.robinfriedli.aiode.command.CommandContext 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 CommandContext

use of net.robinfriedli.aiode.command.CommandContext 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)

Example 5 with CommandContext

use of net.robinfriedli.aiode.command.CommandContext in project aiode by robinfriedli.

the class ScriptCommand method doRun.

@Override
public void doRun() {
    Aiode aiode = Aiode.get();
    CommandContext context = getContext();
    Session session = context.getSession();
    if (script != null || argumentSet("invoke")) {
        executeScript(aiode, context, session);
    } else {
        super.doRun();
    }
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) Aiode(net.robinfriedli.aiode.Aiode) Session(org.hibernate.Session)

Aggregations

CommandContext (net.robinfriedli.aiode.command.CommandContext)21 Session (org.hibernate.Session)9 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)8 Guild (net.dv8tion.jda.api.entities.Guild)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)6 Aiode (net.robinfriedli.aiode.Aiode)6 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)6 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)5 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)5 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)5 StoredScript (net.robinfriedli.aiode.entities.StoredScript)3 List (java.util.List)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Role (net.dv8tion.jda.api.entities.Role)2 User (net.dv8tion.jda.api.entities.User)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 AudioManager (net.robinfriedli.aiode.audio.AudioManager)2 ThreadExecutionQueue (net.robinfriedli.aiode.concurrent.ThreadExecutionQueue)2 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)2 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)2