From 33457e8270daed500a795b5aef39f3d322f724f3 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Sun, 17 Nov 2024 18:49:12 +0000 Subject: [PATCH] Migrate to slash commands --- .../hypr/doki/commands/moderation/Mute.java | 48 ------------------- .../net/hypr/doki/commands/utils/About.java | 45 ----------------- .../net/hypr/doki/commands/utils/Ping.java | 24 ++++++---- .../net/hypr/doki/commands/utils/WhoIs.java | 43 ++++++++--------- .../net/hypr/doki/utils/DurationUtils.java | 40 ++++++++++++++++ 5 files changed, 75 insertions(+), 125 deletions(-) delete mode 100644 src/main/java/net/hypr/doki/commands/moderation/Mute.java delete mode 100644 src/main/java/net/hypr/doki/commands/utils/About.java diff --git a/src/main/java/net/hypr/doki/commands/moderation/Mute.java b/src/main/java/net/hypr/doki/commands/moderation/Mute.java deleted file mode 100644 index 7a84854..0000000 --- a/src/main/java/net/hypr/doki/commands/moderation/Mute.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.hypr.doki.commands.moderation; - -import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.prefixed.BaseCommandEvent; -import com.freya02.botcommands.api.prefixed.CommandEvent; -import com.freya02.botcommands.api.prefixed.TextCommand; -import com.freya02.botcommands.api.prefixed.annotations.Category; -import com.freya02.botcommands.api.prefixed.annotations.Description; -import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand; -import com.freya02.botcommands.api.prefixed.annotations.TextOption; -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.interactions.commands.Command; -import net.hypr.doki.utils.DurationUtils; - -import java.time.Duration; - -@CommandMarker -@Category("Moderation") -@Description("Mutes a user for a specified amount of time") -public class Mute extends TextCommand { - @JDATextCommand(name = "mute", order = 1) - public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") Member member, @TextOption(example = "30m") String duration) { - Member commandExecutor = event.getMember(); - if (!commandExecutor.hasPermission(Permission.KICK_MEMBERS)) { - return; - } - Duration timeoutDuration; - try { - timeoutDuration = DurationUtils.parseDuration(duration); - } catch (IllegalArgumentException ex) { - event.reply("Invalid duration format!").queue(); - return; - } - if (Math.abs(timeoutDuration.toDays()) > 28) { - event.reply("Duration must be less than 28 days!").queue(); - return; - } - member.timeoutFor(timeoutDuration).queue(); - event.reply("Muted " + member.getAsMention() + " (" + member.getEffectiveName() + ") for " + duration).queue(); - } - - @JDATextCommand(name = "mute", order = 2) - public void execute(CommandEvent event) { - event.reply("You must specify a user and duration!").queue(); - } -} diff --git a/src/main/java/net/hypr/doki/commands/utils/About.java b/src/main/java/net/hypr/doki/commands/utils/About.java deleted file mode 100644 index ec455d7..0000000 --- a/src/main/java/net/hypr/doki/commands/utils/About.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.hypr.doki.commands.utils; - -import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.prefixed.CommandEvent; -import com.freya02.botcommands.api.prefixed.TextCommand; -import com.freya02.botcommands.api.prefixed.annotations.Category; -import com.freya02.botcommands.api.prefixed.annotations.Description; -import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.entities.SelfUser; - -import java.awt.*; - -@CommandMarker -@Category("Utils") -@Description("Get about info") -public class About extends TextCommand { - @JDATextCommand(name = "about", aliases = {"doki"}) - public void execute(CommandEvent event) { - JDA jda = event.getJDA(); - SelfUser selfUser = jda.getSelfUser(); - String jdaVersion = ""; - try { - Class jdaClass = Class.forName("net.dv8tion.jda.api.JDA"); - Package jdaPackage = jdaClass.getPackage(); - if (jdaPackage != null) { - // Attempt to get Implementation-Version from the manifest - String version = jdaPackage.getImplementationVersion(); - jdaVersion = version != null ? version : "Unknown"; - } - } catch (Exception ex) { - jdaVersion = "UNKNOWN"; - } - EmbedBuilder aboutEmbed = new EmbedBuilder() - .setTitle("About " + selfUser.getName()) - .setThumbnail(selfUser.getAvatarUrl()) - .setDescription("Banned from every deli nationwide") - .addField("Guilds", String.valueOf(jda.getGuildCache().stream().count()), true) - .addField("JDA Version", jdaVersion, true) - .addField("Owner", "fwoppydwisk", true) - .setColor(new Color(210,138,39)); - event.reply(aboutEmbed.build()).queue(); - } -} diff --git a/src/main/java/net/hypr/doki/commands/utils/Ping.java b/src/main/java/net/hypr/doki/commands/utils/Ping.java index 2234ee6..e8f1bec 100644 --- a/src/main/java/net/hypr/doki/commands/utils/Ping.java +++ b/src/main/java/net/hypr/doki/commands/utils/Ping.java @@ -1,21 +1,29 @@ package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.prefixed.CommandEvent; -import com.freya02.botcommands.api.prefixed.TextCommand; +import com.freya02.botcommands.api.application.ApplicationCommand; +import com.freya02.botcommands.api.application.CommandScope; +import com.freya02.botcommands.api.application.slash.GlobalSlashEvent; +import com.freya02.botcommands.api.application.slash.GuildSlashEvent; +import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand; import com.freya02.botcommands.api.prefixed.annotations.Category; import com.freya02.botcommands.api.prefixed.annotations.Description; -import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand; @CommandMarker @Category("Utils") @Description("Pong!") -public class Ping extends TextCommand { - @JDATextCommand(name = "ping") - public void execute(CommandEvent event) { - final long gatewayPing = event.getJDA().getGatewayPing(); +public class Ping extends ApplicationCommand { + @JDASlashCommand( + name = "ping", + description = "Pong!" + ) + public void onPing(GuildSlashEvent event) { + event.deferReply().queue(); + final long gatewayPing = event.getJDA().getGatewayPing(); event.getJDA().getRestPing() - .queue(restPing -> event.respondFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, restPing).queue()); + .queue(l -> event.getHook() + .sendMessageFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, l) + .queue()); } } diff --git a/src/main/java/net/hypr/doki/commands/utils/WhoIs.java b/src/main/java/net/hypr/doki/commands/utils/WhoIs.java index ca02f86..cc7a403 100644 --- a/src/main/java/net/hypr/doki/commands/utils/WhoIs.java +++ b/src/main/java/net/hypr/doki/commands/utils/WhoIs.java @@ -1,45 +1,40 @@ package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.prefixed.BaseCommandEvent; -import com.freya02.botcommands.api.prefixed.CommandEvent; -import com.freya02.botcommands.api.prefixed.TextCommand; +import com.freya02.botcommands.api.annotations.Optional; +import com.freya02.botcommands.api.application.ApplicationCommand; +import com.freya02.botcommands.api.application.CommandScope; +import com.freya02.botcommands.api.application.annotations.AppOption; +import com.freya02.botcommands.api.application.slash.GuildSlashEvent; +import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand; import com.freya02.botcommands.api.prefixed.annotations.Category; import com.freya02.botcommands.api.prefixed.annotations.Description; -import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand; -import com.freya02.botcommands.api.prefixed.annotations.TextOption; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.User; import java.time.Duration; import java.time.OffsetDateTime; +import java.util.Objects; import java.util.stream.Collectors; @CommandMarker @Category("Utils") @Description("Get information about a user") -public class WhoIs extends TextCommand { - @JDATextCommand(name = "whois", order = 1) - public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") Member user) { - EmbedBuilder whoIsEmbed = buildUserEmbed(user, (CommandEvent) event); - event.reply(whoIsEmbed.build()).queue(); +public class WhoIs extends ApplicationCommand { + @JDASlashCommand( + name = "whois", + description = "Gets information on a user" + ) + public void whois(GuildSlashEvent event, + @Optional @AppOption(name = "member") Member memberParam) { + Member member; + member = Objects.requireNonNullElseGet(memberParam, event::getMember); + EmbedBuilder whoIsEmbed = buildUserEmbed(member, event); + event.replyEmbeds(whoIsEmbed.build()).queue(); } - @JDATextCommand(name = "whois", order = 2) - public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") User user) { - EmbedBuilder whoIsEmbed = buildUserEmbed((Member) user, (CommandEvent) event); - event.reply(whoIsEmbed.build()).queue(); - } - - @JDATextCommand(name = "whois", order = 3) - public void execute(CommandEvent event) { - EmbedBuilder whoIsEmbed = buildUserEmbed(event.getMember(), event); - event.reply(whoIsEmbed.build()).queue(); - } - - private EmbedBuilder buildUserEmbed(Member user, CommandEvent event) { + private EmbedBuilder buildUserEmbed(Member user, GuildSlashEvent event) { return new EmbedBuilder() .setAuthor(user.getUser().getName(), null, user.getEffectiveAvatarUrl()) .setThumbnail(user.getEffectiveAvatarUrl()) diff --git a/src/main/java/net/hypr/doki/utils/DurationUtils.java b/src/main/java/net/hypr/doki/utils/DurationUtils.java index 8fef81c..3ba6e1a 100644 --- a/src/main/java/net/hypr/doki/utils/DurationUtils.java +++ b/src/main/java/net/hypr/doki/utils/DurationUtils.java @@ -1,6 +1,8 @@ package net.hypr.doki.utils; import java.time.Duration; +import java.time.Instant; +import java.time.OffsetDateTime; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,4 +26,42 @@ public class DurationUtils { default -> throw new IllegalArgumentException("Invalid unit"); }; } + + public static String getTimeDifference(OffsetDateTime offsetDateTime) { + // Get the current Instant (current time) + Instant now = Instant.now(); + + // Calculate the duration between the current time and the given OffsetDateTime + Duration duration = Duration.between(now, offsetDateTime.toInstant()); + + // Get the difference in various units + long days = duration.toDays(); + long hours = duration.toHours() % 24; // Modulo 24 to get hours within a single day + long minutes = duration.toMinutes() % 60; // Modulo 60 to get minutes within a single hour + long seconds = duration.getSeconds() % 60; // Modulo 60 to get remaining seconds + + // Build the formatted time difference string + StringBuilder result = new StringBuilder(); + + // Append each time unit if it is non-zero + if (days > 0) { + result.append(days).append(" day").append(days > 1 ? "s" : "").append(" "); + } + if (hours > 0) { + result.append(hours).append(" hour").append(hours > 1 ? "s" : "").append(" "); + } + if (minutes > 0) { + result.append(minutes).append(" minute").append(minutes > 1 ? "s" : "").append(" "); + } + if (seconds > 0) { + result.append(seconds).append(" second").append(seconds > 1 ? "s" : ""); + } + + // If the duration is zero, return "0 seconds" + if (result.isEmpty()) { + result.append("0 second"); + } + + return result.toString().trim(); + } }