diff --git a/src/main/java/net/hypr/doki/Doki.java b/src/main/java/net/hypr/doki/Doki.java index 51971f1..c2095f5 100644 --- a/src/main/java/net/hypr/doki/Doki.java +++ b/src/main/java/net/hypr/doki/Doki.java @@ -49,7 +49,7 @@ public class Doki { start(); jda = getJDA(); CommandsBuilder.newBuilder(437970062922612737L) - .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.disableHelpCommand(true)) + .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(getPrefix())) .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib } catch (Exception e) { log.error("Failed to start the bot", e); diff --git a/src/main/java/net/hypr/doki/commands/moderation/Mute.java b/src/main/java/net/hypr/doki/commands/moderation/Mute.java new file mode 100644 index 0000000..7a84854 --- /dev/null +++ b/src/main/java/net/hypr/doki/commands/moderation/Mute.java @@ -0,0 +1,48 @@ +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/moderation/Timeout.java b/src/main/java/net/hypr/doki/commands/moderation/Timeout.java deleted file mode 100644 index fbce85d..0000000 --- a/src/main/java/net/hypr/doki/commands/moderation/Timeout.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.hypr.doki.commands.moderation; - -import com.freya02.botcommands.api.annotations.BotPermissions; -import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.annotations.UserPermissions; -import com.freya02.botcommands.api.application.ApplicationCommand; -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 net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Member; -import net.hypr.doki.utils.DurationUtils; - -import java.time.Duration; -import java.time.OffsetDateTime; - -@CommandMarker -@BotPermissions(Permission.MODERATE_MEMBERS) -@UserPermissions(Permission.MODERATE_MEMBERS) -public class Timeout extends ApplicationCommand { - @JDASlashCommand( - name = "timeout", - subcommand = "set", - description = "Times out a member" - ) - public void mute(GuildSlashEvent event, - @AppOption(name = "member") Member member, - @AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration) { - Duration timeoutDuration = DurationUtils.parseDuration(duration); - member.timeoutFor(timeoutDuration).queue(); - event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue(); - } - - @JDASlashCommand( - name = "timeout", - subcommand = "cancel", - description = "Cancels the specified users timeout" - ) - public void cancelMute(GuildSlashEvent event, - @AppOption(name = "member") Member member) { - OffsetDateTime timeoutEnd = member.getTimeOutEnd(); - member.removeTimeout().queue(); - event.replyFormat("Removed %s's timeout (%s remaining)", member.getAsMention(), DurationUtils.getTimeDifference(timeoutEnd)).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 new file mode 100644 index 0000000..ec455d7 --- /dev/null +++ b/src/main/java/net/hypr/doki/commands/utils/About.java @@ -0,0 +1,45 @@ +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 557fba7..2234ee6 100644 --- a/src/main/java/net/hypr/doki/commands/utils/Ping.java +++ b/src/main/java/net/hypr/doki/commands/utils/Ping.java @@ -1,27 +1,21 @@ package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.application.ApplicationCommand; -import com.freya02.botcommands.api.application.slash.GuildSlashEvent; -import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand; +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; @CommandMarker @Category("Utils") @Description("Pong!") -public class Ping extends ApplicationCommand { - @JDASlashCommand( - name = "ping", - description = "Pong!" - ) - public void onPing(GuildSlashEvent event) { - event.deferReply().queue(); - +public class Ping extends TextCommand { + @JDATextCommand(name = "ping") + public void execute(CommandEvent event) { final long gatewayPing = event.getJDA().getGatewayPing(); + event.getJDA().getRestPing() - .queue(l -> event.getHook() - .sendMessageFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, l) - .queue()); + .queue(restPing -> event.respondFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, restPing).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 cf0101c..ca02f86 100644 --- a/src/main/java/net/hypr/doki/commands/utils/WhoIs.java +++ b/src/main/java/net/hypr/doki/commands/utils/WhoIs.java @@ -1,39 +1,45 @@ package net.hypr.doki.commands.utils; import com.freya02.botcommands.api.annotations.CommandMarker; -import com.freya02.botcommands.api.annotations.Optional; -import com.freya02.botcommands.api.application.ApplicationCommand; -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.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.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 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(); +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(); } - private EmbedBuilder buildUserEmbed(Member user, GuildSlashEvent event) { + @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) { 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 3ba6e1a..8fef81c 100644 --- a/src/main/java/net/hypr/doki/utils/DurationUtils.java +++ b/src/main/java/net/hypr/doki/utils/DurationUtils.java @@ -1,8 +1,6 @@ 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; @@ -26,42 +24,4 @@ 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(); - } }