Move random methods to random util class, fix footer
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
7de23d8207
commit
4df2429b09
@ -1,7 +1,6 @@
|
|||||||
package wtf.beatrice.hidekobot;
|
package wtf.beatrice.hidekobot;
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.random.util.RandomOrgRandom;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
|
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
|
||||||
@ -15,10 +14,8 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
|||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
@ -30,10 +27,6 @@ public class Cache
|
|||||||
private static final String botPrefix = "hideko";
|
private static final String botPrefix = "hideko";
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Cache.class);
|
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
|
// map to store results of "love calculator", to avoid people re-running the same command until
|
||||||
// they get what they wanted.
|
// they get what they wanted.
|
||||||
// i didn't think this was worthy of a whole database table with a runnable checking for expiration,
|
// 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 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.
|
* 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);
|
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.
|
* Get the bot maintainer's profile id.
|
||||||
@ -320,6 +289,10 @@ public class Cache
|
|||||||
/*private static ConfigurationSource getConfigurationSource()
|
/*private static ConfigurationSource getConfigurationSource()
|
||||||
{ return configurationSource; }*/
|
{ return configurationSource; }*/
|
||||||
|
|
||||||
|
public static String getRandomOrgApiKey() {
|
||||||
|
return configurationSource == null ? null : (String) configurationSource.getConfigValue(ConfigurationEntry.RANDOM_ORG_API_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
public static void setConfigurationSource(ConfigurationSource configurationSource)
|
public static void setConfigurationSource(ConfigurationSource configurationSource)
|
||||||
{ Cache.configurationSource = configurationSource; }
|
{ Cache.configurationSource = configurationSource; }
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ import sun.misc.Signal;
|
|||||||
import wtf.beatrice.hidekobot.commands.completer.ProfileImageCommandCompleter;
|
import wtf.beatrice.hidekobot.commands.completer.ProfileImageCommandCompleter;
|
||||||
import wtf.beatrice.hidekobot.commands.message.HelloCommand;
|
import wtf.beatrice.hidekobot.commands.message.HelloCommand;
|
||||||
import wtf.beatrice.hidekobot.commands.slash.*;
|
import wtf.beatrice.hidekobot.commands.slash.*;
|
||||||
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
|
|
||||||
import wtf.beatrice.hidekobot.datasources.ConfigurationSource;
|
import wtf.beatrice.hidekobot.datasources.ConfigurationSource;
|
||||||
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
||||||
import wtf.beatrice.hidekobot.datasources.PropertiesSource;
|
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.runnables.StatusUpdateTask;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.util.CommandUtil;
|
||||||
import wtf.beatrice.hidekobot.util.FormatUtil;
|
import wtf.beatrice.hidekobot.util.FormatUtil;
|
||||||
|
import wtf.beatrice.hidekobot.util.RandomUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@ -113,13 +113,10 @@ public class HidekoBot
|
|||||||
boolean enableRandomSeedUpdaterTask = false;
|
boolean enableRandomSeedUpdaterTask = false;
|
||||||
// initialize random.org object if API key is provided
|
// initialize random.org object if API key is provided
|
||||||
{
|
{
|
||||||
String apiKey = Cache.getRandomOrgApiKey();
|
if(RandomUtil.isRandomOrgKeyValid())
|
||||||
if(apiKey != null &&
|
|
||||||
!apiKey.isEmpty() &&
|
|
||||||
!apiKey.equals(ConfigurationEntry.RANDOM_ORG_API_KEY.getDefaultValue()))
|
|
||||||
{
|
{
|
||||||
LOGGER.info("Enabling Random.org integration... This might take a while!");
|
LOGGER.info("Enabling Random.org integration... This might take a while!");
|
||||||
Cache.initRandomOrg(apiKey);
|
RandomUtil.initRandomOrg();
|
||||||
enableRandomSeedUpdaterTask = true;
|
enableRandomSeedUpdaterTask = true;
|
||||||
LOGGER.info("Random.org integration enabled!");
|
LOGGER.info("Random.org integration enabled!");
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,10 @@ package wtf.beatrice.hidekobot.commands.base;
|
|||||||
|
|
||||||
import net.dv8tion.jda.api.EmbedBuilder;
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import org.random.util.RandomOrgRandom;
|
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||||
import wtf.beatrice.hidekobot.objects.fun.Dice;
|
import wtf.beatrice.hidekobot.objects.fun.Dice;
|
||||||
|
import wtf.beatrice.hidekobot.util.RandomUtil;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -126,8 +126,8 @@ public class DiceRoll
|
|||||||
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
|
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
|
||||||
embedBuilder.setTitle("Dice Roll");
|
embedBuilder.setTitle("Dice Roll");
|
||||||
|
|
||||||
if(Cache.getRandom() instanceof RandomOrgRandom)
|
if(RandomUtil.isRandomOrgKeyValid())
|
||||||
embedBuilder.setFooter("Seed provided by Random.org");
|
embedBuilder.setFooter("Seed provided by random.org");
|
||||||
|
|
||||||
StringBuilder message = new StringBuilder();
|
StringBuilder message = new StringBuilder();
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
@ -3,7 +3,7 @@ package wtf.beatrice.hidekobot.runnables;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
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,
|
* 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()
|
public void run()
|
||||||
{
|
{
|
||||||
String apiKey = Cache.getRandomOrgApiKey();
|
String apiKey = Cache.getRandomOrgApiKey();
|
||||||
if(apiKey != null &&
|
if(RandomUtil.isRandomOrgKeyValid())
|
||||||
!apiKey.isEmpty() &&
|
|
||||||
!apiKey.equals(ConfigurationEntry.RANDOM_ORG_API_KEY.getDefaultValue()))
|
|
||||||
{
|
{
|
||||||
if(Cache.isVerbose()) LOGGER.info("Updating Random seed from random.org...");
|
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!");
|
if(Cache.isVerbose()) LOGGER.info("Random.org seed updated!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
package wtf.beatrice.hidekobot.util;
|
package wtf.beatrice.hidekobot.util;
|
||||||
|
|
||||||
|
import org.random.util.RandomOrgRandom;
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
|
import wtf.beatrice.hidekobot.datasources.ConfigurationEntry;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class RandomUtil
|
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.
|
* Returns a random integer picked in a range.
|
||||||
*
|
*
|
||||||
@ -26,9 +36,43 @@ public class RandomUtil
|
|||||||
int difference = (max - min) + 1;
|
int difference = (max - min) + 1;
|
||||||
|
|
||||||
// find a number between 0 and our range (eg. 5 -> 8 = 0 -> 3)
|
// 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)
|
// 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;
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user