Don't allow forwarding the same channel to the same Discord channel more than once

This commit is contained in:
Vankka 2023-12-23 00:30:33 +02:00
parent 37bddcbe81
commit 7297cad96c
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
9 changed files with 31 additions and 29 deletions

View File

@ -34,6 +34,7 @@ import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
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;
@ -84,7 +85,7 @@ public class ChannelLockingModule extends AbstractModule<DiscordSRV> {
return;
}
List<DiscordGuildMessageChannel> destinations = discordSRV.discordAPI()
Collection<DiscordGuildMessageChannel> destinations = discordSRV.discordAPI()
.findDestinations((BaseChannelConfig & IChannelConfig) config, true);
for (DiscordGuildMessageChannel destination : destinations) {

View File

@ -35,10 +35,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
@ -104,8 +101,8 @@ public abstract class BroadcastCommand implements GameCommandExecutor, GameComma
String channel = arguments.getString("channel");
String content = arguments.getString("content");
List<DiscordMessageChannel> channels = new ArrayList<>();
CompletableFuture<List<DiscordGuildMessageChannel>> future = null;
Set<DiscordMessageChannel> channels = new HashSet<>();
CompletableFuture<Collection<DiscordGuildMessageChannel>> future = null;
try {
long id = Long.parseUnsignedLong(channel);
@ -124,6 +121,8 @@ public abstract class BroadcastCommand implements GameCommandExecutor, GameComma
if (future != null) {
future.whenComplete((messageChannels, t) -> doBroadcast(sender, content, channel, messageChannels));
} else if (channels.isEmpty()) {
// TODO: please specify target
} else {
doBroadcast(sender, content, channel, channels);
}
@ -141,7 +140,7 @@ public abstract class BroadcastCommand implements GameCommandExecutor, GameComma
.collect(Collectors.toList());
}
private void doBroadcast(ICommandSender sender, String content, String channel, List<? extends DiscordMessageChannel> channels) {
private void doBroadcast(ICommandSender sender, String content, String channel, Collection<? extends DiscordMessageChannel> channels) {
if (channels.isEmpty()) {
sender.sendMessage(
Component.text()

View File

@ -303,7 +303,7 @@ public class SingleConsoleHandler {
return null;
}
DiscordGuildMessageChannel channel = channels.get(0);
DiscordGuildMessageChannel channel = channels.iterator().next();
if (mostRecentMessageId != null) {
long messageId = mostRecentMessageId;
if (isFull) {

View File

@ -59,10 +59,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@ -91,14 +88,14 @@ public class DiscordAPIImpl implements DiscordAPI {
return cachedClients;
}
public <T extends BaseChannelConfig & IChannelConfig> List<DiscordGuildMessageChannel> findDestinations(
public <T extends BaseChannelConfig & IChannelConfig> Collection<DiscordGuildMessageChannel> findDestinations(
T config,
boolean log
) {
return findOrCreateDestinations(config, false, log).join();
}
public <T extends BaseChannelConfig & IChannelConfig> CompletableFuture<List<DiscordGuildMessageChannel>> findOrCreateDestinations(
public <T extends BaseChannelConfig & IChannelConfig> CompletableFuture<Collection<DiscordGuildMessageChannel>> findOrCreateDestinations(
T config,
boolean create,
boolean log
@ -106,20 +103,21 @@ public class DiscordAPIImpl implements DiscordAPI {
return findOrCreateDestinations(config.destination(), config.channelLocking.threads.unarchive, create, log);
}
public CompletableFuture<List<DiscordGuildMessageChannel>> findOrCreateDestinations(
public CompletableFuture<Collection<DiscordGuildMessageChannel>> findOrCreateDestinations(
DestinationConfig destination,
boolean unarchive,
boolean create,
boolean log
) {
List<DiscordGuildMessageChannel> channels = new CopyOnWriteArrayList<>();
Set<DiscordGuildMessageChannel> channels = new HashSet<>();
for (Long channelId : destination.channelIds) {
DiscordMessageChannel channel = getMessageChannelById(channelId);
if (!(channel instanceof DiscordGuildMessageChannel)) {
continue;
}
channels.add((DiscordGuildMessageChannel) channel);
synchronized (channels) {
channels.add((DiscordGuildMessageChannel) channel);
}
}
List<CompletableFuture<Void>> threadFutures = new ArrayList<>();
@ -143,7 +141,9 @@ public class DiscordAPIImpl implements DiscordAPI {
if (thread != null) {
ThreadChannel jdaChannel = thread.asJDA();
if (!jdaChannel.isArchived()) {
channels.add(getThreadChannel(jdaChannel));
synchronized (channels) {
channels.add(getThreadChannel(jdaChannel));
}
continue;
}
}
@ -173,7 +173,9 @@ public class DiscordAPIImpl implements DiscordAPI {
}
if (threadChannel != null) {
channels.add(threadChannel);
synchronized (channels) {
channels.add(threadChannel);
}
}
return null;
}));

View File

@ -47,8 +47,8 @@ public abstract class AbstractDiscordMessageChannel<T extends MessageChannel> im
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractDiscordGuildMessageChannel<?> that = (AbstractDiscordGuildMessageChannel<?>) o;
if (!(o instanceof DiscordMessageChannel)) return false;
DiscordMessageChannel that = (DiscordMessageChannel) o;
return Objects.equals(getId(), that.getId());
}

View File

@ -166,7 +166,7 @@ public abstract class AbstractGameMessageModule<T extends IMessageConfig, E exte
T config,
IPlayer player,
SendableDiscordMessage.Builder format,
List<DiscordGuildMessageChannel> channels,
Collection<DiscordGuildMessageChannel> channels,
E event,
Object... context
) {

View File

@ -28,8 +28,8 @@ import com.discordsrv.common.config.main.channels.StartMessageConfig;
import com.discordsrv.common.config.main.channels.base.BaseChannelConfig;
import com.discordsrv.common.player.IPlayer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@ -57,7 +57,7 @@ public class StartMessageModule extends AbstractGameMessageModule<StartMessageCo
StartMessageConfig config,
IPlayer player,
SendableDiscordMessage.Builder format,
List<DiscordGuildMessageChannel> channels,
Collection<DiscordGuildMessageChannel> channels,
AbstractGameMessageReceiveEvent event,
Object... context
) {

View File

@ -28,8 +28,8 @@ import com.discordsrv.common.config.main.channels.StopMessageConfig;
import com.discordsrv.common.config.main.channels.base.BaseChannelConfig;
import com.discordsrv.common.player.IPlayer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -60,7 +60,7 @@ public class StopMessageModule extends AbstractGameMessageModule<StopMessageConf
StopMessageConfig config,
IPlayer player,
SendableDiscordMessage.Builder format,
List<DiscordGuildMessageChannel> channels,
Collection<DiscordGuildMessageChannel> channels,
AbstractGameMessageReceiveEvent event,
Object... context
) {

View File

@ -81,7 +81,7 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
MinecraftToDiscordChatConfig config,
IPlayer player,
SendableDiscordMessage.Builder format,
List<DiscordGuildMessageChannel> channels,
Collection<DiscordGuildMessageChannel> channels,
GameChatMessageReceiveEvent event,
Object... context
) {