This commit is contained in:
@@ -27,7 +27,8 @@ public class CommandUtil
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtil.class);
|
||||
|
||||
private CommandUtil() {
|
||||
private CommandUtil()
|
||||
{
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
@@ -40,7 +41,8 @@ public class CommandUtil
|
||||
public static void delete(ButtonInteractionEvent event)
|
||||
{
|
||||
// check if the user interacting is the same one who ran the command
|
||||
if (!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) {
|
||||
if (!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId())))
|
||||
{
|
||||
event.reply("❌ You did not run this command!").setEphemeral(true).queue();
|
||||
return;
|
||||
}
|
||||
@@ -63,8 +65,10 @@ public class CommandUtil
|
||||
|
||||
// populate commands list from registered commands
|
||||
List<CommandData> allCommands = new ArrayList<>();
|
||||
for(SlashCommand cmd : Cache.getSlashCommandListener().getRegisteredCommands())
|
||||
{ allCommands.add(cmd.getSlashCommandData()); }
|
||||
for (SlashCommand cmd : Cache.getSlashCommandListener().getRegisteredCommands())
|
||||
{
|
||||
allCommands.add(cmd.getSlashCommandData());
|
||||
}
|
||||
|
||||
JDA jdaInstance = HidekoBot.getAPI();
|
||||
|
||||
@@ -73,22 +77,22 @@ public class CommandUtil
|
||||
|
||||
boolean update = false;
|
||||
|
||||
if(force)
|
||||
if (force)
|
||||
{
|
||||
update = true;
|
||||
} else
|
||||
{
|
||||
|
||||
// for each command that we have already registered...
|
||||
for(Command currRegCmd : registeredCommands)
|
||||
for (Command currRegCmd : registeredCommands)
|
||||
{
|
||||
boolean found = false;
|
||||
|
||||
// iterate through all "recognized" commands
|
||||
for(CommandData cmdData : allCommands)
|
||||
for (CommandData cmdData : allCommands)
|
||||
{
|
||||
// if we find the same command...
|
||||
if(cmdData.getName().equals(currRegCmd.getName()))
|
||||
if (cmdData.getName().equals(currRegCmd.getName()))
|
||||
{
|
||||
// quit the loop since we found it.
|
||||
found = true;
|
||||
@@ -98,7 +102,7 @@ public class CommandUtil
|
||||
|
||||
// if no match was found, we need to send an updated command list because
|
||||
// an old command was probably removed.
|
||||
if(!found)
|
||||
if (!found)
|
||||
{
|
||||
update = true;
|
||||
|
||||
@@ -108,18 +112,18 @@ public class CommandUtil
|
||||
}
|
||||
|
||||
// if an update is not already queued...
|
||||
if(!update)
|
||||
if (!update)
|
||||
{
|
||||
// for each "recognized" valid command
|
||||
for(CommandData currCmdData : allCommands)
|
||||
for (CommandData currCmdData : allCommands)
|
||||
{
|
||||
boolean found = false;
|
||||
|
||||
// iterate through all already registered commands.
|
||||
for(Command cmd : registeredCommands)
|
||||
for (Command cmd : registeredCommands)
|
||||
{
|
||||
// if this command was already registered...
|
||||
if(cmd.getName().equals(currCmdData.getName()))
|
||||
if (cmd.getName().equals(currCmdData.getName()))
|
||||
{
|
||||
// quit the loop since we found a match.
|
||||
found = true;
|
||||
@@ -129,7 +133,7 @@ public class CommandUtil
|
||||
|
||||
// if no match was found, we need to send an updated command list because
|
||||
// a new command was probably added.
|
||||
if(!found)
|
||||
if (!found)
|
||||
{
|
||||
update = true;
|
||||
|
||||
@@ -142,7 +146,7 @@ public class CommandUtil
|
||||
|
||||
LOGGER.info("Found {} commands.", registeredCommands.size());
|
||||
|
||||
if(update)
|
||||
if (update)
|
||||
{
|
||||
// send updated command list.
|
||||
jdaInstance.updateCommands().addCommands(allCommands).queue();
|
||||
@@ -169,18 +173,18 @@ public class CommandUtil
|
||||
MessageChannel textChannel = null;
|
||||
|
||||
// this should never happen, but only message channels are supported.
|
||||
if(!msgChannelType.isMessage())
|
||||
if (!msgChannelType.isMessage())
|
||||
{
|
||||
databaseSource.untrackExpiredMessage(messageId);
|
||||
return;
|
||||
}
|
||||
|
||||
// if this is a DM
|
||||
if(!(msgChannelType.isGuild()))
|
||||
if (!(msgChannelType.isGuild()))
|
||||
{
|
||||
String userId = databaseSource.getTrackedReplyUserId(messageId);
|
||||
User user = userId == null ? null : HidekoBot.getAPI().retrieveUserById(userId).complete();
|
||||
if(user == null)
|
||||
if (user == null)
|
||||
{
|
||||
// if user is not found, consider it expired
|
||||
// (deleted profile, or blocked the bot)
|
||||
@@ -189,12 +193,11 @@ public class CommandUtil
|
||||
}
|
||||
|
||||
textChannel = user.openPrivateChannel().complete();
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
String guildId = databaseSource.getQueuedExpiringMessageGuild(messageId);
|
||||
Guild guild = guildId == null ? null : HidekoBot.getAPI().getGuildById(guildId);
|
||||
if(guild == null)
|
||||
if (guild == null)
|
||||
{
|
||||
// if guild is not found, consider it expired
|
||||
// (server was deleted or bot was kicked)
|
||||
@@ -204,7 +207,7 @@ public class CommandUtil
|
||||
textChannel = guild.getTextChannelById(channelId);
|
||||
}
|
||||
|
||||
if(textChannel == null)
|
||||
if (textChannel == null)
|
||||
{
|
||||
// if channel is not found, count it as expired
|
||||
// (channel was deleted or bot permissions restricted)
|
||||
@@ -215,11 +218,11 @@ public class CommandUtil
|
||||
RestAction<Message> retrieveAction = textChannel.retrieveMessageById(messageId);
|
||||
|
||||
|
||||
if(Cache.isVerbose()) LOGGER.info("cleaning up: {}", messageId);
|
||||
if (Cache.isVerbose()) LOGGER.info("cleaning up: {}", messageId);
|
||||
|
||||
retrieveAction.queue(
|
||||
message -> {
|
||||
if(message == null)
|
||||
if (message == null)
|
||||
{
|
||||
databaseSource.untrackExpiredMessage(messageId);
|
||||
return;
|
||||
|
||||
@@ -12,20 +12,21 @@ import java.util.Arrays;
|
||||
public class FormatUtil
|
||||
{
|
||||
|
||||
private FormatUtil() {
|
||||
private FormatUtil()
|
||||
{
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
// cosmetic string to print on startup.
|
||||
private static final String LOGO = """
|
||||
\s
|
||||
██╗░░██╗██╗██████╗░███████╗██╗░░██╗░█████╗░
|
||||
██║░░██║██║██╔══██╗██╔════╝██║░██╔╝██╔══██╗
|
||||
███████║██║██║░░██║█████╗░░█████═╝░██║░░██║
|
||||
██╔══██║██║██║░░██║██╔══╝░░██╔═██╗░██║░░██║
|
||||
██║░░██║██║██████╔╝███████╗██║░╚██╗╚█████╔╝
|
||||
╚═╝░░╚═╝╚═╝╚═════╝░╚══════╝╚═╝░░╚═╝░╚════╝░
|
||||
\s""";
|
||||
\s
|
||||
██╗░░██╗██╗██████╗░███████╗██╗░░██╗░█████╗░
|
||||
██║░░██║██║██╔══██╗██╔════╝██║░██╔╝██╔══██╗
|
||||
███████║██║██║░░██║█████╗░░█████═╝░██║░░██║
|
||||
██╔══██║██║██║░░██║██╔══╝░░██╔═██╗░██║░░██║
|
||||
██║░░██║██║██████╔╝███████╗██║░╚██╗╚█████╔╝
|
||||
╚═╝░░╚═╝╚═╝╚═════╝░╚══════╝╚═╝░░╚═╝░╚════╝░
|
||||
\s""";
|
||||
|
||||
|
||||
/**
|
||||
@@ -60,21 +61,25 @@ public class FormatUtil
|
||||
*
|
||||
* @return the formatted String
|
||||
*/
|
||||
public static String getNiceDuration(Duration duration) {
|
||||
public static String getNiceDuration(Duration duration)
|
||||
{
|
||||
long days = duration.toDays();
|
||||
long hours = duration.toHoursPart();
|
||||
long minutes = duration.toMinutesPart();
|
||||
long seconds = duration.toSecondsPart();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (days > 0) {
|
||||
if (days > 0)
|
||||
{
|
||||
sb.append(days).append("d ");
|
||||
sb.append(hours).append("h ");
|
||||
sb.append(minutes).append("m ");
|
||||
} else if (hours > 0) {
|
||||
} else if (hours > 0)
|
||||
{
|
||||
sb.append(hours).append("h ");
|
||||
sb.append(minutes).append("m ");
|
||||
} else if (minutes > 0) {
|
||||
} else if (minutes > 0)
|
||||
{
|
||||
sb.append(minutes).append("m ");
|
||||
}
|
||||
sb.append(seconds).append("s");
|
||||
@@ -93,7 +98,7 @@ public class FormatUtil
|
||||
public static Duration parseDuration(String duration)
|
||||
{
|
||||
// sanitize a bit to avoid cluttering with garbled strings
|
||||
if(duration.length() > 16) duration = duration.substring(0, 16);
|
||||
if (duration.length() > 16) duration = duration.substring(0, 16);
|
||||
duration = duration.replaceAll("[^\\w]", ""); //only keep digits and word characters
|
||||
duration = duration.toLowerCase();
|
||||
|
||||
@@ -105,7 +110,7 @@ public class FormatUtil
|
||||
{1,10} is used to limit the size of the input to parse, to avoid stack overflows.
|
||||
no one should be typing more than 10 arguments, or more than 10 digits for a single argument anyway.
|
||||
*/
|
||||
if(!duration.matches("(\\d{1,10}[a-zA-Z]{1,10}){1,10}"))
|
||||
if (!duration.matches("(\\d{1,10}[a-zA-Z]{1,10}){1,10}"))
|
||||
return null;
|
||||
|
||||
String[] durationTimes = duration.split("[a-zA-Z]+");
|
||||
@@ -116,7 +121,7 @@ public class FormatUtil
|
||||
|
||||
Duration fullDuration = Duration.ZERO;
|
||||
|
||||
for(int i = 0; i < durationTimes.length; i++)
|
||||
for (int i = 0; i < durationTimes.length; i++)
|
||||
{
|
||||
String durationTimeStr = durationTimes[i];
|
||||
String durationUnitStr = durationUnits[i];
|
||||
@@ -124,7 +129,7 @@ public class FormatUtil
|
||||
int durationValue = Integer.parseInt(durationTimeStr);
|
||||
TemporalUnit unit = parseTimeUnit(durationUnitStr);
|
||||
|
||||
if(unit != null)
|
||||
if (unit != null)
|
||||
fullDuration = fullDuration.plus(durationValue, unit);
|
||||
else return null; // if we failed finding the time unit, instantly quit with failed parsing.
|
||||
}
|
||||
|
||||
@@ -44,13 +44,14 @@ public class Logger<T>
|
||||
* Logs a message to console, after delaying it.
|
||||
*
|
||||
* @param message the message to log
|
||||
* @param delay the time to wait before logging, in seconds
|
||||
* @param delay the time to wait before logging, in seconds
|
||||
*/
|
||||
public void log(String message, int delay)
|
||||
{
|
||||
// create a new scheduled executor with an anonymous runnable...
|
||||
//... after waiting <delay> seconds.
|
||||
try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor()) {
|
||||
try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor())
|
||||
{
|
||||
executor.schedule(() -> log(message), delay, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,8 @@ import java.util.Random;
|
||||
public class RandomUtil
|
||||
{
|
||||
|
||||
private RandomUtil() {
|
||||
private RandomUtil()
|
||||
{
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
@@ -27,8 +28,8 @@ public class RandomUtil
|
||||
*/
|
||||
public static int getRandomNumber(int min, int max)
|
||||
{
|
||||
if(min == max) return min; // dumbass
|
||||
if(min > max) // swap em
|
||||
if (min == max) return min; // dumbass
|
||||
if (min > max) // swap em
|
||||
{
|
||||
min = min - max;
|
||||
max = min + max;
|
||||
@@ -47,7 +48,8 @@ public class RandomUtil
|
||||
}
|
||||
|
||||
|
||||
public static Random getRandom() {
|
||||
public static Random getRandom()
|
||||
{
|
||||
return randomInstance;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,33 +10,38 @@ import java.util.List;
|
||||
public class SerializationUtil
|
||||
{
|
||||
|
||||
private SerializationUtil() {
|
||||
private SerializationUtil()
|
||||
{
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static <T> String serializeBase64(List<T> dataList) {
|
||||
public static <T> String serializeBase64(List<T> dataList)
|
||||
{
|
||||
|
||||
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
ObjectOutputStream so = new ObjectOutputStream(bo)) {
|
||||
ObjectOutputStream so = new ObjectOutputStream(bo))
|
||||
{
|
||||
so.writeObject(dataList);
|
||||
so.flush();
|
||||
return Base64.getEncoder().encodeToString(bo.toByteArray());
|
||||
}
|
||||
catch (IOException e) {
|
||||
} catch (IOException e)
|
||||
{
|
||||
throw new SerializationException("Error during serialization", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> LinkedList<T> deserializeBase64(String dataStr) {
|
||||
public static <T> LinkedList<T> deserializeBase64(String dataStr)
|
||||
{
|
||||
|
||||
byte[] b = Base64.getDecoder().decode(dataStr);
|
||||
ByteArrayInputStream bi = new ByteArrayInputStream(b);
|
||||
ObjectInputStream si;
|
||||
try {
|
||||
try
|
||||
{
|
||||
si = new ObjectInputStream(bi);
|
||||
return LinkedList.class.cast(si.readObject());
|
||||
}
|
||||
catch (IOException | ClassNotFoundException e) {
|
||||
} catch (IOException | ClassNotFoundException e)
|
||||
{
|
||||
throw new SerializationException("Error during deserialization", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user