Files
src
build.gradle
manifest.kod
Code
3 125
src/main/java/com/kodari/chatbridge/Main.java
package com.kodari.chatbridge;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class Main {
private Main() {
}
public static void main(String[] args) throws InterruptedException {
String token = requiredEnvironmentVariable("DISCORD_TOKEN");
Set<String> channelIds = Stream.of(requiredEnvironmentVariable("BRIDGE_CHANNEL_IDS").split(","))
.map(String::trim)
.filter(channelId -> !channelId.isEmpty())
.collect(Collectors.toCollection(LinkedHashSet::new));
if (channelIds.size() < 2) {
throw new IllegalArgumentException("BRIDGE_CHANNEL_IDS must contain at least two comma-separated text channel IDs.");
}
JDA jda = JDABuilder.createDefault(token)
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new ChatBridge(channelIds))
.build();
jda.awaitReady();
System.out.println("Chat bridge is online for " + channelIds.size() + " channels.");
}
private static String requiredEnvironmentVariable(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) {
throw new IllegalStateException("Set the " + name + " environment variable before starting the bot.");
}
return value;
}
private static final class ChatBridge extends ListenerAdapter {
private final Set<String> channelIds;
private ChatBridge(Set<String> channelIds) {
this.channelIds = Set.copyOf(channelIds);
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
Message message = event.getMessage();
String sourceChannelId = event.getChannel().getId();
if (message.getAuthor().isBot() || message.isWebhookMessage() || !channelIds.contains(sourceChannelId)) {
return;
}
String text = message.getContentDisplay();
for (Message.Attachment attachment : message.getAttachments()) {
if (!text.isEmpty()) {
text += "\n";
}
text += attachment.getUrl();
}
if (text.isBlank()) {
return;
}
String displayName = event.getMember() == null
? message.getAuthor().getName()
: event.getMember().getEffectiveName();
String relayedMessage = ("[" + event.getGuild().getName() + "] " + displayName + ": " + text)
.replace("@", "@\u200B");
if (relayedMessage.codePointCount(0, relayedMessage.length()) > 2000) {
relayedMessage = relayedMessage.substring(0, relayedMessage.offsetByCodePoints(0, 1997)) + "...";
}
for (String targetChannelId : channelIds) {
if (targetChannelId.equals(sourceChannelId)) {
continue;
}
TextChannel target = event.getJDA().getTextChannelById(targetChannelId);
if (target == null) {
System.err.println("Bridge channel is unavailable: " + targetChannelId);
continue;
}
target.sendMessage(relayedMessage).queue(
ignored -> { },
error -> System.err.println("Could not relay message to " + targetChannelId + ": " + error.getMessage())
);
}
}
}
}