Skip to content

Commit

Permalink
Added support for multi-embed pages (through EmbedCluster);
Browse files Browse the repository at this point in the history
Removed public constructors from Page and InteractPage, static methods `of()` should be used now.
  • Loading branch information
ygimenez committed May 19, 2023
1 parent bfafba3 commit b189d51
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 26 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ Example:
MessageBuilder mb = new MessageBuilder();
mb.setContent("Hello World!");

Page messagePage = new Page(mb.build());
Page messagePage = Page.of(mb.build());

// Example using EmbedBuilder
EmbedBuilder eb = new EmbedBuilder()
.setTitle("Example Embed")
.setDescription("Hello World");

Page embedPage = new InteractPage(eb.build());
Page embedPage = InteractPage.of(eb.build());
```

That said, you might want to create a `List` of pages to use the pagination, since a single page does not need to be paginated at all:
Expand All @@ -125,7 +125,7 @@ List<Page> pages = new ArrayList<>();

// Adding 10 pages to the list
for (int i = 0; i < 10; i++) {
pages.add(new InteractPage("This is entry Nº " + (i + 1)));
pages.add(InteractPage.of("This is entry Nº " + (i + 1)));
}
```

Expand Down Expand Up @@ -155,13 +155,13 @@ Map<Emoji, Page> categories = new HashMap<>();

// Manually adding 3 categories to the map, you could use some kind of loop to fill it (see https://emojipedia.org/ for unicodes)
mb.setContent("This is category 1");
categories.put(Emoji.fromFormatted("\u26f3"), new InteractPage("This is category 1"));
categories.put(Emoji.fromFormatted("\u26f3"), InteractPage.of("This is category 1"));

mb.setContent("This is category 2");
categories.put(Emoji.fromFormatted("\u26bd"), new InteractPage("This is category 2"));
categories.put(Emoji.fromFormatted("\u26bd"), InteractPage.of("This is category 2"));

