HidekoBot/src/main/java/wtf/beatrice/hidekobot/Configuration.java

217 lines
6.3 KiB
Java
Raw Normal View History

2022-08-26 00:13:06 +02:00
package wtf.beatrice.hidekobot;
2022-11-21 12:19:35 +01:00
import net.dv8tion.jda.api.interactions.commands.Command;
import org.jetbrains.annotations.Nullable;
import wtf.beatrice.hidekobot.database.DatabaseManager;
import wtf.beatrice.hidekobot.listeners.MessageLogger;
2022-11-21 12:19:35 +01:00
import java.awt.*;
import java.time.Duration;
2022-11-21 12:19:35 +01:00
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
2022-11-21 12:19:35 +01:00
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
2022-08-26 00:13:06 +02:00
public class Configuration
{
private static DatabaseManager dbManager = null;
2022-08-26 00:13:06 +02:00
private static boolean verbose = false;
private static MessageLogger verbosityLogger;
2022-08-26 00:13:06 +02:00
// todo: allow people to set their own user id
private static final long botOwnerId = 979809420714332260L;
private final static String expiryTimestampFormat = "yy/MM/dd HH:mm:ss";
2022-11-21 11:15:23 +01:00
// note: discord sets interactions' expiry time to 15 minutes by default, so we can't go higher than that.
private final static long expiryTimeSeconds = 60L;
2022-11-21 12:19:35 +01:00
// used to count eg. uptime
private static LocalDateTime startupTime;
2022-11-21 14:55:01 +01:00
private static final String botVersion = "0.1.2-slash"; // we should probably find a way to make this consistent with Maven
2022-11-21 12:19:35 +01:00
private static final String botName = "HidekoBot";
private static final Color botColor = Color.PINK;
private static final List<Command> registeredCommands = new ArrayList<>();
2022-11-20 16:07:04 +01:00
private final static String defaultInviteLink =
"https://discord.com/api/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8";
2022-11-20 18:11:00 +01:00
private static String botApplicationId = "";
2022-11-20 16:07:04 +01:00
// discord api returns a broken image if you don't use specific sizes (powers of 2), so we limit it to these
private static final int[] supportedAvatarResolutions = { 16, 32, 64, 128, 256, 512, 1024 };
/**
* Get an array of all the Discord-supported avatar resolutions.
* Discord's API returns a broken image if you don't use specific sizes (powers of 2).
*
* @return array of supported resolutions.
*/
public static int[] getSupportedAvatarResolutions() { return supportedAvatarResolutions; }
2022-08-26 00:13:06 +02:00
/**
* Checks if the bot has been started with the verbose argument.
*
* @return a boolean which is true if the bot is in verbose-mode
*/
2022-08-26 00:13:06 +02:00
public static boolean isVerbose() { return verbose; }
/**
* Set the bot's verbosity status at runtime.
* This also registers or unregisters the message-logger listener.
*
* @param v the verbosity boolean value
*/
public static void setVerbose(boolean v)
{
verbose = v;
if(v)
{
if(verbosityLogger == null)
{
verbosityLogger = new MessageLogger();
}
2022-08-26 00:13:06 +02:00
HidekoBot.getAPI().addEventListener(verbosityLogger);
} else {
if(verbosityLogger != null)
{
HidekoBot.getAPI().removeEventListener(verbosityLogger);
verbosityLogger = null;
}
}
}
/**
2022-11-20 06:07:25 +01:00
* Get the bot owner's profile id.
*
2022-11-20 06:07:25 +01:00
* @return a long of the account's id
*/
public static long getBotOwnerId() { return botOwnerId; }
2022-11-20 18:11:00 +01:00
/**
* Set the bot's application id.
*
* @param id the bot's application id
*/
public static void setBotApplicationId(String id)
2022-11-20 16:07:04 +01:00
{
2022-11-20 18:11:00 +01:00
botApplicationId = id;
2022-11-20 16:07:04 +01:00
}
2022-11-20 18:11:00 +01:00
/**
* Get the bot's application id
*
* @return a string of the bot's application id
*/
public static String getBotApplicationId() { return botApplicationId; }
2022-11-20 16:07:04 +01:00
2022-11-20 18:11:00 +01:00
/**
* Function to generate an invite link for the bot
*
* @return a string containing the invite link
*/
2022-11-20 16:07:04 +01:00
public static String getInviteUrl() {
2022-11-20 18:11:00 +01:00
return defaultInviteLink.replace("%userid%", botApplicationId);
2022-11-20 16:07:04 +01:00
}
/**
* Set the already fully-initialized DatabaseManager instance, ready to be accessed and used.
*
* @param databaseManagerInstance the fully-initialized DatabaseManager instance.
*/
public static void setDatabaseManagerInstance(DatabaseManager databaseManagerInstance)
{
dbManager = databaseManagerInstance;
}
/**
* Get the fully-initialized DatabaseManager instance, ready to be used.
*
* @return the DatabaseManager instance.
*/
public static @Nullable DatabaseManager getDatabaseManager() { return dbManager; }
/**
* Get the DateTimeFormatter string for parsing the expired messages timestamp.
*
* @return the String of the DateTimeFormatter format.
*/
public static String getExpiryTimestampFormat(){ return expiryTimestampFormat; }
/**
* Get the amount of seconds after which a message expires.
*
* @return long value of the expiry seconds.
*/
public static long getExpiryTimeSeconds() { return expiryTimeSeconds; }
2022-11-21 12:19:35 +01:00
public static String getBotName() { return botName; };
/**
* Get the bot's version.
*
* @return a String of the bot version.
*/
public static String getBotVersion() { return botVersion; }
/**
* Get the bot's global color.
*
* @return the Color object.
*/
public static Color getBotColor() { return botColor; }
/**
* Set the list of registered commands. They will be sorted alphabetically.
*
* @param commands a list of registered commands.
*/
public static void setRegisteredCommands(List<Command> commands)
{
// sort alphabetically by field getName()
List<Command> tempList = commands
.stream()
.sorted(Comparator.comparing(Command::getName))
.toList();
registeredCommands.addAll(tempList);
}
/**
* Get a list of all bot registered commands, sorted alphabetically.
*
* @return a copy of the List.
*/
public static List<Command> getRegisteredCommands()
{
return new ArrayList<>(registeredCommands);
}
/**
* Set the bot's startup time. Generally only used at boot time.
*
* @param time a LocalDateTime of the startup moment.
*/
public static void setStartupTime(LocalDateTime time)
{ startupTime = time; }
/**
* Get the time of when the bot was started up.
*
* @return a LocalDateTime object of the startup instant.
*/
public static LocalDateTime getStartupTime() { return startupTime; }
2022-08-26 00:13:06 +02:00
}