kiafumi/src/main/java/moe/oko/Kiafumi/Kiafumi.java

236 lines
7.9 KiB
Java
Raw Normal View History

2022-03-23 20:39:52 +00:00
package moe.oko.Kiafumi;
import moe.oko.Kiafumi.command.CommandClass;
2022-03-28 16:44:00 +00:00
import moe.oko.Kiafumi.command.DuckCommand;
import moe.oko.Kiafumi.command.PingCommand;
2022-03-28 15:56:45 +00:00
import moe.oko.Kiafumi.command.SettingCommand;
2022-03-24 04:10:50 +00:00
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.model.KiafumiDB;
2022-03-26 05:04:24 +00:00
import moe.oko.Kiafumi.model.ServerManager;
import moe.oko.Kiafumi.util.CommandInfo;
2022-03-24 04:10:50 +00:00
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
2022-03-23 20:39:52 +00:00
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.interactions.commands.Command;
2022-03-23 20:39:52 +00:00
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
2022-03-23 20:39:52 +00:00
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;
2022-03-24 04:10:50 +00:00
import java.io.InputStream;
import java.nio.file.Files;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.List;
2022-03-24 04:28:51 +00:00
/**
* Kiafumi Main Class
*/
2022-03-23 20:39:52 +00:00
public class Kiafumi {
private File CONFIG_FILE = new File("config.yml");
2022-03-24 04:10:50 +00:00
public YamlConfiguration yamlConfiguration = new YamlConfiguration();
2022-03-23 20:39:52 +00:00
public Logger logger = LoggerFactory.getLogger("Kiafumi");
public static String PREFIX;
2022-03-23 20:39:52 +00:00
public static Kiafumi instance;
2022-03-23 20:39:52 +00:00
public static JDA JDA;
public List<CommandClass> activeCommands;
2022-03-23 20:39:52 +00:00
public KiafumiConfig config;
2022-03-24 04:10:50 +00:00
public KiafumiDB database;
2022-03-26 05:04:24 +00:00
public ServerManager serverManager;
2022-03-24 04:28:51 +00:00
/**
* Main Class function, makes the classes work or some shit.
* @param args - Arguments for program start.
*/
public static void main(String[] args) {
try {
2022-03-24 04:10:50 +00:00
Kiafumi kia = new Kiafumi();
kia.start();
} catch (Exception ex) {
2022-03-23 20:39:52 +00:00
System.out.println("Failed to start Kiafumi Instance, check your Java installation.");
ex.printStackTrace();
}
}
2022-03-24 04:28:51 +00:00
/**
* Ran on program start. Anything in here can determine whether the program will start.
*/
public void start() {
//All commands to be loaded on startup!
activeCommands = new ArrayList<>();
activeCommands.add(new PingCommand());
2022-03-28 15:56:45 +00:00
activeCommands.add(new SettingCommand());
2022-03-28 16:44:00 +00:00
activeCommands.add(new DuckCommand());
instance = this;
2022-03-23 20:39:52 +00:00
logger.info("Starting Kiafumi.");
2022-03-24 04:28:51 +00:00
//Logger check
System.out.println("If no other messages are present, logger failed to instantiate.");
logger.info("Config load start...");
2022-03-24 04:28:51 +00:00
//Ensuring the configuration file is generated and/or exists.
if (!CONFIG_FILE.exists()) {
2022-03-24 04:10:50 +00:00
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;
}
}
2022-03-24 04:28:51 +00:00
//Try to load configuration into our local friendly configuration API.
try {
2022-03-24 04:10:50 +00:00
yamlConfiguration.load("config.yml");
} catch (FileNotFoundException e) {
e.printStackTrace();
2022-03-24 04:10:50 +00:00
logger.warn("File not found, must've failed to create...");
} catch (Exception e) {
logger.warn("Ensure all values are inputted properly.");
e.printStackTrace();
}
2022-03-24 04:28:51 +00:00
//Initializes our configuration helper & ensures it loads properly.
2022-03-23 20:39:52 +00:00
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);
}
2022-03-24 04:28:51 +00:00
//Registers the stop() function if the program is stopped.
logger.info("Registering shutdown hook.");
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
2022-03-24 04:28:51 +00:00
//We have the prefix as a static thing that can be referenced anywhere, this is for simplicity.
PREFIX = config.getPrefix();
2022-03-24 04:28:51 +00:00
//Initializes database and loads credentials.
2022-03-24 04:10:50 +00:00
database = config.createDb();
2022-03-26 05:04:24 +00:00
serverManager = new ServerManager();
//Makes our JDA instance.
startDiscord();
}
2022-03-24 04:28:51 +00:00
/**
* Ran on program shutdown.
*/
public void stop() {
2022-03-24 04:28:51 +00:00
}
2022-03-24 04:28:51 +00:00
/**
* Starts a JDA Instance.
*/
public void startDiscord() {
2022-03-23 20:39:52 +00:00
try {
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
GatewayIntent.GUILD_MESSAGES,
2022-03-24 04:10:50 +00:00
GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_PRESENCES)
2022-03-23 20:39:52 +00:00
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
2022-03-24 04:10:50 +00:00
.addEventListeners(new MainListener()).build().awaitReady();
2022-03-23 20:39:52 +00:00
} catch(Exception ex) {
error("Initialization broke...");
ex.printStackTrace();
2022-03-24 04:10:50 +00:00
return;
2022-03-23 20:39:52 +00:00
}
registerAllCommands();
info("Loaded " + activeCommands.size() + " commands.");
2022-03-24 04:10:50 +00:00
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();
}
/**
* Quick method to register commands in all servers.
*/
private void registerAllCommands() {
for(Guild guild : JDA.getGuilds()) {
registerForGuild(guild);
}
for(CommandClass cmd : activeCommands) {
JDA.addEventListener(cmd);
}
}
/**
* Registers all bot commands with the guild provided
* @param guild - guild to have commands provided to
*/
public void registerForGuild(Guild guild) {
info("Registering commands for " + guild.getId());
int i = 0;
for(CommandClass cmd : activeCommands) {
for(CommandInfo ci : cmd.getSlashCommandInfo()) {
i++;
if(ci.hasOptions()) {
CommandCreateAction cca = guild.upsertCommand(ci.getName(), ci.getDescription());
for(String name : ci.getOptions().keySet()) {
//Any intelligent id will rage about the option not being used, it's added to the action then executed later, DO not edit this (please).
cca.addOption(ci.getOptions().get(name), name, ci.getOptionDescriptions().get(name), ci.getOptionRequirements().get(name));
}
//Done
cca.queue();
continue;
}
//no options, just push direct cmd
guild.upsertCommand(ci.getName(), ci.getDescription()).queue();
}
}
info("Registered " + i + " commands.");
}
/*
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);
}
2022-03-24 04:10:50 +00:00
2022-03-24 04:28:51 +00:00
//Gets the active database.
2022-03-24 04:10:50 +00:00
public KiafumiDB getDatabase() { return database; }
2022-03-26 05:04:24 +00:00
//Gets active ServerManager
public ServerManager getServerManager() { return serverManager; }
}