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());
}
}
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();
}
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);
});
}
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));
}
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();
}
}
Aggregations