Made sure the bot starts properly and generates the configs
This commit is contained in:
parent
42601a11c2
commit
ef7649d920
|
|
@ -12,6 +12,8 @@ import javax.security.auth.login.LoginException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static java.lang.System.exit;
|
||||||
|
|
||||||
public class AltitudeBot {
|
public class AltitudeBot {
|
||||||
|
|
||||||
private JDA jda;
|
private JDA jda;
|
||||||
|
|
@ -21,14 +23,20 @@ public class AltitudeBot {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
instance = this;
|
instance = new AltitudeBot();
|
||||||
|
instance.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void start() {
|
||||||
Logger.info("Starting bot...");
|
Logger.info("Starting bot...");
|
||||||
initConfigs();
|
initConfigs();
|
||||||
try {
|
try {
|
||||||
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
|
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
|
||||||
} catch (LoginException e) {
|
} catch (LoginException e) {
|
||||||
e.printStackTrace();
|
Logger.info("Unable to log in, shutting down (check token in settings.yml).");
|
||||||
|
exit(1);
|
||||||
|
Logger.exception(e);
|
||||||
}
|
}
|
||||||
initListeners();
|
initListeners();
|
||||||
//TODO init permissionManager
|
//TODO init permissionManager
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,9 @@ public class CommandPoll extends DiscordCommand {
|
||||||
new SubcommandData("results", "Get the results for a poll")
|
new SubcommandData("results", "Get the results for a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
|
.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));
|
.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 SubCommandAddButton(this),
|
||||||
new SubCommandClose(this),
|
new SubCommandClose(this),
|
||||||
new SubCommandEditDescription(this),
|
new SubCommandEditDescription(this),
|
||||||
|
|
@ -89,11 +91,7 @@ public class CommandPoll extends DiscordCommand {
|
||||||
|
|
||||||
SubCommand subCommand = subCommandMap.get(subcommandName);
|
SubCommand subCommand = subCommandMap.get(subcommandName);
|
||||||
if (subCommand == null) {
|
if (subCommand == null) {
|
||||||
event.replyEmbeds(new EmbedBuilder()
|
event.replyEmbeds(Util.invalidSubcommand(subcommandName))
|
||||||
.setTitle("Subcommand not found")
|
|
||||||
.setDescription("Unable to find subcommand `" + subcommandName + "`")
|
|
||||||
.setColor(Color.RED)
|
|
||||||
.build())
|
|
||||||
.setEphemeral(true)
|
.setEphemeral(true)
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,36 @@
|
||||||
package com.alttd.commandManager.commands.embed;
|
package com.alttd.commandManager.commands.embed;
|
||||||
|
|
||||||
import com.alttd.AltitudeBot;
|
import com.alttd.commandManager.CommandManager;
|
||||||
import com.alttd.commandManager.DiscordCommand;
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import com.alttd.commandManager.SubCommand;
|
||||||
import net.dv8tion.jda.api.entities.TextChannel;
|
import com.alttd.permissions.PermissionManager;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
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.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 net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
||||||
|
|
||||||
import java.util.List;
|
import java.awt.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class CommandEmbed extends DiscordCommand {
|
public class CommandEmbed extends DiscordCommand {
|
||||||
|
|
||||||
CommandEmbed() {
|
private final HashMap<String, SubCommand> subCommandMap = new HashMap<>();
|
||||||
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"));
|
|
||||||
|
|
||||||
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
|
@Override
|
||||||
|
|
@ -31,22 +39,40 @@ public class CommandEmbed extends DiscordCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String execute(String[] args, Member commandSource, TextChannel textChannel) {
|
public void execute(SlashCommandInteractionEvent event) {
|
||||||
return null;
|
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
|
@Override
|
||||||
public String execute(String[] args, User commandSource, TextChannel textChannel) {
|
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHelpMessage() {
|
public String getHelpMessage() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getAliases() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ abstract class AbstractConfig {
|
||||||
private ConfigurationNode config;
|
private ConfigurationNode config;
|
||||||
|
|
||||||
AbstractConfig(String filename) {
|
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) {
|
private void init(File file, String filename) {
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,41 @@
|
||||||
package com.alttd.config;
|
package com.alttd.config;
|
||||||
|
|
||||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
|
||||||
|
|
||||||
public class MessagesConfig extends AbstractConfig {
|
public class MessagesConfig extends AbstractConfig {
|
||||||
|
|
||||||
static MessagesConfig messagesConfig;
|
static MessagesConfig messagesConfig;
|
||||||
|
|
||||||
public MessagesConfig() {
|
public MessagesConfig() {
|
||||||
super("messages.yml");
|
super("messages.yml");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void reload() {
|
public static void reload() {
|
||||||
messagesConfig = new MessagesConfig();
|
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_HELP = "`/help`: Shows help menu";
|
||||||
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
||||||
private static void loadHelp() {
|
private static void loadHelp() {
|
||||||
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
||||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void loadPollHelp() {
|
private static void loadPollHelp() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String INVALID_COMMAND = "<command> is not a valid command.";
|
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_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 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 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>";
|
public static String INVALID_COMMAND_ARGUMENTS = "Some of the arguments in your command were invalid: <error>";
|
||||||
private static void loadInvalidCommands() {
|
private static void loadInvalidCommands() {
|
||||||
INVALID_COMMAND = messagesConfig.getString("messages.invalid_command", INVALID_COMMAND);
|
INVALID_COMMAND = messagesConfig.getString("messages.invalid_command", INVALID_COMMAND);
|
||||||
INVALID_COMMAND_ARGS = messagesConfig.getString("messages.invalid_command_args", INVALID_COMMAND_ARGS);
|
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);
|
GUILD_ONLY_MESSAGE = messagesConfig.getString("messages.guild_only_message", GUILD_ONLY_MESSAGE);
|
||||||
NO_PERMISSION_MESSAGE = messagesConfig.getString("messages.no_permission_message", NO_PERMISSION_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);
|
INVALID_COMMAND_ARGUMENTS = messagesConfig.getString("messages.invalid_command_arguments", INVALID_COMMAND_ARGUMENTS);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ public class SettingsConfig extends AbstractConfig {
|
||||||
public static void reload() {
|
public static void reload() {
|
||||||
settingsConfig = new SettingsConfig();
|
settingsConfig = new SettingsConfig();
|
||||||
|
|
||||||
settingsConfig.readConfig(SettingsConfig.class, null);
|
settingsConfig.readConfig(SettingsConfig.class, settingsConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String TOKEN = "token";
|
public static String TOKEN = "token";
|
||||||
|
|
|
||||||
|
|
@ -18,16 +18,11 @@ public class Logger { //TODO make this log to a file
|
||||||
private static final java.util.logging.Logger sql;
|
private static final java.util.logging.Logger sql;
|
||||||
|
|
||||||
static {
|
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())
|
if (!logDir.exists())
|
||||||
{
|
{
|
||||||
try {
|
if (!logDir.mkdir()) {
|
||||||
if (!logDir.createNewFile() || !logDir.mkdir()) {
|
System.out.println("UNABLE TO CREATE LOGGING DIRECTORY");
|
||||||
System.out.println("UNABLE TO CREATE LOGGING DIRECTORY");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -44,11 +39,11 @@ public class Logger { //TODO make this log to a file
|
||||||
String formattedTime = dateFormat.format(date.getTime());
|
String formattedTime = dateFormat.format(date.getTime());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
info.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
info.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
|
||||||
formattedTime + "info.log"));
|
formattedTime + "info.log"));
|
||||||
error.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
error.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
|
||||||
formattedTime + "error.log"));
|
formattedTime + "error.log"));
|
||||||
sql.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
sql.addHandler(new FileHandler(logDir.getAbsolutePath() + File.separator +
|
||||||
formattedTime + "sql.log"));
|
formattedTime + "sql.log"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import net.dv8tion.jda.api.requests.RestAction;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
@ -64,6 +65,15 @@ public class Util {
|
||||||
return embedBuilder.build();
|
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) {
|
public static void registerCommand(CommandManager commandManager, JDA jda, SlashCommandData slashCommandData, String commandName) {
|
||||||
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
|
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
|
||||||
switch (info.getScope()) {
|
switch (info.getScope()) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user