208 lines
6.6 KiB
Java
208 lines
6.6 KiB
Java
package moe.oko.Kiafumi;
|
|
|
|
import moe.oko.Kiafumi.command.CommandClass;
|
|
import moe.oko.Kiafumi.command.PingCommand;
|
|
import moe.oko.Kiafumi.listener.MainListener;
|
|
import moe.oko.Kiafumi.model.KiafumiDB;
|
|
import moe.oko.Kiafumi.model.ServerManager;
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
|
import net.dv8tion.jda.api.JDA;
|
|
import net.dv8tion.jda.api.JDABuilder;
|
|
import net.dv8tion.jda.api.OnlineStatus;
|
|
import net.dv8tion.jda.api.entities.Activity;
|
|
import net.dv8tion.jda.api.entities.Guild;
|
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
|
import net.dv8tion.jda.api.utils.ChunkingFilter;
|
|
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
|
import org.simpleyaml.configuration.file.YamlConfiguration;
|
|
import org.simpleyaml.exceptions.InvalidConfigurationException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.awt.*;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.time.temporal.TemporalAccessor;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Kiafumi Main Class
|
|
*/
|
|
public class Kiafumi {
|
|
|
|
private File CONFIG_FILE = new File("config.yml");
|
|
|
|
public YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
|
|
|
public Logger logger = LoggerFactory.getLogger("Kiafumi");
|
|
|
|
public static String PREFIX;
|
|
|
|
public static Kiafumi instance;
|
|
|
|
public static JDA JDA;
|
|
|
|
public List<CommandClass> activeCommands;
|
|
|
|
public KiafumiConfig config;
|
|
|
|
public KiafumiDB database;
|
|
|
|
public ServerManager serverManager;
|
|
|
|
/**
|
|
* Main Class function, makes the classes work or some shit.
|
|
* @param args - Arguments for program start.
|
|
*/
|
|
public static void main(String[] args) {
|
|
try {
|
|
Kiafumi kia = new Kiafumi();
|
|
kia.start();
|
|
} catch (Exception ex) {
|
|
System.out.println("Failed to start Kiafumi Instance, check your Java installation.");
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ran on program start. Anything in here can determine whether the program will start.
|
|
*/
|
|
public void start() {
|
|
|
|
activeCommands = new ArrayList<>();
|
|
activeCommands.add(new PingCommand());
|
|
|
|
instance = this;
|
|
|
|
logger.info("Starting Kiafumi.");
|
|
//Logger check
|
|
System.out.println("If no other messages are present, logger failed to instantiate.");
|
|
|
|
logger.info("Config load start...");
|
|
|
|
//Ensuring the configuration file is generated and/or exists.
|
|
if (!CONFIG_FILE.exists()) {
|
|
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.yml")) {
|
|
//Save the default cfg
|
|
Files.copy(is, CONFIG_FILE.toPath());
|
|
} catch (Exception ex) {
|
|
logger.warn("Failed to create the configuration file. Stopping. (" + CONFIG_FILE.getAbsolutePath() + ")");
|
|
ex.printStackTrace();
|
|
return;
|
|
}
|
|
}
|
|
|
|
//Try to load configuration into our local friendly configuration API.
|
|
try {
|
|
yamlConfiguration.load("config.yml");
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
logger.warn("File not found, must've failed to create...");
|
|
} catch (Exception e) {
|
|
logger.warn("Ensure all values are inputted properly.");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
//Initializes our configuration helper & ensures it loads properly.
|
|
config = new KiafumiConfig(yamlConfiguration);
|
|
if(config.load()) {
|
|
logger.info("Config loaded, proceeding.");
|
|
} else {
|
|
logger.error("Failed to load configuration. Stopping process.");
|
|
Runtime.getRuntime().exit(0);
|
|
}
|
|
|
|
//Registers the stop() function if the program is stopped.
|
|
logger.info("Registering shutdown hook.");
|
|
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
|
|
|
|
//We have the prefix as a static thing that can be referenced anywhere, this is for simplicity.
|
|
PREFIX = config.getPrefix();
|
|
|
|
//Initializes database and loads credentials.
|
|
database = config.createDb();
|
|
|
|
serverManager = new ServerManager();
|
|
|
|
//Makes our JDA instance.
|
|
startDiscord();
|
|
}
|
|
|
|
/**
|
|
* Ran on program shutdown.
|
|
*/
|
|
public void stop() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Starts a JDA Instance.
|
|
*/
|
|
public void startDiscord() {
|
|
try {
|
|
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
|
|
GatewayIntent.GUILD_MESSAGES,
|
|
GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_PRESENCES)
|
|
.setChunkingFilter(ChunkingFilter.ALL)
|
|
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
|
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
|
|
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
|
|
.addEventListeners(new MainListener()).build().awaitReady();
|
|
} catch(Exception ex) {
|
|
error("Initialization broke...");
|
|
ex.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
registerAllCommands();
|
|
info("Loaded " + activeCommands.size() + " commands.");
|
|
|
|
EmbedBuilder eb = new EmbedBuilder().setTitle("Kiafumi Online")
|
|
.setFooter("Created by Oko, Laika, and Tiddy").setColor(new Color(0x6271c1));
|
|
JDA.getTextChannelById(config.getLogChannel()).sendMessageEmbeds(eb.build()).queue();
|
|
}
|
|
|
|
private void registerAllCommands() {
|
|
for(Guild guild : JDA.getGuilds()) {
|
|
registerForGuild(guild);
|
|
}
|
|
|
|
for(CommandClass cmd : activeCommands) {
|
|
JDA.addEventListener(cmd);
|
|
}
|
|
}
|
|
|
|
public void registerForGuild(Guild guild) {
|
|
info("Registering commands for " + guild.getId());
|
|
for(CommandClass cmd : activeCommands) {
|
|
for(String name : cmd.getSlashCommandInfo().keySet()) {
|
|
guild.upsertCommand(name, cmd.getSlashCommandInfo().get(name)).queue();
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
Static logger info reference.
|
|
*/
|
|
public static void info(String str) {
|
|
instance.logger.info(str);
|
|
}
|
|
|
|
/*
|
|
Static Logger Error reference.
|
|
*/
|
|
public static void error(String str) {
|
|
instance.logger.error(str);
|
|
}
|
|
|
|
//Gets the active database.
|
|
public KiafumiDB getDatabase() { return database; }
|
|
|
|
//Gets active ServerManager
|
|
public ServerManager getServerManager() { return serverManager; }
|
|
}
|