Skip to content

Commit

Permalink
Improved Discord slot list message
Browse files Browse the repository at this point in the history
- Wraps no longer only to squads, but to slots
- Removed unnecessary new lines
- Removed unnecessary spaces
  • Loading branch information
Alf-Melmac committed Nov 23, 2021
1 parent 83a8f77 commit 547a0b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.webalf.slotbot.model.dtos.referenceless;

import de.webalf.slotbot.model.dtos.AbstractEventDto;
import de.webalf.slotbot.util.ListUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import net.dv8tion.jda.api.entities.Message;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -33,11 +35,27 @@ public List<String> getSlotList() {
List<String> messages = new ArrayList<>();
for (SquadReferencelessDto squad : getSquadList()) {
final StringBuilder squadText = squad.toSlotList();
if (slotListText.length() + 2 + squadText.length() > Message.MAX_CONTENT_LENGTH) {
//Existing message size + 2 new line + new line may not exceed maximum discord message size
if ((slotListText.length() + 2 + squadText.length() > Message.MAX_CONTENT_LENGTH) && slotListText.length() + 2 <= Message.MAX_CONTENT_LENGTH) {
slotListText.append("\n"); //New squad

final List<String> splitSquadText = new ArrayList<>(Arrays.asList(squadText.toString().split("\\n"))); //Split squad text on every slot
String nextSlotText = ListUtils.shift(splitSquadText);
//noinspection ConstantConditions We know the squadText is bigger than the content length, nextSlotText will exist before reaching null values
while (slotListText.length() + 1 + nextSlotText.length() < Message.MAX_CONTENT_LENGTH) { //Add slots until there is no more space in the message
slotListText.append("\n").append(nextSlotText);
nextSlotText = ListUtils.shift(splitSquadText);
}

messages.add(slotListText.toString());
slotListText = new StringBuilder();
slotListText = new StringBuilder(nextSlotText).append("\n").append(String.join("\n", splitSquadText)); //Next slot list message must contain the rest of the squad
} else {
//First squad in new message doesn't need a leading new line
if (slotListText.length() != 0) {
slotListText.append("\n\n");
}
slotListText.append(squadText);
}
slotListText.append("\n\n").append(squadText);
}
messages.add(slotListText.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ StringBuilder toSlotList() {
slotText.append("**");
}

slotText.append(": ");
slotText.append(":");

final boolean isBlocked = !isEmpty && LongUtils.parseLong(getUser().getId()) == User.DEFAULT_USER_ID;
if (!isEmpty && !isBlocked) {
slotText.append("**").append(MentionUtils.getUserAsMention(getUser().getId())).append("**");
slotText.append(" **").append(MentionUtils.getUserAsMention(getUser().getId())).append("**");
} else if (isBlocked) {
slotText.append("*").append(getReplacementText()).append("*");
slotText.append(" *").append(getReplacementText()).append("*");
}
return slotText;
}
Expand Down

0 comments on commit 547a0b2

Please sign in to comment.