forked from Fascinated/Bat
update botstats command
This commit is contained in:
parent
4a7e7de6a2
commit
001ece7899
@ -2,6 +2,7 @@ package cc.fascinated.bat.command.impl;
|
|||||||
|
|
||||||
import cc.fascinated.bat.command.BatCommand;
|
import cc.fascinated.bat.command.BatCommand;
|
||||||
import cc.fascinated.bat.common.EmbedUtils;
|
import cc.fascinated.bat.common.EmbedUtils;
|
||||||
|
import cc.fascinated.bat.common.TimeUtils;
|
||||||
import cc.fascinated.bat.model.BatGuild;
|
import cc.fascinated.bat.model.BatGuild;
|
||||||
import cc.fascinated.bat.model.BatUser;
|
import cc.fascinated.bat.model.BatUser;
|
||||||
import cc.fascinated.bat.service.DiscordService;
|
import cc.fascinated.bat.service.DiscordService;
|
||||||
@ -15,11 +16,15 @@ import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.management.RuntimeMXBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Fascinated (fascinated7)
|
* @author Fascinated (fascinated7)
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class BotStatsCommand extends BatCommand {
|
public class BotStatsCommand extends BatCommand {
|
||||||
|
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
|
||||||
private final GuildService guildService;
|
private final GuildService guildService;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@ -39,6 +44,9 @@ public class BotStatsCommand extends BatCommand {
|
|||||||
"➜ Guilds: %s\n".formatted(jda.getGuilds().size()) +
|
"➜ Guilds: %s\n".formatted(jda.getGuilds().size()) +
|
||||||
"➜ Users: %s\n".formatted(jda.getUsers().size()) +
|
"➜ Users: %s\n".formatted(jda.getUsers().size()) +
|
||||||
"➜ Gateway Ping: %sms\n".formatted(jda.getGatewayPing()) +
|
"➜ Gateway Ping: %sms\n".formatted(jda.getGatewayPing()) +
|
||||||
|
"\n" +
|
||||||
|
"**Bat Statistics**\n" +
|
||||||
|
"➜ Uptime: %s\n".formatted(TimeUtils.format(bean.getUptime())) +
|
||||||
"➜ Cached Guilds: %s\n".formatted(guildService.getGuilds().size()) +
|
"➜ Cached Guilds: %s\n".formatted(guildService.getGuilds().size()) +
|
||||||
"➜ Cached Users: %s".formatted(userService.getUsers().size())
|
"➜ Cached Users: %s".formatted(userService.getUsers().size())
|
||||||
).build()).queue();
|
).build()).queue();
|
||||||
|
27
src/main/java/cc/fascinated/bat/common/MathUtils.java
Normal file
27
src/main/java/cc/fascinated/bat/common/MathUtils.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package cc.fascinated.bat.common;
|
||||||
|
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.DecimalFormatSymbols;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Fascinated (fascinated7)
|
||||||
|
*/
|
||||||
|
@UtilityClass
|
||||||
|
public final class MathUtils {
|
||||||
|
/**
|
||||||
|
* Format a number to a specific amount of decimal places.
|
||||||
|
*
|
||||||
|
* @param number the number to format
|
||||||
|
* @param additional the additional decimal places to format
|
||||||
|
* @return the formatted number
|
||||||
|
*/
|
||||||
|
public static double format(double number, int additional) {
|
||||||
|
return Double.parseDouble(
|
||||||
|
new DecimalFormat("#.#" + "#".repeat(Math.max(0, additional - 1)),
|
||||||
|
new DecimalFormatSymbols()
|
||||||
|
).format(number)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
165
src/main/java/cc/fascinated/bat/common/TimeUtils.java
Normal file
165
src/main/java/cc/fascinated/bat/common/TimeUtils.java
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package cc.fascinated.bat.common;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Fascinated (fascinated7)
|
||||||
|
*/
|
||||||
|
@UtilityClass
|
||||||
|
public final class TimeUtils {
|
||||||
|
/**
|
||||||
|
* Format a time in millis to a readable time format.
|
||||||
|
*
|
||||||
|
* @param millis the millis to format
|
||||||
|
* @return the formatted time
|
||||||
|
*/
|
||||||
|
public static String format(long millis) {
|
||||||
|
return format(millis, WildTimeUnit.FIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a time in millis to a readable time format.
|
||||||
|
*
|
||||||
|
* @param millis the millis to format
|
||||||
|
* @param timeUnit the time unit to format the millis to
|
||||||
|
* @return the formatted time
|
||||||
|
*/
|
||||||
|
public static String format(long millis, WildTimeUnit timeUnit) {
|
||||||
|
return format(millis, timeUnit, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a time in millis to a readable time format.
|
||||||
|
*
|
||||||
|
* @param millis the millis to format
|
||||||
|
* @param timeUnit the time unit to format the millis to
|
||||||
|
* @param compact whether to use a compact display
|
||||||
|
* @return the formatted time
|
||||||
|
*/
|
||||||
|
public static String format(long millis, WildTimeUnit timeUnit, boolean compact) {
|
||||||
|
return format(millis, timeUnit, true, compact);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a time in millis to a readable time format.
|
||||||
|
*
|
||||||
|
* @param millis the millis to format
|
||||||
|
* @param timeUnit the time unit to format the millis to
|
||||||
|
* @param decimals whether to include decimals
|
||||||
|
* @param compact whether to use a compact display
|
||||||
|
* @return the formatted time
|
||||||
|
*/
|
||||||
|
public static String format(long millis, WildTimeUnit timeUnit, boolean decimals, boolean compact) {
|
||||||
|
if (millis == -1L) { // Format permanent
|
||||||
|
return "Perm" + (compact ? "" : "anent");
|
||||||
|
}
|
||||||
|
// Format the time to the best fitting time unit
|
||||||
|
if (timeUnit == WildTimeUnit.FIT) {
|
||||||
|
for (WildTimeUnit otherTimeUnit : WildTimeUnit.VALUES) {
|
||||||
|
if (otherTimeUnit != WildTimeUnit.FIT && millis >= otherTimeUnit.getMillis()) {
|
||||||
|
timeUnit = otherTimeUnit;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double time = MathUtils.format((double) millis / timeUnit.getMillis(), 1); // Format the time
|
||||||
|
if (!decimals) { // Remove decimals
|
||||||
|
time = (int) time;
|
||||||
|
}
|
||||||
|
String formatted = time + (compact ? timeUnit.getSuffix() : " " + timeUnit.getDisplay()); // Append the time unit
|
||||||
|
if (time != 1.0 && !compact) { // Pluralize the time unit
|
||||||
|
formatted+= "s";
|
||||||
|
}
|
||||||
|
return formatted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the given input into a time in millis.
|
||||||
|
* <p>
|
||||||
|
* E.g: 1d, 1h, 1d1h, etc
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param input the input to parse
|
||||||
|
* @return the time in millis
|
||||||
|
*/
|
||||||
|
public static long fromString(String input) {
|
||||||
|
Matcher matcher = WildTimeUnit.SUFFIX_PATTERN.matcher(input); // Match the given input
|
||||||
|
long millis = 0; // The total millis
|
||||||
|
|
||||||
|
// Match corresponding suffixes and add up the total millis
|
||||||
|
while (matcher.find()) {
|
||||||
|
int amount = Integer.parseInt(matcher.group(1)); // The amount of time to add
|
||||||
|
String suffix = matcher.group(2); // The unit suffix
|
||||||
|
WildTimeUnit timeUnit = WildTimeUnit.fromSuffix(suffix); // The time unit to add
|
||||||
|
if (timeUnit != null) { // Increment the total millis
|
||||||
|
millis+= amount * timeUnit.getMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a unit of time.
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor @AllArgsConstructor
|
||||||
|
@Getter(AccessLevel.PRIVATE) @ToString
|
||||||
|
public enum WildTimeUnit {
|
||||||
|
FIT,
|
||||||
|
YEARS("Year", "y", TimeUnit.DAYS.toMillis(365L)),
|
||||||
|
MONTHS("Month", "mo", TimeUnit.DAYS.toMillis(30L)),
|
||||||
|
WEEKS("Week", "w", TimeUnit.DAYS.toMillis(7L)),
|
||||||
|
DAYS("Day", "d", TimeUnit.DAYS.toMillis(1L)),
|
||||||
|
HOURS("Hour", "h", TimeUnit.HOURS.toMillis(1L)),
|
||||||
|
MINUTES("Minute", "m", TimeUnit.MINUTES.toMillis(1L)),
|
||||||
|
SECONDS("Second", "s", TimeUnit.SECONDS.toMillis(1L)),
|
||||||
|
MILLISECONDS("Millisecond", "ms", 1L);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Our cached unit values.
|
||||||
|
*/
|
||||||
|
public static final WildTimeUnit[] VALUES = values();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Our cached suffix pattern.
|
||||||
|
*/
|
||||||
|
public static final Pattern SUFFIX_PATTERN = Pattern.compile("(\\d+)(mo|ms|[ywdhms])");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The display of this time unit.
|
||||||
|
*/
|
||||||
|
private String display;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The suffix of this time unit.
|
||||||
|
*/
|
||||||
|
private String suffix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The amount of millis in this time unit.
|
||||||
|
*/
|
||||||
|
private long millis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the time unit with the given suffix.
|
||||||
|
*
|
||||||
|
* @param suffix the time unit suffix
|
||||||
|
* @return the time unit, null if not found
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static WildTimeUnit fromSuffix(String suffix) {
|
||||||
|
for (WildTimeUnit unit : VALUES) {
|
||||||
|
if (unit != FIT && unit.getSuffix().equals(suffix)) {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user