Added a command to get suggestion data into excel

This commit is contained in:
Teriuihi 2023-03-12 05:32:07 +01:00
parent 11d2e2bac8
commit df6d060863
4 changed files with 183 additions and 2 deletions

View File

@ -64,7 +64,10 @@ dependencies {
// Configurate // Configurate
implementation("org.spongepowered:configurate-yaml:4.1.2") implementation("org.spongepowered:configurate-yaml:4.1.2")
// Excel
implementation("org.apache.poi:poi:5.2.0")
implementation("org.apache.poi:poi-ooxml:5.2.0")
// Other stuff?
compileOnly("org.projectlombok:lombok:1.18.24") compileOnly("org.projectlombok:lombok:1.18.24")
annotationProcessor("org.projectlombok:lombok:1.18.24") annotationProcessor("org.projectlombok:lombok:1.18.24")
} }

View File

@ -51,6 +51,7 @@ public class CommandManager extends ListenerAdapter {
new CommandToggleRole(commandSetToggleableRoles, jda, this), new CommandToggleRole(commandSetToggleableRoles, jda, this),
new CommandRemindMe(jda, this, modalManager), new CommandRemindMe(jda, this, modalManager),
new CommandSoftLock(jda, this, lockedChannel), new CommandSoftLock(jda, this, lockedChannel),
new CommandDataSuggestions(jda, this),
new CommandAuction(jda, this, selectMenuManager)); new CommandAuction(jda, this, selectMenuManager));
} }

View File

@ -0,0 +1,120 @@
package com.alttd.commandManager.commands;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.util.ExcelWriter;
import com.alttd.util.Util;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class CommandDataSuggestions extends DiscordCommand {
private final CommandData commandData;
private final CommandManager commandManager;
public CommandDataSuggestions(JDA jda, CommandManager commandManager) {
this.commandManager = commandManager;
this.commandData = Commands.slash(getName(), "Get data about suggestions from the forum channel")
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
.setGuildOnly(true);
Util.registerCommand(commandManager, jda, commandData, getName());
}
@Override
public String getName() {
return "data_suggestions";
}
private void filterAndStoreActiveSuggestions(InteractionHook reply, HashSet<ThreadChannel> activeSuggestions) {
ExcelWriter excelWriter = new ExcelWriter();
long waitSeconds = 0;
for (ThreadChannel activeSuggestion : activeSuggestions) {
activeSuggestion.getHistoryFromBeginning(1).queueAfter(waitSeconds, TimeUnit.SECONDS, hist -> {
List<Message> retrievedHistory = hist.getRetrievedHistory();
if (retrievedHistory.size() != 1) {
return;
}
Message message = retrievedHistory.get(0);
MessageReaction thumbsUp = message.getReaction(Emoji.fromUnicode("\uD83D\uDC4D"));
MessageReaction thumbsDown = message.getReaction(Emoji.fromUnicode("\uD83D\uDC4E"));
excelWriter.addRow(
message.getJumpUrl(),
activeSuggestion.getName(),
thumbsUp == null ? "err" : String.valueOf(thumbsUp.getCount()),
thumbsDown == null ? "err" : String.valueOf(thumbsDown.getCount()));
});
waitSeconds += 5;
}
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(() -> excelWriter.saveAndSend(reply), waitSeconds, TimeUnit.SECONDS);
}
private void processSuggestionThreads(SlashCommandInteractionEvent event, ForumChannel forumChannel, ForumTag unansweredTag, List<ThreadChannel> activeSuggestions) {
activeSuggestions.addAll(forumChannel.getThreadChannels());
activeSuggestions = activeSuggestions.stream()
.filter(threadChannel -> !threadChannel.isLocked())
.filter(threadChannel -> threadChannel.getAppliedTags().contains(unansweredTag))
.toList();
HashSet<ThreadChannel> activeSuggestionsSet = new HashSet<>(activeSuggestions);
event.deferReply(true).queue(reply -> filterAndStoreActiveSuggestions(reply, activeSuggestionsSet));
}
@Override
public void execute(SlashCommandInteractionEvent event) {
Guild guild = event.getGuild();
if (guild == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "This command needs to be executed from a guild")).queue();
return;
}
ForumChannel forumChannel = guild.getForumChannelById(1019660718867488768L);
if (forumChannel == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Can't find the forum channel in this guild")).queue();
return;
}
List<ForumTag> tagList = forumChannel.getAvailableTagsByName("unanswered", true);
if (tagList.size() != 1) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Expected one unanswered tag found: " + tagList.size())).queue();
return;
}
ForumTag unansweredTag = tagList.get(0);
forumChannel.retrieveArchivedPublicThreadChannels().queue(threads -> {
processSuggestionThreads(event, forumChannel, unansweredTag, threads);
});
}
@Override
public void suggest(CommandAutoCompleteInteractionEvent event) {
event.replyChoices(List.of()).queue();
}
@Override
public String getHelpMessage() {
return null;
}
@Override
public CommandData getCommandData() {
return commandData;
}
}

View File

@ -0,0 +1,57 @@
package com.alttd.util;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.utils.FileUpload;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWriter {
private final String fileName = "suggestions.xlsx";
private final String sheetName = "Sheet1";
private final Workbook workbook;
private final Sheet sheet;
private int currentRow = 0;
private boolean done = false;
public ExcelWriter() {
this.workbook = new XSSFWorkbook();
// Create a new sheet in the workbook
this.sheet = workbook.createSheet(sheetName);
}
public synchronized void addRow(String... data) {
if (done) {
Logger.warning("Tried to write to finished excel file");
return;
}
Row row = sheet.createRow(currentRow++);
int curCel = 0;
for (String entry : data) {
Cell cell = row.createCell(curCel++);
cell.setCellValue(entry);
}
}
public synchronized void saveAndSend(InteractionHook reply) {
done = true;
try {
File file = new File(fileName);
FileOutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
workbook.close();
reply.sendFiles(FileUpload.fromData(file)).queue(done -> {
//noinspection ResultOfMethodCallIgnored
file.delete();
});
} catch (IOException e) {
reply.sendMessageEmbeds(Util.genericErrorEmbed("Error", "Error while uploading excel file")).queue();
}
}
}