2022-08-26 00:13:06 +02:00
|
|
|
package wtf.beatrice.hidekobot;
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2022-11-22 23:44:34 +01:00
|
|
|
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
|
|
|
|
import wtf.beatrice.hidekobot.datasources.ConfigurationSource;
|
|
|
|
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
|
|
|
import wtf.beatrice.hidekobot.datasources.PropertiesSource;
|
2022-11-22 16:19:08 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.MessageCommandListener;
|
2022-08-26 20:43:12 +02:00
|
|
|
import wtf.beatrice.hidekobot.listeners.MessageLogger;
|
2022-11-23 00:01:05 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener;
|
2022-11-22 14:53:46 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
2022-11-22 00:31:52 +01:00
|
|
|
import wtf.beatrice.hidekobot.util.Logger;
|
2022-08-26 20:43:12 +02:00
|
|
|
|
2022-11-21 12:19:35 +01:00
|
|
|
import java.awt.*;
|
2022-11-22 00:04:34 +01:00
|
|
|
import java.lang.reflect.Field;
|
2022-11-21 12:19:35 +01:00
|
|
|
import java.time.LocalDateTime;
|
2022-12-20 22:15:52 +01:00
|
|
|
import java.util.Random;
|
2022-11-21 12:19:35 +01:00
|
|
|
|
2022-11-21 20:19:35 +01:00
|
|
|
public class Cache
|
2022-08-26 00:13:06 +02:00
|
|
|
{
|
|
|
|
|
2022-11-22 00:04:34 +01:00
|
|
|
|
2022-11-22 21:48:13 +01:00
|
|
|
// todo: make this compatible with the message listener's regex
|
|
|
|
private static final String botPrefix = "hideko";
|
2022-11-22 00:04:34 +01:00
|
|
|
private static final Logger logger = new Logger(Cache.class);
|
2022-11-22 23:28:59 +01:00
|
|
|
|
2022-12-20 22:15:52 +01:00
|
|
|
// the Random instance that we should always use when looking for an RNG based thing.
|
|
|
|
// the seed is updated periodically.
|
|
|
|
private static final Random randomInstance = new Random();
|
|
|
|
|
2022-11-22 23:28:59 +01:00
|
|
|
private static PropertiesSource propertiesSource = null;
|
2022-11-22 00:28:33 +01:00
|
|
|
private static ConfigurationSource configurationSource = null;
|
|
|
|
private static DatabaseSource databaseSource = null;
|
2022-08-26 00:13:06 +02:00
|
|
|
private static boolean verbose = false;
|
2022-11-22 21:48:13 +01:00
|
|
|
private static MessageLogger verbosityLogger = null;
|
2022-11-21 20:04:28 +01:00
|
|
|
private static final long botMaintainerId = 979809420714332260L;
|
2022-11-21 00:14:13 +01:00
|
|
|
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.
|
2022-11-21 19:11:18 +01:00
|
|
|
private final static long expiryTimeSeconds = 15L;
|
2022-11-21 00:14:13 +01:00
|
|
|
|
2022-11-22 16:19:08 +01:00
|
|
|
// used to count e.g. uptime
|
2022-11-22 21:48:13 +01:00
|
|
|
private static LocalDateTime startupTime = null;
|
2022-11-21 12:19:35 +01:00
|
|
|
|
2022-11-21 23:28:33 +01:00
|
|
|
private final static String execPath = System.getProperty("user.dir");
|
2022-11-22 20:50:53 +01:00
|
|
|
private static final String botName = "Hideko";
|
2022-11-22 14:53:46 +01:00
|
|
|
|
|
|
|
private static SlashCommandListener slashCommandListener = null;
|
2022-11-23 00:01:05 +01:00
|
|
|
private static SlashCommandCompletionListener slashCommandCompletionListener = null;
|
2022-11-22 16:19:08 +01:00
|
|
|
private static MessageCommandListener messageCommandListener = null;
|
2022-11-21 12:19:35 +01:00
|
|
|
|
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
|
|
|
|
2022-11-20 19:00:27 +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
|
|
|
|
2022-12-20 22:15:52 +01:00
|
|
|
public static Random getRandom() {
|
|
|
|
return randomInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setRandomSeed(long seed) {
|
|
|
|
randomInstance.setSeed(seed);
|
|
|
|
}
|
|
|
|
|
2022-08-26 20:43:12 +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; }
|
|
|
|
|
2022-08-26 20:43:12 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2022-08-26 20:43:12 +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-08-26 20:43:12 +02:00
|
|
|
*
|
2022-11-20 06:07:25 +01:00
|
|
|
* @return a long of the account's id
|
2022-08-26 20:43:12 +02:00
|
|
|
*/
|
2022-11-22 00:04:34 +01:00
|
|
|
public static long getBotOwnerId() {
|
2022-11-22 23:42:21 +01:00
|
|
|
return configurationSource == null ? 0L : (Long) configurationSource.getConfigValue(ConfigurationEntry.BOT_OWNER_ID);
|
2022-11-22 00:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the bot's token.
|
|
|
|
*
|
|
|
|
* @return a String of the bot's token.
|
|
|
|
*/
|
|
|
|
public static String getBotToken() {
|
2022-11-22 23:42:21 +01:00
|
|
|
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.BOT_TOKEN);
|
2022-11-22 00:04:34 +01:00
|
|
|
}
|
2022-11-20 05:57:42 +01:00
|
|
|
|
2022-11-21 20:04:28 +01:00
|
|
|
/**
|
|
|
|
* Get the bot maintainer's profile id.
|
|
|
|
*
|
|
|
|
* @return a long of the account's id
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static long getBotMaintainerId() { return botMaintainerId; }
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
/**
|
2022-11-22 00:28:33 +01:00
|
|
|
* Set the already fully-initialized DatabaseSource instance, ready to be accessed and used.
|
2022-11-21 00:14:13 +01:00
|
|
|
*
|
2022-11-22 00:28:33 +01:00
|
|
|
* @param databaseSourceInstance the fully-initialized DatabaseSource instance.
|
2022-11-21 00:14:13 +01:00
|
|
|
*/
|
2022-11-22 00:28:33 +01:00
|
|
|
public static void setDatabaseSourceInstance(DatabaseSource databaseSourceInstance)
|
2022-11-21 00:14:13 +01:00
|
|
|
{
|
2022-11-22 00:28:33 +01:00
|
|
|
databaseSource = databaseSourceInstance;
|
2022-11-21 00:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 00:28:33 +01:00
|
|
|
* Get the fully-initialized DatabaseSource instance, ready to be used.
|
2022-11-21 00:14:13 +01:00
|
|
|
*
|
2022-11-22 00:28:33 +01:00
|
|
|
* @return the DatabaseSource instance.
|
2022-11-21 00:14:13 +01:00
|
|
|
*/
|
2022-11-22 00:28:33 +01:00
|
|
|
public static @Nullable DatabaseSource getDatabaseSource() { return databaseSource; }
|
2022-11-21 00:14:13 +01:00
|
|
|
|
2022-11-22 23:28:59 +01:00
|
|
|
/**
|
|
|
|
* Set the properties source instance loaded from the JAR archive.
|
|
|
|
*
|
|
|
|
* @param propertiesSourceInstance the properties source instance.
|
|
|
|
*/
|
|
|
|
public static void setPropertiesSourceInstance(PropertiesSource propertiesSourceInstance)
|
|
|
|
{
|
|
|
|
propertiesSource = propertiesSourceInstance;
|
|
|
|
}
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-11-22 23:28:59 +01:00
|
|
|
public static String getBotVersion() {
|
|
|
|
return propertiesSource.getProperty("bot.version");
|
|
|
|
}
|
2022-11-21 12:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the bot's global color.
|
|
|
|
*
|
|
|
|
* @return the Color object.
|
|
|
|
*/
|
2022-11-22 00:04:34 +01:00
|
|
|
public static Color getBotColor() {
|
|
|
|
Color defaultColor = Color.PINK;
|
2022-11-22 00:28:33 +01:00
|
|
|
if(configurationSource == null) return defaultColor;
|
2022-11-22 23:42:21 +01:00
|
|
|
String colorName = (String) configurationSource.getConfigValue(ConfigurationEntry.BOT_COLOR);
|
2022-11-22 00:04:34 +01:00
|
|
|
|
|
|
|
Color color = null;
|
|
|
|
try {
|
|
|
|
Field field = Color.class.getField(colorName);
|
|
|
|
color = (Color)field.get(null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.log("Unknown color: " + colorName);
|
|
|
|
}
|
|
|
|
return color == null ? defaultColor : color;
|
|
|
|
}
|
2022-11-21 12:19:35 +01:00
|
|
|
|
|
|
|
|
2022-11-22 16:19:08 +01:00
|
|
|
//todo javadocs
|
2022-11-22 14:53:46 +01:00
|
|
|
public static void setSlashCommandListener(SlashCommandListener commandListener)
|
2022-11-22 16:19:08 +01:00
|
|
|
{ slashCommandListener = commandListener; }
|
2022-11-21 12:19:35 +01:00
|
|
|
|
2022-11-22 14:53:46 +01:00
|
|
|
public static SlashCommandListener getSlashCommandListener() { return slashCommandListener; }
|
2022-11-21 12:19:35 +01:00
|
|
|
|
2022-11-22 16:19:08 +01:00
|
|
|
|
2022-11-23 00:01:05 +01:00
|
|
|
public static void setSlashCommandCompletionListener(SlashCommandCompletionListener commandCompletionListener)
|
|
|
|
{ slashCommandCompletionListener = commandCompletionListener; }
|
|
|
|
|
|
|
|
public static SlashCommandCompletionListener getSlashCommandCompletionListener() { return slashCommandCompletionListener; }
|
|
|
|
|
|
|
|
|
2022-11-22 16:19:08 +01:00
|
|
|
public static void setMessageCommandListener(MessageCommandListener commandListener)
|
|
|
|
{ messageCommandListener = commandListener; }
|
|
|
|
|
|
|
|
public static MessageCommandListener getMessageCommandListener() { return messageCommandListener; }
|
|
|
|
|
2022-11-21 12:19:35 +01:00
|
|
|
/**
|
|
|
|
* 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-11-21 19:54:49 +01:00
|
|
|
public static String getFullHeartBeatLink() {
|
2022-11-22 23:42:21 +01:00
|
|
|
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.HEARTBEAT_LINK);
|
2022-11-21 19:54:49 +01:00
|
|
|
}
|
|
|
|
//todo javadocs
|
2022-11-21 23:28:33 +01:00
|
|
|
public static String getExecPath() { return execPath; }
|
2022-11-21 19:54:49 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
/*private static ConfigurationSource getConfigurationSource()
|
|
|
|
{ return configurationSource; }*/
|
2022-11-21 23:36:42 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
public static void setConfigurationSource(ConfigurationSource configurationSource)
|
|
|
|
{ Cache.configurationSource = configurationSource; }
|
2022-11-21 23:36:42 +01:00
|
|
|
|
2022-11-22 21:48:13 +01:00
|
|
|
/**
|
|
|
|
* Get the bot's prefix
|
|
|
|
*
|
|
|
|
* @return a String of the bot's prefix.
|
|
|
|
*/
|
|
|
|
public static String getBotPrefix() { return botPrefix; }
|
|
|
|
|
2022-08-26 00:13:06 +02:00
|
|
|
}
|