Skip to content

Commit

Permalink
feat: adding messages pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
yaansz committed Nov 20, 2023
1 parent 25b7a3d commit 0ac01c4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.awt.*;
Expand Down Expand Up @@ -44,8 +48,8 @@ public void saveMessage(Message message) {
isHateOpenai = existingMessage.isHateOpenai();
checked = existingMessage.isChecked();
}

this.service.save(new DiscordMessage(message, checked, isHate, isHateOpenai));
DiscordMessage discordMessage = new DiscordMessage(message, checked, isHate, isHateOpenai);
if(!discordMessage.getContent().isEmpty()) this.service.save(discordMessage);
}

@Override
Expand Down Expand Up @@ -88,9 +92,28 @@ public MessageEmbed getStatsByGuildId(Long guildId) throws FieldLengthException
return handler.build();
}

public MessageEmbed getStatsByGuildIdAndUserId(Long guildId, Long userId) throws FieldLengthException {
HateUser user = service.getHateStatsByGuildIdAndUserId(guildId, userId);
public void getStatsByGuildIdAndUserId(SlashCommandInteractionEvent event, Long guildId, Long userId, int page) throws FieldLengthException {
List<DiscordMessage> messages = getDiscordMessagesByPage(guildId, userId, page);
EmbedManager.EmbedHandler handler = generateHandlerToHateUser(guildId, userId, page, messages);

event.replyEmbeds(handler.build()).addActionRow(
Button.primary("hate-stats:" + guildId + ":" + userId + ":" + (page - 1), "Previous page").withDisabled(page == 0),
Button.primary("hate-stats:" + guildId + ":" + userId + ":" + (page + 1), "Next page").withDisabled(messages.size() < 5)
).setEphemeral(true).queue();
}

public void editStatsByGuildIdAndUserId(ButtonInteractionEvent event, Long guildId, Long userId, int page) throws FieldLengthException {
List<DiscordMessage> messages = getDiscordMessagesByPage(guildId, userId, page);
EmbedManager.EmbedHandler handler = generateHandlerToHateUser(guildId, userId, page, messages);

event.editMessageEmbeds(handler.build()).setActionRow(
Button.primary("hate-stats:" + guildId + ":" + userId + ":" + (page - 1), "Previous page").withDisabled(page == 0),
Button.primary("hate-stats:" + guildId + ":" + userId + ":" + (page + 1), "Next page").withDisabled(messages.size() < 5)
).queue();
}

public EmbedManager.EmbedHandler generateHandlerToHateUser(Long guildId, Long userId, int page, List<DiscordMessage> messages) throws FieldLengthException {
HateUser user = service.getHateStatsByGuildIdAndUserId(guildId, userId);
EmbedManager.EmbedHandler handler = new EmbedManager.EmbedHandler();

if(user != null) {
Expand All @@ -106,7 +129,20 @@ public MessageEmbed getStatsByGuildIdAndUserId(Long guildId, Long userId) throws
handler.getBuilder().setColor(new Color(243, 60, 99));
}

return handler.build();
StringBuilder sb = new StringBuilder();
for(DiscordMessage message : messages) {
sb.append(message.isHate() ? "🤬" : "😀").append(" ").append(message.getContent()).append("\n");
}

handler.addField(new MessageEmbed.Field("Messages", sb.toString(), false));
handler.addField(new MessageEmbed.Field("Page", String.valueOf(page + 1), false));

return handler;
}

public List<DiscordMessage> getDiscordMessagesByPage(Long guildId, Long userId, int page) {
Pageable pageable = Pageable.ofSize(5).withPage(page);
return service.getDiscordMessageByGuildIdAndUserIdAndCheckedIsTrue(guildId, userId, pageable).toList();
}

public MessageEmbed getGlobalStats() throws FieldLengthException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public DiscordMessage(Message message, boolean checked, boolean hate, boolean ha
this.userId = message.getAuthor().getIdLong();
this.guildId = message.getGuild().getIdLong();
this.channelId = message.getChannel().getIdLong();
this.content = message.getContentDisplay();
this.content = message.getContentRaw();
this.timeCreated = message.getTimeCreated();
this.timeEdited = message.isEdited() ? message.getTimeEdited() : message.getTimeCreated();
this.checked = false;
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/softawii/capivara/listeners/HateGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.softawii.capivara.exceptions.FieldLengthException;
import com.softawii.capivara.services.DiscordMessageService;
import com.softawii.curupira.annotations.IArgument;
import com.softawii.curupira.annotations.IButton;
import com.softawii.curupira.annotations.ICommand;
import com.softawii.curupira.annotations.IGroup;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -50,8 +52,7 @@ public static void stats(SlashCommandInteractionEvent event) {
public static void user(SlashCommandInteractionEvent event) {
User evaluate = event.getOption("user").getAsUser();
try {
MessageEmbed embed = manager.getStatsByGuildIdAndUserId(event.getGuild().getIdLong(), evaluate.getIdLong());
event.replyEmbeds(embed).setEphemeral(true).queue();
manager.getStatsByGuildIdAndUserId(event, event.getGuild().getIdLong(), evaluate.getIdLong(), 0);
} catch (FieldLengthException e) {
event.reply("Error: " + e.getMessage()).setEphemeral(true).queue();
}
Expand All @@ -74,4 +75,19 @@ public static void global(SlashCommandInteractionEvent event) {
event.reply("Error: " + e.getMessage()).setEphemeral(true).queue();
}
}

@IButton(id="hate-stats")
public static void statsButton(ButtonInteractionEvent event) {
String eventId = event.getComponentId();
String[] split = eventId.split(":");
Long guildId = Long.parseLong(split[1]);
Long userId = Long.parseLong(split[2]);
int page = Integer.parseInt(split[3]);

try {
manager.editStatsByGuildIdAndUserId(event, guildId, userId, page);
} catch (FieldLengthException e) {
event.reply("Error: " + e.getMessage()).setEphemeral(true).queue();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.softawii.capivara.repository;

import com.softawii.capivara.entity.DiscordMessage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand Down Expand Up @@ -73,4 +75,6 @@ public interface DiscordMessageRepository extends JpaRepository<DiscordMessage,
where CHECKED = true
""", nativeQuery = true)
Tuple getGlobalStats();

Page<DiscordMessage> getDiscordMessageByGuildIdAndUserIdAndCheckedIsTrue(Long guildId, Long userId, Pageable request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.dv8tion.jda.api.entities.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.Tuple;
Expand Down Expand Up @@ -111,4 +113,8 @@ public HateStats getGlobalStats() {

return new HateStats(messageCount, hateCount, hate);
}

public Page<DiscordMessage> getDiscordMessageByGuildIdAndUserIdAndCheckedIsTrue(Long guildId, Long userId, Pageable request) {
return repository.getDiscordMessageByGuildIdAndUserIdAndCheckedIsTrue(guildId, userId, request);
}
}

0 comments on commit 0ac01c4

Please sign in to comment.