[AB-94] adding possibility to reference member/role/channel via name for parameters

showing an embed for parameter handling exceptions
adding discord webhook for build status
This commit is contained in:
Sheldan
2021-02-20 23:17:21 +01:00
parent a20fe82e15
commit 9db5889160
12 changed files with 168 additions and 38 deletions

View File

@@ -1,12 +1,15 @@
package dev.sheldan.abstracto.core.command.handler;
import dev.sheldan.abstracto.core.command.CommandConstants;
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.provided.MemberParameterHandler;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
@@ -29,9 +32,20 @@ public class MemberParameterHandlerImpl implements MemberParameterHandler {
if(matcher.matches()) {
return CompletableFuture.completedFuture(iterators.getMemberIterator().next());
} else {
// TODO add handling for names
long userId = Long.parseLong(inputString);
return context.getGuild().retrieveMemberById(userId).submit().thenApply(member -> member);
if(NumberUtils.isParsable(inputString)) {
long userId = Long.parseLong(inputString);
return context.getGuild().retrieveMemberById(userId).submit().thenApply(member -> member);
} else {
List<Member> possibleMembers = context.getGuild().getMembersByName(inputString, true);
if(possibleMembers.isEmpty()) {
throw new AbstractoTemplatedException("No member found with name.", "no_member_found_by_name_exception");
}
if(possibleMembers.size() > 1) {
throw new AbstractoTemplatedException("Multiple members found with name.", "multiple_members_found_by_name_exception");
}
return CompletableFuture.completedFuture(possibleMembers.get(0));
}
}
}

View File

@@ -1,12 +1,15 @@
package dev.sheldan.abstracto.core.command.handler;
import dev.sheldan.abstracto.core.command.CommandConstants;
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.provided.RoleParameterHandler;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.regex.Matcher;
@Component
@@ -23,8 +26,19 @@ public class RoleParameterHandlerImpl implements RoleParameterHandler {
if(matcher.matches()) {
return iterators.getRoleIterator().next();
} else {
long roleId = Long.parseLong(inputString);
return context.getGuild().getRoleById(roleId);
if(NumberUtils.isParsable(inputString)) {
long roleId = Long.parseLong(inputString);
return context.getGuild().getRoleById(roleId);
} else {
List<Role> roles = context.getGuild().getRolesByName(inputString, true);
if(roles.isEmpty()) {
throw new AbstractoTemplatedException("No role found with name.", "no_role_found_by_name_exception");
}
if(roles.size() > 1) {
throw new AbstractoTemplatedException("Multiple roles found with name.", "multiple_roles_found_by_name_exception");
}
return roles.get(0);
}
}
}

View File

@@ -1,12 +1,15 @@
package dev.sheldan.abstracto.core.command.handler;
import dev.sheldan.abstracto.core.command.CommandConstants;
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.provided.TextChannelParameterHandler;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.regex.Matcher;
@Component
@@ -23,8 +26,19 @@ public class TextChannelParameterHandlerImpl implements TextChannelParameterHand
if(matcher.matches()) {
return iterators.getChannelIterator().next();
} else {
long channelId = Long.parseLong(inputString);
return context.getGuild().getTextChannelById(channelId);
if(NumberUtils.isParsable(inputString)) {
long channelId = Long.parseLong(inputString);
return context.getGuild().getTextChannelById(channelId);
} else {
List<TextChannel> possibleTextChannels = context.getGuild().getTextChannelsByName(inputString, true);
if(possibleTextChannels.isEmpty()) {
throw new AbstractoTemplatedException("No channel found with name.", "no_channel_found_by_name_exception");
}
if(possibleTextChannels.size() > 1) {
throw new AbstractoTemplatedException("Multiple channels found with name.", "multiple_channels_found_by_name_exception");
}
return possibleTextChannels.get(0);
}
}
}

View File

