changed catch block in join listener for Exception, so the listener dont interrupt one another

fixed join template
added join listener to check if a joined user should be muted
changed cached message to be an intant instead
fixed wrong id in mute table
moved the timestamps for mute notifications to the footer
fixed a case for message embeds, when the embed does not have a description
This commit is contained in:
Sheldan
2020-04-24 20:04:19 +02:00
parent b41a596acd
commit 0f8a39a336
15 changed files with 78 additions and 19 deletions

View File

@@ -0,0 +1,36 @@
package dev.sheldan.abstracto.moderation.listener;
import dev.sheldan.abstracto.core.listener.JoinListener;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.moderation.config.ModerationFeatures;
import dev.sheldan.abstracto.moderation.service.MuteService;
import dev.sheldan.abstracto.moderation.service.management.MuteManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class JoinMuteListener implements JoinListener {
@Autowired
private MuteManagementService muteManagementService;
@Autowired
private MuteService muteService;
@Override
public void execute(Member member, Guild guild, AUserInAServer aUserInAServer) {
if(muteManagementService.hasActiveMute(aUserInAServer)) {
log.info("Re-muting user {} which joined the server {}, because the mute has not ended yet.", member.getIdLong(), guild.getIdLong());
muteService.applyMuteRole(aUserInAServer);
}
}
@Override
public String getFeature() {
return ModerationFeatures.MUTING;
}
}

View File

@@ -1,10 +1,11 @@
package dev.sheldan.abstracto.moderation.repository;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.moderation.models.database.Mute;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MuteRepository extends JpaRepository<Mute, Long> {
boolean existsByMutedUserAndMuteEndedFalse(AUserInAServer userInAServer);
}

View File

@@ -87,7 +87,7 @@ public class MuteServiceBean implements MuteService {
FullUser mutingUser = FullUser
.builder()
.aUserInAServer(userManagementService.loadUser(memberToMute))
.aUserInAServer(userManagementService.loadUser(mutingMember))
.member(mutingMember)
.build();
return muteUser(mutedUser, mutingUser, reason, unmuteDate, message);
@@ -123,8 +123,7 @@ public class MuteServiceBean implements MuteService {
throw new MuteException("Mute role for server has not been setup");
}
AUserInAServer userInServerBeingMuted = userBeingMuted.getAUserInAServer();
MuteRole muteRole = muteRoleManagementService.retrieveMuteRoleForServer(userInServerBeingMuted.getServerReference());
roleService.addRoleToUser(userInServerBeingMuted, muteRole.getRole());
applyMuteRole(userInServerBeingMuted);
AServerAChannelMessage origin = null;
if(message != null) {
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
@@ -147,6 +146,12 @@ public class MuteServiceBean implements MuteService {
return mute;
}
@Override
public void applyMuteRole(AUserInAServer aUserInAServer) {
MuteRole muteRole = muteRoleManagementService.retrieveMuteRoleForServer(aUserInAServer.getServerReference());
roleService.addRoleToUser(aUserInAServer, muteRole.getRole());
}
@Override
public void startUnmuteJobFor(Instant unmuteDate, Mute mute) {
Duration muteDuration = Duration.between(Instant.now(), unmuteDate);
@@ -205,6 +210,8 @@ public class MuteServiceBean implements MuteService {
.server(mute.getMutingServer())
.build();
sendUnmuteLog(unMuteLog);
mute.setMuteEnded(true);
muteManagementService.saveMute(mute);
}
@Override

View File

@@ -31,6 +31,7 @@ public class MuteManagementServiceBean implements MuteManagementService {
.mutingChannel(origin.getChannel())
.messageId(origin.getMessageId())
.reason(reason)
.muteEnded(false)
.build();
muteRepository.save(mute);
return mute;
@@ -40,4 +41,17 @@ public class MuteManagementServiceBean implements MuteManagementService {
public Mute findMute(Long muteId) {
return muteRepository.getOne(muteId);
}
@Override
public Mute saveMute(Mute mute) {
muteRepository.save(mute);
return mute;
}
@Override
public boolean hasActiveMute(AUserInAServer userInAServer) {
return muteRepository.existsByMutedUserAndMuteEndedFalse(userInAServer);
}
}

View File

@@ -28,10 +28,6 @@
"name": "Reason",
"value": "${mute.reason}"
},
{
"name": "Muted from",
"value": "${formatInstant(mute.muteDate, "yyyy-MM-dd HH:mm:ss")}"
},
{
"name": "Muted for",
"value": "${fmtDuration(muteDuration)}"
@@ -43,5 +39,6 @@
],
"footer": {
"text": "Mute #${mute.id}"
}
},
"timeStamp": "${mute.muteDate}"
}

View File

@@ -39,5 +39,6 @@
],
"footer": {
"text": "Mute #${mute.id}"
}
},
"timeStamp": "${mute.muteTargetDate}"
}