did a lot of work on poll command
This commit is contained in:
@@ -1,26 +1,89 @@
|
||||
package com.alttd.util;
|
||||
|
||||
import com.alttd.AltitudeBot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Logger { //TODO make this log to a file
|
||||
|
||||
private static final java.util.logging.Logger logger;
|
||||
private static final java.util.logging.Logger info;
|
||||
private static final java.util.logging.Logger error;
|
||||
private static final java.util.logging.Logger sql;
|
||||
|
||||
static {
|
||||
logger = java.util.logging.Logger.getLogger("DiscordBot");
|
||||
File logDir = new File(AltitudeBot.getInstance().getDataFolder() + File.pathSeparator + "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();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
info = java.util.logging.Logger.getLogger("info");
|
||||
error = java.util.logging.Logger.getLogger("error");
|
||||
sql = java.util.logging.Logger.getLogger("sql");
|
||||
info.setLevel(Level.ALL);
|
||||
error.setLevel(Level.ALL);
|
||||
sql.setLevel(Level.ALL);
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd");
|
||||
Date date = new Date();
|
||||
String formattedTime = dateFormat.format(date.getTime());
|
||||
|
||||
try {
|
||||
info.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
||||
formattedTime + "info.log"));
|
||||
error.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
||||
formattedTime + "error.log"));
|
||||
sql.addHandler(new FileHandler(logDir.getAbsolutePath() + File.pathSeparator +
|
||||
formattedTime + "sql.log"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void info(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.info(message);
|
||||
info.info(message);
|
||||
}
|
||||
|
||||
public static void warning(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.warning(message);
|
||||
error.warning(message);
|
||||
}
|
||||
|
||||
public static void severe(String message, String... replacements) {
|
||||
message = replace(message, replacements);
|
||||
logger.severe(message);
|
||||
error.severe(message);
|
||||
}
|
||||
|
||||
public static void sql(String message) {
|
||||
sql.info(message);
|
||||
}
|
||||
|
||||
public static void sql(SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
sql.info("SQLState: " + exception.getSQLState() + "\n");
|
||||
sql.severe("Error:\n" + exception.getMessage());
|
||||
}
|
||||
|
||||
public static void exception(Exception exception) {
|
||||
exception.printStackTrace();
|
||||
error.severe("Error:\n" + exception.getMessage());
|
||||
}
|
||||
|
||||
private static String replace(String message, String... replacements) {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.alttd.util;
|
||||
|
||||
import net.dv8tion.jda.api.entities.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
|
||||
public class OptionMappingParsing {
|
||||
|
||||
public static String getString(String optionName, SlashCommandInteractionEvent event, String commandName) {
|
||||
OptionMapping optionMappingString = event.getInteraction().getOption(optionName);
|
||||
String text = optionMappingString == null ? null : optionMappingString.getAsString();
|
||||
if (text == null)
|
||||
event.replyEmbeds(Util.invalidCommand(commandName, "Not a valid string or didn't give input for " + optionName, event.getInteraction())).setEphemeral(true).queue();
|
||||
return text;
|
||||
}
|
||||
|
||||
public static GuildMessageChannel getGuildChannel(String optionName, SlashCommandInteractionEvent event, String commandName) {
|
||||
OptionMapping optionMappingChannel = event.getInteraction().getOption(optionName);
|
||||
GuildMessageChannel messageChannel = optionMappingChannel == null ? null : optionMappingChannel.getAsMessageChannel();
|
||||
if (messageChannel == null)
|
||||
event.replyEmbeds(Util.invalidCommand(commandName, "Not a valid text channel or didn't give input for " + optionName, event.getInteraction())).setEphemeral(true).queue();
|
||||
return messageChannel;
|
||||
}
|
||||
|
||||
public static Long getLong(String optionName, SlashCommandInteractionEvent event, String commandName) {
|
||||
OptionMapping optionMappingLong = event.getInteraction().getOption(optionName);
|
||||
if (optionMappingLong == null) {
|
||||
event.replyEmbeds(Util.invalidCommand(commandName, "Not a valid number or didn't give input for " + optionName, event.getInteraction())).setEphemeral(true).queue();
|
||||
return null;
|
||||
}
|
||||
return optionMappingLong.getAsLong();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,17 @@
|
||||
package com.alttd.util;
|
||||
|
||||
import com.alttd.config.MessagesConfig;
|
||||
import com.alttd.templates.Parser;
|
||||
import com.alttd.templates.Template;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -12,4 +21,37 @@ public class Util {
|
||||
.map(Role::getIdLong)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static void handleFailure(Throwable failure) {
|
||||
Logger.warning(failure.getMessage());
|
||||
}
|
||||
|
||||
public static MessageEmbed guildOnlyCommand(String commandName) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle("Guild Only")
|
||||
.setDescription(Parser.parse(MessagesConfig.GUILD_ONLY_MESSAGE, Template.of("command", commandName)))
|
||||
.setColor(Color.RED)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static MessageEmbed noPermission(String commandName) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle("No Permission")
|
||||
.setDescription(Parser.parse(MessagesConfig.NO_PERMISSION_MESSAGE, Template.of("command", commandName)))
|
||||
.setColor(Color.RED)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static MessageEmbed invalidCommand(String commandName, String error, SlashCommandInteraction interaction) {
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder()
|
||||
.setTitle("Invalid Command")
|
||||
.setDescription(Parser.parse(MessagesConfig.INVALID_COMMAND_ARGUMENTS,
|
||||
Template.of("command", commandName),
|
||||
Template.of("error", error)))
|
||||
.setColor(Color.RED);
|
||||
for (OptionMapping option : interaction.getOptions()) {
|
||||
embedBuilder.addField(option.getName(), option.getAsString(), false);
|
||||
}
|
||||
return embedBuilder.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user