refactor: move discord and irc channels configuration to each bot's option for easy server adding and also makes the config less messy
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package me.chayapak1.chomens_bot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Configuration {
|
||||
public List<String> prefixes;
|
||||
@@ -104,13 +102,17 @@ public class Configuration {
|
||||
public String serverId;
|
||||
public boolean enableDiscordHashing = true;
|
||||
public String token;
|
||||
public Map<String, String> servers = new HashMap<>();
|
||||
public EmbedColors embedColors = new EmbedColors();
|
||||
public String trustedRoleName = "Trusted";
|
||||
public String adminRoleName = "Admin";
|
||||
public String ownerRoleName = "Owner";
|
||||
public String statusMessage = "Oh hi!";
|
||||
public String inviteLink = "https://discord.gg/xdgCkUyaA4";
|
||||
|
||||
public static class EmbedColors {
|
||||
public String normal = "#FFFF00";
|
||||
public String error = "#FF0000";
|
||||
}
|
||||
}
|
||||
|
||||
public static class IRC {
|
||||
@@ -120,7 +122,6 @@ public class Configuration {
|
||||
public int port;
|
||||
public String name = "chomens-bot";
|
||||
public String password = "";
|
||||
public Map<String, String> servers = new HashMap<>();
|
||||
}
|
||||
|
||||
public static class Music {
|
||||
@@ -132,11 +133,6 @@ public class Configuration {
|
||||
}
|
||||
}
|
||||
|
||||
public static class EmbedColors {
|
||||
public String normal = "#FFFF00";
|
||||
public String error = "#FF0000";
|
||||
}
|
||||
|
||||
public static class Eval {
|
||||
public String address = "ws://localhost:3069";
|
||||
}
|
||||
@@ -170,6 +166,8 @@ public class Configuration {
|
||||
public int port;
|
||||
public String username;
|
||||
public String serverName;
|
||||
public String discordChannel;
|
||||
public String ircChannel;
|
||||
public boolean hidden = false;
|
||||
public boolean useCore = true;
|
||||
public boolean useCorePlaceBlock = false;
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Main {
|
||||
public static final Path stopReasonFilePath = Path.of("shutdown_reason.txt");
|
||||
@@ -207,25 +208,26 @@ public class Main {
|
||||
|
||||
final boolean[] stoppedDiscord = new boolean[copiedList.size()];
|
||||
|
||||
int botIndex = 0;
|
||||
final AtomicInteger botIndex = new AtomicInteger();
|
||||
for (final Bot bot : copiedList) {
|
||||
try {
|
||||
if (discordEnabled) {
|
||||
final String channelId = Main.discord.servers.get(bot.getServerString(true));
|
||||
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
|
||||
|
||||
final MessageCreateAction messageAction = Main.discord.sendMessageInstantly(stoppingMessage, channelId, false);
|
||||
|
||||
final int finalBotIndex = botIndex;
|
||||
final int currentIndex = botIndex.get();
|
||||
|
||||
messageAction.queue(
|
||||
(message) -> stoppedDiscord[finalBotIndex] = true,
|
||||
(error) -> stoppedDiscord[finalBotIndex] = true // should i also set this to true on fail?
|
||||
(message) -> stoppedDiscord[currentIndex] = true,
|
||||
(error) -> stoppedDiscord[currentIndex] = true // should i also set this to true on fail?
|
||||
);
|
||||
}
|
||||
|
||||
bot.stop();
|
||||
} catch (final Exception ignored) { }
|
||||
|
||||
botIndex++;
|
||||
botIndex.getAndIncrement();
|
||||
}
|
||||
|
||||
if (discordEnabled) {
|
||||
|
||||
@@ -51,9 +51,9 @@ public class GuildMessageEventHandler extends ListenerAdapter {
|
||||
if (event.getAuthor().getId().equals(jda.getSelfUser().getId())) return;
|
||||
|
||||
for (final Bot bot : Main.bots) {
|
||||
final String channelId = Main.discord.servers.get(bot.getServerString(true));
|
||||
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
|
||||
|
||||
if (!event.getChannel().getId().equals(channelId)) continue;
|
||||
if (channelId == null || !event.getChannel().getId().equals(channelId)) continue;
|
||||
|
||||
final Message messageObject = event.getMessage();
|
||||
final String messageString = messageObject.getContentDisplay();
|
||||
|
||||
@@ -14,6 +14,7 @@ import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
@@ -24,7 +25,9 @@ import net.kyori.adventure.text.event.HoverEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
|
||||
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -44,16 +47,16 @@ public class DiscordPlugin {
|
||||
public JDA jda;
|
||||
|
||||
public final Configuration.Discord options;
|
||||
public final Map<String, String> servers;
|
||||
|
||||
public final String prefix;
|
||||
public final Component messagePrefix;
|
||||
public final String serverId;
|
||||
public final String discordUrl;
|
||||
|
||||
public DiscordPlugin (final Configuration config) {
|
||||
this.options = config.discord;
|
||||
this.prefix = options.prefix;
|
||||
this.servers = options.servers;
|
||||
this.serverId = config.discord.serverId;
|
||||
this.discordUrl = config.discord.inviteLink;
|
||||
this.messagePrefix = Component.empty()
|
||||
.append(Component.text("ChomeNS ").color(NamedTextColor.YELLOW))
|
||||
@@ -89,7 +92,9 @@ public class DiscordPlugin {
|
||||
Main.EXECUTOR.scheduleAtFixedRate(this::onDiscordTick, 0, 50, TimeUnit.MILLISECONDS);
|
||||
|
||||
for (final Bot bot : Main.bots) {
|
||||
final String channelId = servers.get(bot.getServerString(true));
|
||||
final String channelId = findChannelId(bot.options.discordChannel);
|
||||
|
||||
if (channelId == null) continue;
|
||||
|
||||
logData.put(channelId, new LogData());
|
||||
|
||||
@@ -159,6 +164,16 @@ public class DiscordPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable String findChannelId (final String channelName) {
|
||||
final Guild guild = jda.getGuildById(serverId);
|
||||
if (guild == null) return null;
|
||||
|
||||
final List<TextChannel> channels = guild.getTextChannelsByName(channelName, true);
|
||||
if (channels.isEmpty()) return null;
|
||||
|
||||
return channels.getFirst().getId();
|
||||
}
|
||||
|
||||
// based from HBot (and modified quite a bit)
|
||||
private final Map<String, LogData> logData = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -211,7 +226,9 @@ public class DiscordPlugin {
|
||||
|
||||
public void onDiscordTick () {
|
||||
for (final Bot bot : Main.bots) {
|
||||
final String channelId = servers.get(bot.getServerString(true));
|
||||
final String channelId = findChannelId(bot.options.discordChannel);
|
||||
|
||||
if (channelId == null) continue;
|
||||
|
||||
final LogData data = logData.get(channelId);
|
||||
|
||||
|
||||
@@ -87,9 +87,10 @@ public class GrepLogPlugin {
|
||||
|
||||
if (matches == 0) throw new CommandException(Component.translatable("commands.greplog.error.no_matches_found"));
|
||||
|
||||
final String channelId = Main.discord.servers.get(bot.getServerString(true));
|
||||
final TextChannel logChannel = Main.discord.jda.getTextChannelById(channelId);
|
||||
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
|
||||
if (channelId == null) return;
|
||||
|
||||
final TextChannel logChannel = Main.discord.jda.getTextChannelById(channelId);
|
||||
if (logChannel == null) return;
|
||||
|
||||
logChannel
|
||||
|
||||
@@ -26,8 +26,6 @@ import java.util.concurrent.TimeUnit;
|
||||
public class IRCPlugin extends ListenerAdapter {
|
||||
private final Configuration.IRC ircConfig;
|
||||
|
||||
private final Map<String, String> servers;
|
||||
|
||||
public final Map<String, List<String>> messageQueue = new HashMap<>();
|
||||
|
||||
private PircBotX bot;
|
||||
@@ -35,8 +33,6 @@ public class IRCPlugin extends ListenerAdapter {
|
||||
public IRCPlugin (final Configuration config) {
|
||||
this.ircConfig = config.irc;
|
||||
|
||||
this.servers = ircConfig.servers;
|
||||
|
||||
if (!ircConfig.enabled) return;
|
||||
|
||||
final org.pircbotx.Configuration.Builder builder = new org.pircbotx.Configuration.Builder()
|
||||
@@ -51,8 +47,10 @@ public class IRCPlugin extends ListenerAdapter {
|
||||
if (!ircConfig.password.isEmpty())
|
||||
builder.addCapHandler(new SASLCapHandler(ircConfig.name, ircConfig.password, true));
|
||||
|
||||
for (final Map.Entry<String, String> entry : ircConfig.servers.entrySet())
|
||||
builder.addAutoJoinChannel(entry.getValue());
|
||||
for (final Bot bot : Main.bots) {
|
||||
final String channel = bot.options.ircChannel;
|
||||
if (channel != null) builder.addAutoJoinChannel(channel);
|
||||
}
|
||||
|
||||
final org.pircbotx.Configuration configuration = builder.buildConfiguration();
|
||||
|
||||
@@ -87,76 +85,61 @@ public class IRCPlugin extends ListenerAdapter {
|
||||
|
||||
@Override
|
||||
public void onMessage (final MessageEvent event) {
|
||||
Bot serverBot = null;
|
||||
for (final Bot bot : Main.bots) {
|
||||
if (!bot.options.ircChannel.equals(event.getChannel().getName())) continue;
|
||||
|
||||
String targetChannel = null;
|
||||
final String commandPrefix = ircConfig.prefix;
|
||||
|
||||
for (final Map.Entry<String, String> entry : servers.entrySet()) {
|
||||
if (entry.getValue().equals(event.getChannel().getName())) {
|
||||
serverBot = Main.bots.stream()
|
||||
.filter(eachBot -> entry.getKey().equals(eachBot.getServerString(true)))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
final User user = event.getUser();
|
||||
|
||||
targetChannel = entry.getValue();
|
||||
if (user == null) return;
|
||||
|
||||
break;
|
||||
final String name = user.getRealName().isBlank() ? user.getNick() : user.getRealName();
|
||||
final String message = event.getMessage();
|
||||
|
||||
if (message.startsWith(commandPrefix)) {
|
||||
final String noPrefix = message.substring(commandPrefix.length());
|
||||
|
||||
final IRCCommandContext context = new IRCCommandContext(bot, commandPrefix, name);
|
||||
|
||||
bot.commandHandler.executeCommand(noPrefix, context);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
final Component prefix = Component
|
||||
.text(event.getChannel().getName())
|
||||
.hoverEvent(
|
||||
HoverEvent.showText(
|
||||
Component
|
||||
.empty()
|
||||
.append(Component.text("on "))
|
||||
.append(Component.text(ircConfig.host))
|
||||
.append(Component.text(":"))
|
||||
.append(Component.text(ircConfig.port))
|
||||
.color(NamedTextColor.GRAY)
|
||||
)
|
||||
)
|
||||
.color(NamedTextColor.BLUE);
|
||||
|
||||
final Component username = Component
|
||||
.text(name)
|
||||
.hoverEvent(HoverEvent.showText(Component.text(event.getUser().getHostname()).color(NamedTextColor.RED)))
|
||||
.color(NamedTextColor.RED);
|
||||
|
||||
final Component messageComponent = Component
|
||||
.text(message)
|
||||
.color(NamedTextColor.GRAY);
|
||||
|
||||
final Component component = Component.translatable(
|
||||
"[%s] %s › %s",
|
||||
prefix,
|
||||
username,
|
||||
messageComponent
|
||||
).color(NamedTextColor.DARK_GRAY);
|
||||
|
||||
bot.chat.tellraw(component);
|
||||
}
|
||||
|
||||
if (serverBot == null) return;
|
||||
|
||||
final String commandPrefix = ircConfig.prefix;
|
||||
|
||||
final User user = event.getUser();
|
||||
|
||||
if (user == null) return;
|
||||
|
||||
final String name = user.getRealName().isBlank() ? user.getNick() : user.getRealName();
|
||||
final String message = event.getMessage();
|
||||
|
||||
if (message.startsWith(commandPrefix)) {
|
||||
final String noPrefix = message.substring(commandPrefix.length());
|
||||
|
||||
final IRCCommandContext context = new IRCCommandContext(serverBot, commandPrefix, name);
|
||||
|
||||
serverBot.commandHandler.executeCommand(noPrefix, context);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
final Component prefix = Component
|
||||
.text(targetChannel)
|
||||
.hoverEvent(
|
||||
HoverEvent.showText(
|
||||
Component
|
||||
.empty()
|
||||
.append(Component.text("on "))
|
||||
.append(Component.text(ircConfig.host))
|
||||
.append(Component.text(":"))
|
||||
.append(Component.text(ircConfig.port))
|
||||
.color(NamedTextColor.GRAY)
|
||||
)
|
||||
)
|
||||
.color(NamedTextColor.BLUE);
|
||||
|
||||
final Component username = Component
|
||||
.text(name)
|
||||
.hoverEvent(HoverEvent.showText(Component.text(event.getUser().getHostname()).color(NamedTextColor.RED)))
|
||||
.color(NamedTextColor.RED);
|
||||
|
||||
final Component messageComponent = Component
|
||||
.text(message)
|
||||
.color(NamedTextColor.GRAY);
|
||||
|
||||
final Component component = Component.translatable(
|
||||
"[%s] %s › %s",
|
||||
prefix,
|
||||
username,
|
||||
messageComponent
|
||||
).color(NamedTextColor.DARK_GRAY);
|
||||
|
||||
serverBot.chat.tellraw(component);
|
||||
}
|
||||
|
||||
private void systemMessageReceived (final Bot bot, final String ansi) {
|
||||
@@ -202,12 +185,6 @@ public class IRCPlugin extends ListenerAdapter {
|
||||
} catch (final Exception ignored) { }
|
||||
}
|
||||
|
||||
private void addMessageToQueue (final Bot bot, final String message) {
|
||||
final String channel = servers.get(bot.getServerString(true));
|
||||
|
||||
addMessageToQueue(channel, message);
|
||||
}
|
||||
|
||||
private void addMessageToQueue (final String channel, final String message) {
|
||||
final List<String> split = new ArrayList<>(Arrays.asList(message.split("\n")));
|
||||
|
||||
@@ -221,12 +198,7 @@ public class IRCPlugin extends ListenerAdapter {
|
||||
}
|
||||
|
||||
public void sendMessage (final Bot bot, final String message) {
|
||||
final String hostAndPort = bot.getServerString(true);
|
||||
|
||||
final String channel = servers.get(hostAndPort);
|
||||
|
||||
if (channel == null) return;
|
||||
|
||||
addMessageToQueue(channel, message);
|
||||
final String channel = bot.options.ircChannel;
|
||||
if (channel != null) addMessageToQueue(channel, message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user