1010import org .slf4j .LoggerFactory ;
1111
1212import javax .annotation .Nonnull ;
13+ import java .time .Instant ;
1314import java .util .*;
1415
1516/**
@@ -45,6 +46,11 @@ private static void muteMember(@NotNull Member member) {
4546 .queue ();
4647 }
4748
49+ private static boolean isActionEffective (@ NotNull ActionRecord action ) {
50+ // Effective if permanent or expires in the future
51+ return action .actionExpiresAt () == null || action .actionExpiresAt ().isAfter (Instant .now ());
52+ }
53+
4854 @ Override
4955 public void onGuildMemberJoin (@ Nonnull GuildMemberJoinEvent event ) {
5056 Member member = event .getMember ();
@@ -55,27 +61,24 @@ public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
5561 }
5662
5763 private boolean shouldMemberBeMuted (@ NotNull IPermissionHolder member ) {
58- List <ActionRecord > actions = new ArrayList <>(actionsStore
59- .getActionsByTargetAscending (member .getGuild ().getIdLong (), member .getIdLong ()));
60- Collections .reverse (actions );
61-
62- Optional <ActionRecord > lastMute = actions .stream ()
63- .filter (action -> action .actionType () == ModerationAction .MUTE )
64- .findFirst ();
64+ Optional <ActionRecord > lastMute = actionsStore .findLastActionAgainstTargetByType (
65+ member .getGuild ().getIdLong (), member .getIdLong (), ModerationAction .MUTE );
6566 if (lastMute .isEmpty ()) {
6667 // User was never muted
6768 return false ;
6869 }
6970
70- Optional <ActionRecord > lastUnmute = actions .stream ()
71- .filter (action -> action .actionType () == ModerationAction .UNMUTE )
72- .findFirst ();
71+ Optional <ActionRecord > lastUnmute = actionsStore .findLastActionAgainstTargetByType (
72+ member .getGuild ().getIdLong (), member .getIdLong (), ModerationAction .UNMUTE );
7373 if (lastUnmute .isEmpty ()) {
7474 // User was never unmuted
75- return true ;
75+ return isActionEffective ( lastMute . orElseThrow ()) ;
7676 }
7777
7878 // The last issued action takes priority
79- return lastMute .orElseThrow ().issuedAt ().isAfter (lastUnmute .orElseThrow ().issuedAt ());
79+ if (lastMute .orElseThrow ().issuedAt ().isAfter (lastUnmute .orElseThrow ().issuedAt ())) {
80+ return isActionEffective (lastMute .orElseThrow ());
81+ }
82+ return false ;
8083 }
8184}
0 commit comments