Handle HierarchyExceptions in timeout command
This commit is contained in:
parent
24961d3b17
commit
b3d57a0fb9
1 changed files with 15 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
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();
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue