TotalFreedomMod/discord/src/main/java/me/totalfreedom/discord/TFD4J.java

125 lines
4.1 KiB
Java

package me.totalfreedom.discord;
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
import me.totalfreedom.discord.command.HelpCommand;
import me.totalfreedom.discord.command.ListCommand;
import me.totalfreedom.discord.command.TPSCommand;
import me.totalfreedom.discord.handling.CommandHandler;
import me.totalfreedom.discord.listener.*;
import me.totalfreedom.discord.react.ReactiveBukkitScheduler;
import me.totalfreedom.totalfreedommod.api.Context;
import me.totalfreedom.totalfreedommod.api.TFD4JCommons;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.slf4j.Logger;
/**
* @author Paldiu
* @author videogamesm12
* @since 2023-03-16
*/
public class TFD4J extends JavaPlugin
{
private final Logger slf4j = this.getSLF4JLogger();
private Bot bot;
private MinecraftListener mc;
private AdminChatListener ac;
private PrivateMessageListener pm;
private ReactionListener rl;
private TFD4JCommons tfd4jcommons;
private BukkitNative bn;
private CommandHandler ch;
private ReactiveBukkitScheduler rbs;
@Override
public void onEnable()
{
slf4j().info("Hello from TFD4J! Initializing our API implementation...");
this.tfd4jcommons = new TFD4JCommonsImpl(this);
slf4j().info("API successfully initialized! Initializing our bot...");
this.bot = new Bot();
bot.initialize();
slf4j().info("Bot successfully initialized! Abstracting the BukkitScheduler...");
this.rbs = new ReactiveBukkitScheduler(this);
String string = String.format("Scheduler successfully wrapped into %s! Registering the Bukkit Native listener...", ReactiveBukkitScheduler.class.getName()); // Fixes SonarLint's "Invoke methods only conditionally."
slf4j().info(string);
this.bn = new BukkitNative(this);
Bukkit.getPluginManager().registerEvents(this.bn, this);
slf4j().info("Bukkit Native listener successfully registered! Registering the Discord4J Listeners...");
this.mc = new MinecraftListener(this);
this.ac = new AdminChatListener(this);
this.pm = new PrivateMessageListener(this);
this.rl = new ReactionListener(this);
pm.privateMessageReceived();
rl.onReactionAdd();
mc.minecraftChatBound();
ac.adminChatBound();
slf4j().info("Discord4J listeners successfully registered! Registering the Command Handler...");
this.ch = new CommandHandler(bot.getClient().getRestClient());
slf4j().info("Command Handler successfully registered! Registering commands...");
this.ch.registerCommand(new HelpCommand());
this.ch.registerCommand(new ListCommand());
this.ch.registerCommand(new TPSCommand());
this.getBot().getClient().on(ChatInputInteractionEvent.class, ch::handle)
.subscribeOn(getReactiveBukkitScheduler())
.subscribe();
slf4j().info("Commands successfully registered! Providing context to TFM...");
Context<TFD4JCommons> context = new Context<>(tfd4jcommons);
bot.getTFM().getCommons().ag.setDiscordContext(context);
bot.getTFM().getCommons().registerDiscord();
slf4j().info("Context provided! TFD4J is now ready to go!");
}
@Override
public void onDisable()
{
slf4j().info("Disconnecting the Discord bot...");
bot.getClient()
.onDisconnect()
.doOnError(th -> slf4j().error("Error disconnecting the bot!", th))
.doOnSuccess(v -> slf4j().info("Bot disconnected!"))
.subscribe();
slf4j().info("Goodbye from TFD4J!");
}
public Logger slf4j()
{
return slf4j;
}
public Bot getBot()
{
return bot;
}
public TFD4JCommons getImpl()
{
return tfd4jcommons;
}
public MinecraftListener getMinecraftListener()
{
return mc;
}
public AdminChatListener getAdminChatListener()
{
return ac;
}
public ReactiveBukkitScheduler getReactiveBukkitScheduler()
{
return rbs;
}
}