Move random methods to random util class, fix footer
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-01-15 04:34:19 +01:00
parent 7de23d8207
commit 4df2429b09
5 changed files with 58 additions and 46 deletions

View File

@@ -1,7 +1,6 @@
package wtf.beatrice.hidekobot;
import org.jetbrains.annotations.Nullable;
import org.random.util.RandomOrgRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
@@ -15,10 +14,8 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
import java.awt.*;
import java.lang.reflect.Field;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -30,10 +27,6 @@ public class Cache
private static final String botPrefix = "hideko";
private static final Logger LOGGER = LoggerFactory.getLogger(Cache.class);
// the Random instance that we should always use when looking for an RNG based thing.
// the seed is updated periodically.
private static Random randomInstance = new SecureRandom();
// map to store results of "love calculator", to avoid people re-running the same command until
// they get what they wanted.
// i didn't think this was worthy of a whole database table with a runnable checking for expiration,
@@ -83,27 +76,6 @@ public class Cache
*/
public static int[] getSupportedAvatarResolutions() { return supportedAvatarResolutions; }
public static Random getRandom() {
return randomInstance;
}
public static void initRandomOrg(String apiKey)
{
/* we use the random.org instance to generate 160 random bytes.
then, we're feeding those 160 bytes as a seed for a SecureRandom.
this is preferred to calling the RandomOrgRandom directly every time,
because it has to query the api and (1) takes a long time, especially with big
dice rolls, and (2) you'd run in the limits extremely quickly if the bot
was run publicly for everyone to use.
*/
RandomOrgRandom randomOrg = new RandomOrgRandom(apiKey);
byte[] randomBytes = new byte[160];
randomOrg.nextBytes(randomBytes);
randomInstance = new SecureRandom(randomBytes);
}
/**
* Checks if the bot has been started with the verbose argument.
@@ -158,9 +130,6 @@ public class Cache
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.BOT_TOKEN);
}
public static String getRandomOrgApiKey() {
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.RANDOM_ORG_API_KEY);
}
/**
* Get the bot maintainer's profile id.
@@ -320,6 +289,10 @@ public class Cache
/*private static ConfigurationSource getConfigurationSource()
{ return configurationSource; }*/
public static String getRandomOrgApiKey() {
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.RANDOM_ORG_API_KEY);
}
public static void setConfigurationSource(ConfigurationSource configurationSource)
{ Cache.configurationSource = configurationSource; }