From b3d57a0fb973b7c8952138156ba57541f7125b88 Mon Sep 17 00:00:00 2001 From: floppydiskette Date: Sun, 24 Nov 2024 20:59:38 +0000 Subject: [PATCH] Handle HierarchyExceptions in timeout command --- .../hypr/doki/commands/moderation/Timeout.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/hypr/doki/commands/moderation/Timeout.java b/src/main/java/net/hypr/doki/commands/moderation/Timeout.java index bcdab66..71eef4a 100644 --- a/src/main/java/net/hypr/doki/commands/moderation/Timeout.java +++ b/src/main/java/net/hypr/doki/commands/moderation/Timeout.java @@ -10,6 +10,7 @@ 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.dv8tion.jda.api.exceptions.HierarchyException; import net.hypr.doki.utils.DurationUtils; import java.time.Duration; @@ -34,8 +35,14 @@ public class Timeout extends ApplicationCommand { event.replyFormat("Invalid duration %s!, must be between 1m and 7d", duration).queue(); return; } - member.timeoutFor(timeoutDuration) - .reason(Objects.requireNonNullElse(reason, "No reason provided")).queue(); + + try { + member.timeoutFor(timeoutDuration) + .reason(Objects.requireNonNullElse(reason, "No reason provided")).queue(); + } catch (HierarchyException ex) { + event.replyFormat("Failed to time out %s (member has roles above me!)", member.getAsMention()).setEphemeral(true).queue(); + return; + } event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue(); } @@ -47,7 +54,12 @@ public class Timeout extends ApplicationCommand { public void cancelTimeout(GuildSlashEvent event, @AppOption(name = "member") Member member) { OffsetDateTime timeoutEnd = member.getTimeOutEnd(); - member.removeTimeout().queue(); + try { + member.removeTimeout().queue(); + } catch (HierarchyException ex) { + event.replyFormat("Failed to remove %s's timeout (member has roles above me!)", member.getAsMention()).setEphemeral(true).queue(); + return; + } event.replyFormat("Removed %s's timeout (%s remaining)", member.getAsMention(), DurationUtils.getTimeDifference(timeoutEnd)).queue(); }