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:
Bea 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; }

View File

@ -10,7 +10,6 @@ import sun.misc.Signal;
import wtf.beatrice.hidekobot.commands.completer.ProfileImageCommandCompleter;
import wtf.beatrice.hidekobot.commands.message.HelloCommand;
import wtf.beatrice.hidekobot.commands.slash.*;
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
import wtf.beatrice.hidekobot.datasources.ConfigurationSource;
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
import wtf.beatrice.hidekobot.datasources.PropertiesSource;
@ -21,6 +20,7 @@ import wtf.beatrice.hidekobot.runnables.RandomOrgSeedTask;
import wtf.beatrice.hidekobot.runnables.StatusUpdateTask;
import wtf.beatrice.hidekobot.util.CommandUtil;
import wtf.beatrice.hidekobot.util.FormatUtil;
import wtf.beatrice.hidekobot.util.RandomUtil;
import java.io.File;
import java.time.LocalDateTime;
@ -113,13 +113,10 @@ public class HidekoBot
boolean enableRandomSeedUpdaterTask = false;
// initialize random.org object if API key is provided
{
String apiKey = Cache.getRandomOrgApiKey();
if(apiKey != null &&
!apiKey.isEmpty() &&
!apiKey.equals(ConfigurationEntry.RANDOM_ORG_API_KEY.getDefaultValue()))
if(RandomUtil.isRandomOrgKeyValid())
{
LOGGER.info("Enabling Random.org integration... This might take a while!");
Cache.initRandomOrg(apiKey);
RandomUtil.initRandomOrg();
enableRandomSeedUpdaterTask = true;
LOGGER.info("Random.org integration enabled!");
}

View File

@ -2,10 +2,10 @@ package wtf.beatrice.hidekobot.commands.base;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.User;
import org.random.util.RandomOrgRandom;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.MessageResponse;
import wtf.beatrice.hidekobot.objects.fun.Dice;
import wtf.beatrice.hidekobot.util.RandomUtil;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@ -126,8 +126,8 @@ public class DiceRoll
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
embedBuilder.setTitle("Dice Roll");
if(Cache.getRandom() instanceof RandomOrgRandom)
embedBuilder.setFooter("Seed provided by Random.org");
if(RandomUtil.isRandomOrgKeyValid())
embedBuilder.setFooter("Seed provided by random.org");
StringBuilder message = new StringBuilder();
int total = 0;

View File

@ -3,7 +3,7 @@ package wtf.beatrice.hidekobot.runnables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
import wtf.beatrice.hidekobot.util.RandomUtil;
/**
* This runnable pulls a random seed from random.org and used it to feed a SecureRandom,
@ -21,12 +21,10 @@ public class RandomOrgSeedTask implements Runnable
public void run()
{
String apiKey = Cache.getRandomOrgApiKey();
if(apiKey != null &&
!apiKey.isEmpty() &&
!apiKey.equals(ConfigurationEntry.RANDOM_ORG_API_KEY.getDefaultValue()))
if(RandomUtil.isRandomOrgKeyValid())
{
if(Cache.isVerbose()) LOGGER.info("Updating Random seed from random.org...");
Cache.initRandomOrg(Cache.getRandomOrgApiKey());
RandomUtil.initRandomOrg();
if(Cache.isVerbose()) LOGGER.info("Random.org seed updated!");
}
}

View File

@ -1,9 +1,19 @@
package wtf.beatrice.hidekobot.util;
import org.random.util.RandomOrgRandom;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
import java.security.SecureRandom;
import java.util.Random;
public class RandomUtil
{
// the Random instance that we should always use when looking for an RNG based thing.
// the seed is updated periodically, if the random.org integration is enabled.
private static Random randomInstance = new SecureRandom();
/**
* Returns a random integer picked in a range.
*
@ -26,9 +36,43 @@ public class RandomUtil
int difference = (max - min) + 1;
// find a number between 0 and our range (eg. 5 -> 8 = 0 -> 3)
int randomTemp = Cache.getRandom().nextInt(difference);
int randomTemp = getRandom().nextInt(difference);
// add the minimum value, so we are sure to be in the original range (0->5, 1->6, 2->7, 3->8)
return randomTemp + min;
}
public static Random getRandom() {
return randomInstance;
}
public static void initRandomOrg()
{
/* 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.
*/
String apiKey = Cache.getRandomOrgApiKey();
RandomOrgRandom randomOrg = new RandomOrgRandom(apiKey);
byte[] randomBytes = new byte[160];
randomOrg.nextBytes(randomBytes);
randomInstance = new SecureRandom(randomBytes);
}
public static boolean isRandomOrgKeyValid()
{
String apiKey = Cache.getRandomOrgApiKey();
return apiKey != null &&
!apiKey.isEmpty() &&
!apiKey.equals(ConfigurationEntry.RANDOM_ORG_API_KEY.getDefaultValue());
}
}