@@ -41,25 +41,30 @@ public class ExceptionServiceBean implements ExceptionService {
@Override
public CommandResult reportExceptionToContext(Throwable throwable, CommandContext context, Command command) {
if(command != null && command.getConfiguration().isSupportsEmbedException()) {
if(command != null) {
log.info("Reporting generic exception {} of command {} towards channel {} in server {}.",
throwable.getClass().getSimpleName(), command.getConfiguration().getName(), context.getChannel().getId(), context.getGuild().getId());
} else {
log.info("Reporting generic exception {} towards channel {} in server {}.",
throwable.getClass().getSimpleName(), context.getChannel().getId(), context.getGuild().getId());
}
if((command != null && command.getConfiguration().isSupportsEmbedException()) || throwable instanceof Templatable) {
try {
GenericExceptionModel exceptionModel = buildCommandModel(throwable, context);
log.info("Reporting generic exception {} of command {} towards channel {} in server {}.",
throwable.getClass().getSimpleName(), command.getConfiguration().getName(), context.getChannel().getId(), context.getGuild().getId());
channelService.sendEmbedTemplateInTextChannelList("generic_command_exception", exceptionModel, context.getChannel());
reportGenericException(throwable, context);
} catch (Exception e) {
log.error("Failed to notify about exception.", e);
}
} else if(throwable instanceof Templatable){
GenericExceptionModel exceptionModel = buildCommandModel(throwable, context);
String text = templateService.renderTemplate(MODEL_WRAPPER_TEMPLATE_KEY, exceptionModel, context.getGuild().getIdLong());
channelService.sendTextToChannel(text, context.getChannel());
} else {
channelService.sendTextToChannel(throwable.getLocalizedMessage(), context.getChannel());
}
return CommandResult.fromReportedError();
}
private void reportGenericException(Throwable throwable, CommandContext context) {
GenericExceptionModel exceptionModel = buildCommandModel(throwable, context);
channelService.sendEmbedTemplateInTextChannelList("generic_command_exception", exceptionModel, context.getChannel());
}
@Override
public void reportExceptionToGuildMessageReceivedContext(Throwable exception, GuildMessageReceivedEvent event) {
if(exception instanceof Templatable){

View File

@@ -225,9 +225,9 @@ public class ReactionServiceBean implements ReactionService {
public CompletableFuture<Void> removeReaction(CachedMessage message, CachedEmote cachedEmote, ServerUser user) {
CompletableFuture<Message> messageFuture = messageService.loadMessageFromCachedMessage(message);
CompletableFuture<Member> memberFuture = memberService.retrieveMemberInServer(user);
return FutureUtils.toSingleFuture(Arrays.asList(messageFuture, memberFuture)).thenCompose(unused -> {
return removeReaction(messageFuture.join(), cachedEmote, memberFuture.join().getUser());
});
return FutureUtils.toSingleFuture(Arrays.asList(messageFuture, memberFuture)).thenCompose(unused ->
removeReaction(messageFuture.join(), cachedEmote, memberFuture.join().getUser())
);
}
@Override

View File

@@ -88,9 +88,9 @@ public class StartupServiceBean implements Startup {
Set<Long> knownRolesId = SnowflakeUtils.getOwnItemsIds(knownARoles);
Set<Long> availableRoles = SnowflakeUtils.getSnowflakeIds(existingRoles);
Set<Long> newRoles = SetUtils.difference(availableRoles, knownRolesId);
newRoles.forEach(aLong -> {
roleManagementService.createRole(aLong, existingAServer);
});
newRoles.forEach(aLong ->
roleManagementService.createRole(aLong, existingAServer)
);
}
private void synchronizeChannelsOf(Guild guild, AServer existingServer){

View File

@@ -286,7 +286,7 @@ public class TemplateServiceBean implements TemplateService {
public String renderSimpleTemplate(String key, Long serverId) {
try {
serverContext.setServerId(serverId);
return renderSimpleTemplate(key, serverId);
return renderSimpleTemplate(key);
} finally {
serverContext.clear();
}