Add command for managing staff join dates

Implemented `CommandStaffJoinDate` to allow viewing and setting staff join dates. Included methods in `QueriesStaffJoinDate` for database interactions and defined the `JoinDate` record to encapsulate join date information.
This commit is contained in:
Teriuihi 2024-08-25 19:39:43 +02:00
parent cbf51f3fc8
commit 8a65c27bac
4 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package com.alttd.DTO;
import java.time.Instant;
public record JoinDate (long userId, Instant joinDate){
}

View File

@ -55,6 +55,7 @@ public class CommandManager extends ListenerAdapter {
new CommandSoftLock(jda, this, lockedChannel),
new CommandDataSuggestions(jda, this),
new CommandAuction(jda, this, selectMenuManager),
new CommandStaffJoinDate(jda, this),
new CommandBal());
}

View File

@ -0,0 +1,119 @@
package com.alttd.commandManager.commands;
import com.alttd.DTO.JoinDate;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.database.queries.QueriesStaffJoinDate;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.IMentionable;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
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.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CommandStaffJoinDate extends DiscordCommand {
public CommandStaffJoinDate(JDA jda, CommandManager commandManager) {
CommandData commandData = Commands.slash(getName(), "View join date for staff members, or add them manually")
.addOption(OptionType.MENTIONABLE, "staff", "Set the soft lock \"on\" or \"off\"", false, true)
.addOption(OptionType.NUMBER, "join-time", "Channel to change soft lock state for", false, true)
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
.setGuildOnly(true);
Util.registerCommand(commandManager, jda, commandData, getName());
}
@Override
public String getName() {
return "staff-join-date";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
event.deferReply(true).queue(interactionHook -> processCommand(event, interactionHook));
}
private void processCommand(SlashCommandInteractionEvent event, InteractionHook interactionHook) {
OptionMapping staff = event.getOption("staff");
if (staff == null) {
interactionHook.editOriginalEmbeds(getFullStaffEmbed()).queue();
return;
}
Member member = staff.getAsMember();
if (member == null) {
interactionHook.editOriginalEmbeds(Util.genericErrorEmbed("Invalid member", String.format("%s is not a valid member", staff.getAsMentionable().getAsMention()))).queue();
return;
}
OptionMapping option = event.getOption("join-time");
if (option == null) {
interactionHook.editOriginalEmbeds(getStaffEmbed(member.getIdLong())).queue();
return;
}
long time = option.getAsLong();
Instant instant = Instant.ofEpochSecond(time);
if (instant.isAfter(Instant.now())) {
interactionHook.editOriginalEmbeds(Util.genericErrorEmbed("Invalid time", "The time must be in the past")).queue();
return;
}
QueriesStaffJoinDate.setJoinDate(member.getIdLong(), instant);
interactionHook.editOriginalEmbeds(Util.genericSuccessEmbed(
String.format("Set join date for %s", member.getAsMention()),
String.format("Set to <t:%d:R>", instant.getEpochSecond())))
.queue();
}
private MessageEmbed getStaffEmbed(long userId) {
Optional<JoinDate> joinDate = QueriesStaffJoinDate.getJoinDate(userId);
return joinDate.map(date -> new EmbedBuilder()
.setTitle("Join date for %s".formatted(userId))
.setDescription(String.format("<t:%d:R>", date.joinDate().getEpochSecond()))
.build())
.orElseGet(() -> Util.genericErrorEmbed("No join date found", String.format("No join date found for <@%s>", userId)));
}
private MessageEmbed getFullStaffEmbed() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("All staff join dates");
List<JoinDate> allJoinDates = QueriesStaffJoinDate.getAllJoinDates();
if (allJoinDates == null || allJoinDates.isEmpty()) {
embedBuilder.setDescription("No staff join dates found.");
} else {
embedBuilder.setDescription(allJoinDates.stream().map(joinDate ->
"<@%d>: <t:%d:R>".formatted(joinDate.userId(), joinDate.joinDate().getEpochSecond()))
.collect(Collectors.joining("\n")));
}
return embedBuilder.build();
}
@Override
public void suggest(CommandAutoCompleteInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
@Override
public CommandData getCommandData() {
return null;
}
}

View File

@ -0,0 +1,64 @@
package com.alttd.database.queries;
import com.alttd.DTO.JoinDate;
import com.alttd.database.Database;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class QueriesStaffJoinDate {
public static Optional<JoinDate> getJoinDate(long userId) {
String sql = "SELECT date FROM staff_join_date WHERE user_id = ?";
try (PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql)) {
preparedStatement.setLong(1, userId);
ResultSet resultSet = preparedStatement.executeQuery();
if (!resultSet.next())
return Optional.empty();
Instant date = Instant.ofEpochMilli(resultSet.getLong("date"));
if (date == null) {
return Optional.empty();
}
return Optional.of(new JoinDate(userId, date));
} catch (SQLException exception) {
exception.printStackTrace();
return Optional.empty();
}
}
public static void setJoinDate(long userId, Instant date) {
String sql = "INSERT INTO staff_join_date (user_id, date) VALUES (?, ?)";
try (PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql)) {
preparedStatement.setLong(1, userId);
preparedStatement.setLong(2, date.toEpochMilli());
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static List<JoinDate> getAllJoinDates() {
String sql = "SELECT * FROM staff_join_date";
try (PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql)) {
ResultSet resultSet = preparedStatement.executeQuery();
List<JoinDate> joinDates = new ArrayList<>();
while (resultSet.next())
joinDates.add(new JoinDate(resultSet.getLong("user_id"), Instant.ofEpochMilli(resultSet.getLong("date"))));
return joinDates;
} catch (SQLException exception) {
exception.printStackTrace();
return null;
}
}
}