mb.setContent("This is category 3");
categories.put(Emoji.fromFormatted("\u270f"), new InteractPage("This is category 3"));
categories.put(Emoji.fromFormatted("\u270f"), InteractPage.of("This is category 3"));
```

Then just call `Pages.categorize()` method just like you did before:
Expand Down Expand Up @@ -212,7 +212,7 @@ for (int i = 0; i < 2; i++) {
}

ThrowingFunction<Integer, Page> func = i -> {
return new InteractPage(data.get(i));
return InteractPage.of(data.get(i));
};
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.5</version>
<version>5.0.0-beta.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/ygimenez/method/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,8 @@ public static void modifyButtons(Message msg, @Nullable Page p, Map<String, Func
act = msg.editMessage((String) p.getContent());
} else if (p.getContent() instanceof MessageEmbed) {
act = msg.editMessageEmbeds((MessageEmbed) p.getContent());
} else if (p.getContent() instanceof EmbedCluster) {
act = msg.editMessageEmbeds(((EmbedCluster) p.getContent()).getEmbeds());
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/github/ygimenez/model/EmbedCluster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.ygimenez.model;

import net.dv8tion.jda.api.entities.MessageEmbed;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
* Class used for adding more than one embed per {@link Page}.
*/
public class EmbedCluster {
private final List<MessageEmbed> embeds;

/**
* Creates a new {@link MessageEmbed} cluster to be used for multi-embed pages. While this constructor allows null
* entries, it's advised not to as it'll likely result in errors during pagination.
*
* @param embeds The embeds to be used, amount must be between 1 and 10.
*/
public EmbedCluster(@NotNull List<MessageEmbed> embeds) {
if (embeds.size() < 1 || embeds.size() >= 10) {
throw new IllegalArgumentException("Embed count must be at least 1 and at most 10");
}

this.embeds = embeds;
}

/**
* Creates a new {@link MessageEmbed} cluster to be used for multi-embed pages. This constructor will use an
* immutable {@link List}, and cannot receive null values.
*
* @param embeds The embeds to be used, amount must be between 1 and 10.
* @throws NullPointerException If any of the embeds is null.
*/
public EmbedCluster(@NotNull MessageEmbed... embeds) {
this(List.of(embeds));
}

/**
* Returns the internal list of embeds contained within this cluster.
*
* @return The stored list of {@link MessageEmbed}s.
*/
public List<MessageEmbed> getEmbeds() {
return embeds;
}
}
38 changes: 30 additions & 8 deletions src/main/java/com/github/ygimenez/model/InteractPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.ygimenez.method.Pages;
import com.github.ygimenez.type.Emote;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
Expand All @@ -14,22 +13,45 @@
import java.util.Objects;

/**
* Class representing either a {@link Message} or {@link MessageEmbed} object.
* Class representing either a {@link String}, {@link MessageEmbed} or {@link EmbedCluster} object.
* Fundamentally the same as {@link Page} but will use {@link Button}s instead of emotes.
*/
public class InteractPage extends Page {
private final Map<ButtonStyle, ButtonStyle> styles = new EnumMap<>(ButtonStyle.class);
private final Map<Emote, String> caption = new EnumMap<>(Emote.class);

protected InteractPage(@NotNull Object content) {
super(content);
}

/**
* An {@link InteractPage} object to be used in this library's methods. Currently, only {@link Message}
* and {@link MessageEmbed} are supported.
* Create a new {@link InteractPage} for embed-less page, with support for interaction buttons.
*
* @param content The {@link Message}/{@link MessageEmbed} object to be used as pages.
* @throws IllegalArgumentException Thrown if argument is not a {@link Message} nor {@link MessageEmbed}.
* @param content The desired content
* @return A new {@link InteractPage} instance.
*/
public InteractPage(@NotNull Object content) throws IllegalArgumentException {
super(content);
public static InteractPage of(@NotNull String content) {
return new InteractPage(content);
}

/**
* Create a new {@link InteractPage} for single-embed page, with support for interaction buttons.
*
* @param content The desired content
* @return A new {@link InteractPage} instance.
*/
public static InteractPage of(@NotNull MessageEmbed content) {
return new InteractPage(content);
}

/**
* Create a new {@link InteractPage} for multi-embed page, with support for interaction buttons.
*
* @param content The desired content
* @return A new {@link InteractPage} instance.
*/
public static InteractPage of(@NotNull EmbedCluster content) {
return new InteractPage(content);
}

/**
Expand Down
49 changes: 39 additions & 10 deletions src/main/java/com/github/ygimenez/model/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,52 @@
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.jetbrains.annotations.NotNull;

import java.util.stream.Collectors;

/**
* Class representing either a {@link String} or {@link MessageEmbed} object.
* Class representing either a {@link String}, {@link MessageEmbed} or {@link EmbedCluster} object.
*/
public class Page {
private final Object content;

/**
* A {@link Page} object to be used in this library's methods. Currently, only {@link String}
* and {@link MessageEmbed} are supported.
*
* @param content The {@link String}/{@link MessageEmbed} object to be used as pages.
* @throws IllegalArgumentException Thrown if argument is not a {@link String} nor {@link MessageEmbed}.
*/
public Page(@NotNull Object content) throws IllegalArgumentException {
if (!(content instanceof String) && !(content instanceof MessageEmbed)) {
protected Page(@NotNull Object content) throws IllegalArgumentException {
if (!(content instanceof String || content instanceof MessageEmbed || content instanceof EmbedCluster)) {
throw new IllegalArgumentException("Page content must be either a String or a MessageEmbed");
}

this.content = content;
}

/**
* Create a new {@link Page} for embed-less page, with support for interaction buttons.
*
* @param content The desired content
* @return A new {@link Page} instance.
*/
public static Page of(@NotNull String content) {
return new Page(content);
}

/**
* Create a new {@link Page} for single-embed page, with support for interaction buttons.
*
* @param content The desired content
* @return A new {@link Page} instance.
*/
public static Page of(@NotNull MessageEmbed content) {
return new Page(content);
}

/**
* Create a new {@link Page} for multi-embed page, with support for interaction buttons.
*
* @param content The desired content
* @return A new {@link Page} instance.
*/
public static Page of(@NotNull EmbedCluster content) {
return new Page(content);
}

/**
* Method to get this {@link Page}'s content object.
*
Expand All @@ -45,6 +70,10 @@ public String toString() {
return ((Message) content).getContentRaw();
} else if (content instanceof MessageEmbed) {
return ((MessageEmbed) content).getDescription();
} else if (content instanceof EmbedCluster) {
return ((EmbedCluster) content).getEmbeds().stream()
.map(MessageEmbed::getDescription)
.collect(Collectors.joining("\n"));
} else {
return "Unknown type";
}
Expand Down

0 comments on commit b189d51

Please sign in to comment.