Add option to lock threads instead of just archiving

This commit is contained in:
Vankka 2024-06-23 23:56:33 +03:00
parent 88cb649171
commit 57cc5668d3
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
2 changed files with 38 additions and 53 deletions

View File

@ -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<DiscordSRV> {
@ -52,55 +51,49 @@ public class ChannelLockingModule extends AbstractModule<DiscordSRV> {
@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);
}
ChannelLockingConfig shutdownConfig = config.channelLocking;
ChannelLockingConfig.Channels channels = shutdownConfig.channels;
ChannelLockingConfig.Threads threads = shutdownConfig.threads;
private void run(boolean unlocked) {
for (BaseChannelConfig config : discordSRV.channelConfig().getAllChannels()) {
IChannelConfig channelConfig = config instanceof IChannelConfig ? (IChannelConfig) config : null;
if (channelConfig == null) {
continue;
}
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;
}
Collection<DiscordGuildMessageChannel> destinations = discordSRV.destinations()
.lookupDestination(((IChannelConfig) config).destination(), false, false).join();
boolean isThreads = threads.archive || threads.lock;
discordSRV.destinations()
.lookupDestination(channelConfig.destination(), false, true)
.whenComplete((destinations, t) -> {
for (DiscordGuildMessageChannel destination : destinations) {
if (archive && destination instanceof DiscordThreadChannel) {
((DiscordThreadChannel) destination).asJDA().getManager()
.setArchived(true)
.reason("DiscordSRV channel locking")
.queue();
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, false);
channelPermissions(channels, destination, unlocked);
}
}
});
}
}
private void channelPermissions(
ChannelLockingConfig.Channels shutdownConfig,
@ -163,15 +156,4 @@ public class ChannelLockingModule extends AbstractModule<DiscordSRV> {
}
action.reason("DiscordSRV channel locking").queue();
}
private void doForAllChannels(BiConsumer<BaseChannelConfig, IChannelConfig> channelConsumer) {
for (BaseChannelConfig config : discordSRV.channelConfig().getAllChannels()) {
IChannelConfig channelConfig = config instanceof IChannelConfig ? (IChannelConfig) config : null;
if (channelConfig == null) {
continue;
}
channelConsumer.accept(config, channelConfig);
}
}
}

View File

@ -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;
}