diff --git a/common/src/main/java/com/discordsrv/common/channel/ChannelLockingModule.java b/common/src/main/java/com/discordsrv/common/channel/ChannelLockingModule.java index 9c5116be..3729794f 100644 --- a/common/src/main/java/com/discordsrv/common/channel/ChannelLockingModule.java +++ b/common/src/main/java/com/discordsrv/common/channel/ChannelLockingModule.java @@ -32,12 +32,11 @@ import net.dv8tion.jda.api.entities.IPermissionHolder; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer; import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; +import net.dv8tion.jda.api.managers.channel.concrete.ThreadChannelManager; import net.dv8tion.jda.api.requests.restaction.PermissionOverrideAction; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.function.BiConsumer; public class ChannelLockingModule extends AbstractModule { @@ -52,54 +51,48 @@ public class ChannelLockingModule extends AbstractModule { @Override public void enable() { - doForAllChannels((config, channelConfig) -> { - ChannelLockingConfig shutdownConfig = config.channelLocking; - ChannelLockingConfig.Channels channels = shutdownConfig.channels; - - discordSRV.destinations() - .lookupDestination(((IChannelConfig) config).destination(), false, true) - .whenComplete((destinations, t) -> { - if (channels.everyone || !channels.roleIds.isEmpty()) { - for (DiscordGuildMessageChannel destination : destinations) { - channelPermissions(channels, destination, true); - } - } - }); - }); + run(true); } @Override public void disable() { - doForAllChannels((config, channelConfig) -> { - if (!(config instanceof IChannelConfig)) { - return; + run(false); + } + + private void run(boolean unlocked) { + for (BaseChannelConfig config : discordSRV.channelConfig().getAllChannels()) { + IChannelConfig channelConfig = config instanceof IChannelConfig ? (IChannelConfig) config : null; + if (channelConfig == null) { + continue; } - ChannelLockingConfig shutdownConfig = config.channelLocking; - ChannelLockingConfig.Channels channels = shutdownConfig.channels; - ChannelLockingConfig.Threads threads = shutdownConfig.threads; + ChannelLockingConfig lockingConfig = config.channelLocking; + ChannelLockingConfig.Channels channels = lockingConfig.channels; + ChannelLockingConfig.Threads threads = lockingConfig.threads; - boolean archive = threads.archive; boolean isChannels = channels.everyone || !channels.roleIds.isEmpty(); - if (!threads.archive && !isChannels) { - return; - } + boolean isThreads = threads.archive || threads.lock; - Collection destinations = discordSRV.destinations() - .lookupDestination(((IChannelConfig) config).destination(), false, false).join(); - - for (DiscordGuildMessageChannel destination : destinations) { - if (archive && destination instanceof DiscordThreadChannel) { - ((DiscordThreadChannel) destination).asJDA().getManager() - .setArchived(true) - .reason("DiscordSRV channel locking") - .queue(); - } - if (isChannels) { - channelPermissions(channels, destination, false); - } - } - }); + discordSRV.destinations() + .lookupDestination(channelConfig.destination(), false, true) + .whenComplete((destinations, t) -> { + for (DiscordGuildMessageChannel destination : destinations) { + if (isThreads && destination instanceof DiscordThreadChannel) { + ThreadChannelManager manager = ((DiscordThreadChannel) destination).asJDA().getManager(); + if (threads.archive) { + manager = manager.setArchived(!unlocked); + } + if (threads.lock) { + manager = manager.setLocked(!unlocked); + } + manager.reason("DiscordSRV channel locking").queue(); + } + if (isChannels) { + channelPermissions(channels, destination, unlocked); + } + } + }); + } } private void channelPermissions( @@ -163,15 +156,4 @@ public class ChannelLockingModule extends AbstractModule { } action.reason("DiscordSRV channel locking").queue(); } - - private void doForAllChannels(BiConsumer channelConsumer) { - for (BaseChannelConfig config : discordSRV.channelConfig().getAllChannels()) { - IChannelConfig channelConfig = config instanceof IChannelConfig ? (IChannelConfig) config : null; - if (channelConfig == null) { - continue; - } - - channelConsumer.accept(config, channelConfig); - } - } } diff --git a/common/src/main/java/com/discordsrv/common/config/main/channels/ChannelLockingConfig.java b/common/src/main/java/com/discordsrv/common/config/main/channels/ChannelLockingConfig.java index df6d880c..db8c8941 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/channels/ChannelLockingConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/channels/ChannelLockingConfig.java @@ -52,7 +52,10 @@ public class ChannelLockingConfig { public static class Threads { @Comment("If the configured threads should be archived while the server is shutdown") - public boolean archive = true; + public boolean archive = false; + + @Comment("If the configured threads should be locked while the server is shutdown") + public boolean lock = true; }