Made sure the bot starts properly and generates the configs

This commit is contained in:
Stijn 2022-04-08 19:51:12 +02:00
parent 42601a11c2
commit ef7649d920
8 changed files with 90 additions and 52 deletions

View File

@ -12,6 +12,8 @@ import javax.security.auth.login.LoginException;
import java.io.File;
import java.net.URISyntaxException;
import static java.lang.System.exit;
public class AltitudeBot {
private JDA jda;
@ -21,14 +23,20 @@ public class AltitudeBot {
return instance;
}
public void main(String[] args) {
instance = this;
public static void main(String[] args) {
instance = new AltitudeBot();
instance.start();
}
private void start() {
Logger.info("Starting bot...");
initConfigs();
try {
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
} catch (LoginException e) {
e.printStackTrace();
Logger.info("Unable to log in, shutting down (check token in settings.yml).");
exit(1);
Logger.exception(e);
}
initListeners();
//TODO init permissionManager

View File

@ -54,7 +54,9 @@ public class CommandPoll extends DiscordCommand {
new SubcommandData("results", "Get the results for a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you want the results for", true));
Util.registerSubcommands(subCommandMap, new SubCommandAdd(this),
slashCommandData.setDefaultEnabled(true);
Util.registerSubcommands(subCommandMap,
new SubCommandAdd(this),
new SubCommandAddButton(this),
new SubCommandClose(this),
new SubCommandEditDescription(this),
@ -89,11 +91,7 @@ public class CommandPoll extends DiscordCommand {
SubCommand subCommand = subCommandMap.get(subcommandName);
if (subCommand == null) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Subcommand not found")
.setDescription("Unable to find subcommand `" + subcommandName + "`")
.setColor(Color.RED)
.build())
event.replyEmbeds(Util.invalidSubcommand(subcommandName))
.setEphemeral(true)
.queue();
return;

View File

@ -1,28 +1,36 @@
package com.alttd.commandManager.commands.embed;
import com.alttd.AltitudeBot;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import com.alttd.commandManager.SubCommand;
import com.alttd.permissions.PermissionManager;
import com.alttd.util.Logger;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import java.util.List;
import java.awt.*;
import java.util.HashMap;
public class CommandEmbed extends DiscordCommand {
CommandEmbed() {
CommandData commandData = new CommandData(getName(), "A command to create embeds for polls etc");
commandData.setDefaultEnabled(false);
commandData.addSubcommands(new SubcommandData("add", "Add a new embed to a channel")
.addOption(OptionType.CHANNEL, "target",
"The channel to send the new embed in", true)
.addOption(OptionType.STRING, "type", "Either `poll` or `default`")
.addOption(OptionType.STRING, "text", "The description for your poll, max 1000 characters"));
private final HashMap<String, SubCommand> subCommandMap = new HashMap<>();
AltitudeBot.getInstance().getJDA().upsertCommand(commandData).queue();
public CommandEmbed(JDA jda, CommandManager commandManager) {
SlashCommandData slashCommandData = Commands.slash(getName(), "A command to create embeds for polls etc")
.addSubcommands(
new SubcommandData("add", "Add a new embed to a channel")
.addOption(OptionType.CHANNEL, "target", "The channel to send the new embed in", true)
.addOption(OptionType.STRING, "type", "Either `poll` or `default`")
.addOption(OptionType.STRING, "text", "The description for your poll, max 1000 characters"));
slashCommandData.setDefaultEnabled(true);
Util.registerCommand(commandManager, jda, slashCommandData, getName());
}
@Override
@ -31,22 +39,40 @@ public class CommandEmbed extends DiscordCommand {
}
@Override
public String execute(String[] args, Member commandSource, TextChannel textChannel) {
return null;
public void execute(SlashCommandInteractionEvent event) {
if (event.getGuild() == null || event.getMember() == null) {
event.replyEmbeds(Util.guildOnlyCommand(getName())).setEphemeral(true).queue();
return;
}
if (PermissionManager.getInstance().hasPermission(event.getTextChannel(), event.getIdLong(), Util.getGroupIds(event.getMember()), getPermission())) {
event.replyEmbeds(Util.noPermission(getName())).setEphemeral(true).queue();
return;
}
String subcommandName = event.getInteraction().getSubcommandName();
if (subcommandName == null) {
Logger.severe("No subcommand found for %", getName());
return;
}
SubCommand subCommand = subCommandMap.get(subcommandName);
if (subCommand == null) {
event.replyEmbeds(Util.invalidSubcommand(subcommandName))
.setEphemeral(true)
.queue();
return;
}
subCommand.execute(event);
}
@Override
public String execute(String[] args, User commandSource, TextChannel textChannel) {
return null;
public void suggest(CommandAutoCompleteInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
@Override
public List<String> getAliases() {
return null;
}
}

View File

@ -30,7 +30,7 @@ abstract class AbstractConfig {
private ConfigurationNode config;
AbstractConfig(String filename) {
init(new File(AltitudeBot.getInstance().getDataFolder(), filename), filename);
init(new File(new File(AltitudeBot.getInstance().getDataFolder()).getParentFile(), filename), filename);
}
private void init(File file, String filename) {

View File

@ -1,40 +1,41 @@
package com.alttd.config;
import net.dv8tion.jda.api.entities.MessageEmbed;
public class MessagesConfig extends AbstractConfig {
static MessagesConfig messagesConfig;
public MessagesConfig() {
super("messages.yml");
}
public static void reload() {
messagesConfig = new MessagesConfig();
messagesConfig.readConfig(MessagesConfig.class, null);
messagesConfig.readConfig(MessagesConfig.class, messagesConfig);
}
public static String HELP_HELP = "`/help`: Shows help menu";
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
private static void loadHelp() {
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
}
private static void loadPollHelp() {
}
public static String INVALID_COMMAND = "<command> is not a valid command.";
public static String INVALID_COMMAND_ARGS = "`<args>` is/are not valid argument(s) for `<command>`.\nFor more info see <prefix>help <command>";
public static String INVALID_SUBCOMMAND = "Subcommand not found";
public static String INVALID_SUBCOMMAND_DESC = "Unable to find subcommand `<subcommand>`";
public static String GUILD_ONLY_MESSAGE = "Sorry, <command> can only be executed from within a guild.";
public static String NO_PERMISSION_MESSAGE = "Sorry, <command> can only be executed from within a guild.";
public static String INVALID_COMMAND_ARGUMENTS = "Some of the arguments in your command were invalid: <error>";
private static void loadInvalidCommands() {
INVALID_COMMAND = messagesConfig.getString("messages.invalid_command", INVALID_COMMAND);
INVALID_COMMAND_ARGS = messagesConfig.getString("messages.invalid_command_args", INVALID_COMMAND_ARGS);
INVALID_SUBCOMMAND = messagesConfig.getString("messages.invalid_subcommand", INVALID_SUBCOMMAND);
GUILD_ONLY_MESSAGE = messagesConfig.getString("messages.guild_only_message", GUILD_ONLY_MESSAGE);
NO_PERMISSION_MESSAGE = messagesConfig.getString("messages.no_permission_message", NO_PERMISSION_MESSAGE);
INVALID_COMMAND_ARGUMENTS = messagesConfig.getString("messages.invalid_command_arguments", INVALID_COMMAND_ARGUMENTS);

View File

@ -11,7 +11,7 @@ public class SettingsConfig extends AbstractConfig {
public static void reload() {
settingsConfig = new SettingsConfig();
settingsConfig.readConfig(SettingsConfig.class, null);
settingsConfig.readConfig(SettingsConfig.class, settingsConfig);
}
public static String TOKEN = "token";

View File

@ -18,16 +18,11 @@ public class Logger { //TODO make this log to a file
private static final java.util.logging.Logger sql;
static {
File logDir = new File(AltitudeBot.getInstance().getDataFolder() + File.pathSeparator + "logs");
File logDir = new File(new File(AltitudeBot.getInstance().getDataFolder()).getParent() + File.separator + "logs");
if (!logDir.exists())
{
try {
if (!logDir.createNewFile() || !logDir.mkdir()) {
System.out.println("UNABLE TO CREATE LOGGING DIRECTORY");
System.exit(1);
}
} catch (IOException e) {
e.printStackTrace();
if (!logDir.mkdir()) {
System.out.println("UNABLE TO CREATE LOGGING DIRECTORY");
System.exit(1);
}
}
@ -44,11 +39,11 @@ public class Logger { //TODO make this log to a file
String formattedTime = dateFormat.format(date.getTime());
try {
info.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
info.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
formattedTime + "info.log"));
error.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
error.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
formattedTime + "error.log"));
sql.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
sql.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
formattedTime + "sql.log"));
} catch (IOException e) {
e.printStackTrace();

View File

@ -18,6 +18,7 @@ import net.dv8tion.jda.api.requests.RestAction;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@ -64,6 +65,15 @@ public class Util {
return embedBuilder.build();
}
public static MessageEmbed invalidSubcommand(String subcommandName) {
return new EmbedBuilder()
.setTitle(MessagesConfig.INVALID_SUBCOMMAND)
.setDescription(Parser.parse(MessagesConfig.INVALID_SUBCOMMAND_DESC,
Template.of("subcommand", subcommandName)))
.setColor(Color.RED)
.build();
}
public static void registerCommand(CommandManager commandManager, JDA jda, SlashCommandData slashCommandData, String commandName) {
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
switch (info.getScope()) {