From 4fc3b58ad14b5793677b723cbb05f73e9dd99ca2 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Fri, 25 Oct 2024 00:37:55 +0100 Subject: [PATCH] Add mute command --- .../hypr/doki/commands/moderation/Mute.java | 48 +++++++++++++++++++ .../net/hypr/doki/utils/DurationUtils.java | 27 +++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/net/hypr/doki/commands/moderation/Mute.java create mode 100644 src/main/java/net/hypr/doki/utils/DurationUtils.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 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/utils/DurationUtils.java b/src/main/java/net/hypr/doki/utils/DurationUtils.java new file mode 100644 index 0000000..8fef81c --- /dev/null +++ b/src/main/java/net/hypr/doki/utils/DurationUtils.java @@ -0,0 +1,27 @@ +package net.hypr.doki.utils; + +import java.time.Duration; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class DurationUtils { + private static final Pattern timePattern = Pattern.compile("(\\d+)(?:([dhms]))?"); + public static Duration parseDuration(String input) throws IllegalArgumentException { + Matcher matcher = timePattern.matcher(input); + + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid duration format"); + } + + int value = Integer.parseInt(matcher.group(1)); + String unit = matcher.group(2); + + return switch (unit == null || unit.isEmpty() ? "m" : unit.toLowerCase()) { + case "d" -> Duration.ofDays(value); + case "h" -> Duration.ofHours(value); + case "m" -> Duration.ofMinutes(value); + case "s" -> Duration.ofSeconds(value); + default -> throw new IllegalArgumentException("Invalid unit"); + }; + } +}