refactor: improve how the bot loads plugins
This commit is contained in:
parent
2a6ef53c25
commit
a942cfd557
6 changed files with 45 additions and 61 deletions
|
@ -98,16 +98,9 @@ public class Bot {
|
||||||
this.bots = bots;
|
this.bots = bots;
|
||||||
|
|
||||||
this.config = config;
|
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.tick = new TickPlugin(this);
|
||||||
this.chat = new ChatPlugin(this);
|
this.chat = new ChatPlugin(this);
|
||||||
this.commandSpy = new CommandSpyPlugin(this);
|
this.commandSpy = new CommandSpyPlugin(this);
|
||||||
|
|
|
@ -2,14 +2,13 @@ package me.chayapak1.chomens_bot;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
import me.chayapak1.chomens_bot.plugins.ConsolePlugin;
|
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.ComponentUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.HttpUtilities;
|
import me.chayapak1.chomens_bot.util.HttpUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
|
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 net.kyori.adventure.text.Component;
|
||||||
import org.yaml.snakeyaml.LoaderOptions;
|
import org.yaml.snakeyaml.LoaderOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
@ -49,7 +48,7 @@ public class Main {
|
||||||
|
|
||||||
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
|
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 {
|
public static void main(String[] args) throws IOException {
|
||||||
final Path configPath = Path.of("config.yml");
|
final Path configPath = Path.of("config.yml");
|
||||||
|
@ -84,9 +83,6 @@ public class Main {
|
||||||
|
|
||||||
config = yaml.load(reader);
|
config = yaml.load(reader);
|
||||||
|
|
||||||
PersistentDataUtilities.init();
|
|
||||||
ComponentUtilities.stringify(Component.empty()); // best way to initialize the class 2024
|
|
||||||
|
|
||||||
executor.scheduleAtFixedRate(() -> {
|
executor.scheduleAtFixedRate(() -> {
|
||||||
try {
|
try {
|
||||||
checkInternet();
|
checkInternet();
|
||||||
|
@ -141,28 +137,25 @@ public class Main {
|
||||||
alreadyStarted = true;
|
alreadyStarted = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Configuration.BotOption[] botsOptions = config.bots;
|
final 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Configuration.BotOption botOption : botsOptions) {
|
for (Configuration.BotOption botOption : botsOptions) {
|
||||||
final Bot bot = new Bot(botOption, bots, config);
|
final Bot bot = new Bot(botOption, bots, config);
|
||||||
bots.add(bot);
|
bots.add(bot);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fard
|
// initialize util classes and plugins
|
||||||
new ConsolePlugin(bots, config, jda);
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
|
@ -233,7 +226,7 @@ public class Main {
|
||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jda != null) jda.shutdown();
|
if (discord.jda != null) discord.jda.shutdown();
|
||||||
|
|
||||||
if (discordEnabled) {
|
if (discordEnabled) {
|
||||||
for (int i = 0; i < 150; i++) {
|
for (int i = 0; i < 150; i++) {
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
package me.chayapak1.chomens_bot.plugins;
|
package me.chayapak1.chomens_bot.plugins;
|
||||||
|
|
||||||
import me.chayapak1.chomens_bot.Bot;
|
import me.chayapak1.chomens_bot.Bot;
|
||||||
import me.chayapak1.chomens_bot.Configuration;
|
|
||||||
import me.chayapak1.chomens_bot.Main;
|
import me.chayapak1.chomens_bot.Main;
|
||||||
import me.chayapak1.chomens_bot.command.Command;
|
import me.chayapak1.chomens_bot.command.Command;
|
||||||
import me.chayapak1.chomens_bot.command.ConsoleCommandContext;
|
import me.chayapak1.chomens_bot.command.ConsoleCommandContext;
|
||||||
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||||
import net.dv8tion.jda.api.JDA;
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import org.jline.reader.*;
|
import org.jline.reader.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ConsolePlugin implements Completer {
|
public class ConsolePlugin implements Completer {
|
||||||
|
@ -23,10 +20,8 @@ public class ConsolePlugin implements Completer {
|
||||||
|
|
||||||
public String prefix;
|
public String prefix;
|
||||||
|
|
||||||
private static final List<Listener> listeners = new ArrayList<>();
|
public ConsolePlugin () {
|
||||||
|
this.allBots = Main.bots;
|
||||||
public ConsolePlugin (List<Bot> allBots, Configuration config, JDA jda) {
|
|
||||||
this.allBots = allBots;
|
|
||||||
this.reader = LineReaderBuilder
|
this.reader = LineReaderBuilder
|
||||||
.builder()
|
.builder()
|
||||||
.completer(this)
|
.completer(this)
|
||||||
|
@ -38,13 +33,8 @@ public class ConsolePlugin implements Completer {
|
||||||
prefix = bot.config.consoleCommandPrefix;
|
prefix = bot.config.consoleCommandPrefix;
|
||||||
|
|
||||||
bot.console = this;
|
bot.console = this;
|
||||||
|
|
||||||
bot.logger = new LoggerPlugin(bot);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new DiscordPlugin(config, jda);
|
|
||||||
if (config.irc.enabled) new IRCPlugin(config);
|
|
||||||
|
|
||||||
final String prompt = "> ";
|
final String prompt = "> ";
|
||||||
|
|
||||||
Main.executorService.submit(() -> {
|
Main.executorService.submit(() -> {
|
||||||
|
@ -59,8 +49,6 @@ public class ConsolePlugin implements Completer {
|
||||||
handleLine(line);
|
handleLine(line);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for (Listener listener : listeners) { listener.ready(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 () {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,13 @@ import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.ComponentUtilities;
|
import me.chayapak1.chomens_bot.util.ComponentUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.*;
|
||||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
|
||||||
import net.dv8tion.jda.api.entities.Role;
|
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import net.dv8tion.jda.api.events.session.ShutdownEvent;
|
import net.dv8tion.jda.api.events.session.ShutdownEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
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.Component;
|
||||||
import net.kyori.adventure.text.JoinConfiguration;
|
import net.kyori.adventure.text.JoinConfiguration;
|
||||||
import net.kyori.adventure.text.event.ClickEvent;
|
import net.kyori.adventure.text.event.ClickEvent;
|
||||||
|
@ -35,7 +34,7 @@ import java.util.*;
|
||||||
// please ignore my ohio code
|
// please ignore my ohio code
|
||||||
// also this is one of the classes which has >100 lines or actually >400 LMAO
|
// also this is one of the classes which has >100 lines or actually >400 LMAO
|
||||||
public class DiscordPlugin {
|
public class DiscordPlugin {
|
||||||
public final JDA jda;
|
public JDA jda;
|
||||||
|
|
||||||
public final Map<String, String> servers;
|
public final Map<String, String> servers;
|
||||||
|
|
||||||
|
@ -49,11 +48,10 @@ public class DiscordPlugin {
|
||||||
|
|
||||||
public boolean shuttedDown = false;
|
public boolean shuttedDown = false;
|
||||||
|
|
||||||
public DiscordPlugin (Configuration config, JDA jda) {
|
public DiscordPlugin (Configuration config) {
|
||||||
final Configuration.Discord options = config.discord;
|
final Configuration.Discord options = config.discord;
|
||||||
this.prefix = options.prefix;
|
this.prefix = options.prefix;
|
||||||
this.servers = options.servers;
|
this.servers = options.servers;
|
||||||
this.jda = jda;
|
|
||||||
this.discordUrl = config.discord.inviteLink;
|
this.discordUrl = config.discord.inviteLink;
|
||||||
this.messagePrefix = Component.empty()
|
this.messagePrefix = Component.empty()
|
||||||
.append(Component.text("ChomeNS ").color(NamedTextColor.YELLOW))
|
.append(Component.text("ChomeNS ").color(NamedTextColor.YELLOW))
|
||||||
|
@ -65,8 +63,17 @@ public class DiscordPlugin {
|
||||||
)
|
)
|
||||||
.clickEvent(ClickEvent.openUrl(discordUrl));
|
.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;
|
if (jda == null) return;
|
||||||
|
|
||||||
|
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
|
||||||
|
|
||||||
for (Bot bot : Main.bots) {
|
for (Bot bot : Main.bots) {
|
||||||
final String channelId = servers.get(bot.host + ":" + bot.port);
|
final String channelId = servers.get(bot.host + ":" + bot.port);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package me.chayapak1.chomens_bot.plugins;
|
package me.chayapak1.chomens_bot.plugins;
|
||||||
|
|
||||||
import me.chayapak1.chomens_bot.Bot;
|
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.ComponentUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
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;
|
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
|
||||||
|
|
||||||
public class LoggerPlugin extends ChatPlugin.Listener {
|
public class LoggerPlugin extends ChatPlugin.Listener {
|
||||||
|
public static void init () {
|
||||||
|
for (Bot bot : Main.bots) new LoggerPlugin(bot);
|
||||||
|
}
|
||||||
|
|
||||||
private final Bot bot;
|
private final Bot bot;
|
||||||
|
|
||||||
private boolean addedListener = false;
|
private boolean addedListener = false;
|
||||||
|
@ -16,7 +21,7 @@ public class LoggerPlugin extends ChatPlugin.Listener {
|
||||||
|
|
||||||
private int totalConnects = 0;
|
private int totalConnects = 0;
|
||||||
|
|
||||||
public LoggerPlugin(Bot bot) {
|
public LoggerPlugin (Bot bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
|
|
||||||
bot.addListener(new Bot.Listener() {
|
bot.addListener(new Bot.Listener() {
|
||||||
|
@ -75,6 +80,8 @@ public class LoggerPlugin extends ChatPlugin.Listener {
|
||||||
log(message + string, true, false);
|
log(message + string, true, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bot.logger = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void log (String message) {
|
public void log (String message) {
|
||||||
|
|
|
@ -24,6 +24,8 @@ public class ComponentUtilities {
|
||||||
return component1.toString().equals(component2.toString());
|
return component1.toString().equals(component2.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void init () {}
|
||||||
|
|
||||||
// component parsing
|
// component parsing
|
||||||
public static final Map<String, String> language = loadJsonStringMap("language.json");
|
public static final Map<String, String> language = loadJsonStringMap("language.json");
|
||||||
private static final Map<String, String> voiceChatLanguage = loadJsonStringMap("voiceChatLanguage.json");
|
private static final Map<String, String> voiceChatLanguage = loadJsonStringMap("voiceChatLanguage.json");
|
||||||
|
|
Loading…
Reference in a new issue