refactor: improve how the bot loads plugins

This commit is contained in:
Chayapak 2024-11-17 16:37:05 +07:00
parent 2a6ef53c25
commit a942cfd557
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
6 changed files with 45 additions and 61 deletions

View file

@ -98,16 +98,9 @@ public class Bot {
this.bots = bots;
this.config = config;
ConsolePlugin.addListener(new ConsolePlugin.Listener() {
@Override
public void ready() {
Bot.this.ready();
}
});
}
public void ready () {
public void connect () {
this.tick = new TickPlugin(this);
this.chat = new ChatPlugin(this);
this.commandSpy = new CommandSpyPlugin(this);

View file

@ -2,14 +2,13 @@ package me.chayapak1.chomens_bot;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import me.chayapak1.chomens_bot.plugins.ConsolePlugin;
import me.chayapak1.chomens_bot.plugins.DiscordPlugin;
import me.chayapak1.chomens_bot.plugins.IRCPlugin;
import me.chayapak1.chomens_bot.plugins.LoggerPlugin;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.HttpUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.kyori.adventure.text.Component;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
@ -49,7 +48,7 @@ public class Main {
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
private static JDA jda = null;
private static DiscordPlugin discord;
public static void main(String[] args) throws IOException {
final Path configPath = Path.of("config.yml");
@ -84,9 +83,6 @@ public class Main {
config = yaml.load(reader);
PersistentDataUtilities.init();
ComponentUtilities.stringify(Component.empty()); // best way to initialize the class 2024
executor.scheduleAtFixedRate(() -> {
try {
checkInternet();
@ -141,28 +137,25 @@ public class Main {
alreadyStarted = true;
try {
Configuration.BotOption[] botsOptions = config.bots;
// idk if these should be here lol, but it is just the discord stuff
if (config.discord.enabled) {
JDABuilder builder = JDABuilder.createDefault(config.discord.token);
builder.enableIntents(GatewayIntent.MESSAGE_CONTENT);
try {
jda = builder.build();
jda.awaitReady();
} catch (InterruptedException ignored) {
System.exit(1);
}
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
}
final Configuration.BotOption[] botsOptions = config.bots;
for (Configuration.BotOption botOption : botsOptions) {
final Bot bot = new Bot(botOption, bots, config);
bots.add(bot);
}
// fard
new ConsolePlugin(bots, config, jda);
// initialize util classes and plugins
PersistentDataUtilities.init();
ComponentUtilities.init();
new ConsolePlugin();
LoggerPlugin.init();
if (config.discord.enabled) discord = new DiscordPlugin(config);
if (config.irc.enabled) new IRCPlugin(config);
LoggerUtilities.info("Initialized all bots. Now connecting");
for (Bot bot : bots) bot.connect();
} catch (Exception e) {
e.printStackTrace();
@ -233,7 +226,7 @@ public class Main {
} catch (Exception ignored) {}
}
if (jda != null) jda.shutdown();
if (discord.jda != null) discord.jda.shutdown();
if (discordEnabled) {
for (int i = 0; i < 150; i++) {

View file

@ -1,17 +1,14 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Configuration;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.ConsoleCommandContext;
import me.chayapak1.chomens_bot.util.ColorUtilities;
import net.dv8tion.jda.api.JDA;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jline.reader.*;
import java.util.ArrayList;
import java.util.List;
public class ConsolePlugin implements Completer {
@ -23,10 +20,8 @@ public class ConsolePlugin implements Completer {
public String prefix;
private static final List<Listener> listeners = new ArrayList<>();
public ConsolePlugin (List<Bot> allBots, Configuration config, JDA jda) {
this.allBots = allBots;
public ConsolePlugin () {
this.allBots = Main.bots;
this.reader = LineReaderBuilder
.builder()
.completer(this)
@ -38,13 +33,8 @@ public class ConsolePlugin implements Completer {
prefix = bot.config.consoleCommandPrefix;
bot.console = this;
bot.logger = new LoggerPlugin(bot);
}
new DiscordPlugin(config, jda);
if (config.irc.enabled) new IRCPlugin(config);
final String prompt = "> ";
Main.executorService.submit(() -> {
@ -59,8 +49,6 @@ public class ConsolePlugin implements Completer {
handleLine(line);
}
});
for (Listener listener : listeners) { listener.ready(); }
}
@Override
@ -112,10 +100,4 @@ public class ConsolePlugin implements Completer {
);
}
}
public static void addListener (Listener listener) { listeners.add(listener); }
public static class Listener {
public void ready () {}
}
}

View file

@ -9,14 +9,13 @@ import me.chayapak1.chomens_bot.util.ColorUtilities;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.session.ShutdownEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent;
@ -35,7 +34,7 @@ import java.util.*;
// please ignore my ohio code
// also this is one of the classes which has >100 lines or actually >400 LMAO
public class DiscordPlugin {
public final JDA jda;
public JDA jda;
public final Map<String, String> servers;
@ -49,11 +48,10 @@ public class DiscordPlugin {
public boolean shuttedDown = false;
public DiscordPlugin (Configuration config, JDA jda) {
public DiscordPlugin (Configuration config) {
final Configuration.Discord options = config.discord;
this.prefix = options.prefix;
this.servers = options.servers;
this.jda = jda;
this.discordUrl = config.discord.inviteLink;
this.messagePrefix = Component.empty()
.append(Component.text("ChomeNS ").color(NamedTextColor.YELLOW))
@ -65,8 +63,17 @@ public class DiscordPlugin {
)
.clickEvent(ClickEvent.openUrl(discordUrl));
final JDABuilder builder = JDABuilder.createDefault(config.discord.token);
builder.enableIntents(GatewayIntent.MESSAGE_CONTENT);
try {
jda = builder.build();
jda.awaitReady();
} catch (InterruptedException ignored) {}
if (jda == null) return;
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
for (Bot bot : Main.bots) {
final String channelId = servers.get(bot.host + ":" + bot.port);

View file

@ -1,6 +1,7 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.kyori.adventure.text.Component;
@ -8,6 +9,10 @@ import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
public class LoggerPlugin extends ChatPlugin.Listener {
public static void init () {
for (Bot bot : Main.bots) new LoggerPlugin(bot);
}
private final Bot bot;
private boolean addedListener = false;
@ -16,7 +21,7 @@ public class LoggerPlugin extends ChatPlugin.Listener {
private int totalConnects = 0;
public LoggerPlugin(Bot bot) {
public LoggerPlugin (Bot bot) {
this.bot = bot;
bot.addListener(new Bot.Listener() {
@ -75,6 +80,8 @@ public class LoggerPlugin extends ChatPlugin.Listener {
log(message + string, true, false);
}
});
bot.logger = this;
}
public void log (String message) {

View file

@ -24,6 +24,8 @@ public class ComponentUtilities {
return component1.toString().equals(component2.toString());
}
public static void init () {}
// component parsing
public static final Map<String, String> language = loadJsonStringMap("language.json");
private static final Map<String, String> voiceChatLanguage = loadJsonStringMap("voiceChatLanguage.json");