forked from Fascinated/Bat
add spotify feature
This commit is contained in:
@ -13,7 +13,7 @@ public final class MathUtils {
|
||||
/**
|
||||
* Format a number to a specific amount of decimal places.
|
||||
*
|
||||
* @param number the number to format
|
||||
* @param number the number to format
|
||||
* @param additional the additional decimal places to format
|
||||
* @return the formatted number
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@ public class MemberUtils {
|
||||
* Checks if a user has permission to edit another user
|
||||
*
|
||||
* @param guild the guild to check
|
||||
* @param user the user to check
|
||||
* @param user the user to check
|
||||
* @return if the user has permission to edit another user
|
||||
*/
|
||||
public static boolean hasPermissionToEdit(BatGuild guild, BatUser user) {
|
||||
|
@ -12,7 +12,7 @@ public class NumberUtils {
|
||||
/**
|
||||
* Formats a number with commas.
|
||||
* <p>
|
||||
* Example: 1000 -> 1,000 | Example: 1000.5 -> 1,000.5
|
||||
* Example: 1000 -> 1,000 | Example: 1000.5 -> 1,000.5
|
||||
* </p>
|
||||
*
|
||||
* @param number the number to format
|
||||
|
@ -8,14 +8,16 @@ import lombok.Setter;
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter @Setter
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class Profile {
|
||||
/**
|
||||
* The key of the profile.
|
||||
*/
|
||||
private String profileKey;
|
||||
|
||||
public Profile() {}
|
||||
public Profile() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the profile
|
||||
|
@ -19,7 +19,7 @@ public class ProfileHolder {
|
||||
* Gets a profile for the holder
|
||||
*
|
||||
* @param clazz The class of the profile
|
||||
* @param <T> The type of the profile
|
||||
* @param <T> The type of the profile
|
||||
* @return The profile
|
||||
*/
|
||||
public <T extends Profile> T getProfile(Class<T> clazz) {
|
||||
@ -30,7 +30,7 @@ public class ProfileHolder {
|
||||
Profile profile = profiles.values().stream().filter(p -> p.getClass().equals(clazz)).findFirst().orElse(null);
|
||||
if (profile == null) {
|
||||
try {
|
||||
profile = (Profile) clazz.newInstance();
|
||||
profile = clazz.newInstance();
|
||||
profiles.put(profile.getProfileKey(), profile);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -11,9 +11,9 @@ public class RoleUtils {
|
||||
/**
|
||||
* Checks if a member has permission to give the role to another member
|
||||
*
|
||||
* @param guild the guild to check
|
||||
* @param guild the guild to check
|
||||
* @param member the member to check
|
||||
* @param role the role to check
|
||||
* @param role the role to check
|
||||
* @return if the member has permission to give the role
|
||||
*/
|
||||
public static boolean hasPermissionToGiveRole(BatGuild guild, Member member, Role role) {
|
||||
|
20
src/main/java/cc/fascinated/bat/common/StringUtils.java
Normal file
20
src/main/java/cc/fascinated/bat/common/StringUtils.java
Normal file
@ -0,0 +1,20 @@
|
||||
package cc.fascinated.bat.common;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
public class StringUtils {
|
||||
/**
|
||||
* Generates a random string
|
||||
*
|
||||
* @param length the length of the string
|
||||
* @return the random string
|
||||
*/
|
||||
public static String randomString(int length) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (int i = 0; i < length; i++) {
|
||||
stringBuilder.append((char) (Math.random() * 26 + 'a'));
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
@ -20,49 +20,49 @@ public final class TimeUtils {
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis) {
|
||||
return format(millis, WildTimeUnit.FIT);
|
||||
return format(millis, BatTimeFormat.FIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a time in millis to a readable time format.
|
||||
*
|
||||
* @param millis the millis to 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) {
|
||||
public static String format(long millis, BatTimeFormat timeUnit) {
|
||||
return format(millis, timeUnit, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a time in millis to a readable time format.
|
||||
*
|
||||
* @param millis the millis to 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
|
||||
* @param compact whether to use a compact display
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis, WildTimeUnit timeUnit, boolean compact) {
|
||||
public static String format(long millis, BatTimeFormat 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 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
|
||||
* @param compact whether to use a compact display
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis, WildTimeUnit timeUnit, boolean decimals, boolean compact) {
|
||||
public static String format(long millis, BatTimeFormat 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()) {
|
||||
if (timeUnit == BatTimeFormat.FIT) {
|
||||
for (BatTimeFormat otherTimeUnit : BatTimeFormat.VALUES) {
|
||||
if (otherTimeUnit != BatTimeFormat.FIT && millis >= otherTimeUnit.getMillis()) {
|
||||
timeUnit = otherTimeUnit;
|
||||
break;
|
||||
}
|
||||
@ -74,7 +74,7 @@ public final class TimeUtils {
|
||||
}
|
||||
String formatted = time + (compact ? timeUnit.getSuffix() : " " + timeUnit.getDisplay()); // Append the time unit
|
||||
if (time != 1.0 && !compact) { // Pluralize the time unit
|
||||
formatted+= "s";
|
||||
formatted += "s";
|
||||
}
|
||||
return formatted;
|
||||
}
|
||||
@ -89,16 +89,16 @@ public final class TimeUtils {
|
||||
* @return the time in millis
|
||||
*/
|
||||
public static long fromString(String input) {
|
||||
Matcher matcher = WildTimeUnit.SUFFIX_PATTERN.matcher(input); // Match the given input
|
||||
Matcher matcher = BatTimeFormat.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
|
||||
BatTimeFormat timeUnit = BatTimeFormat.fromSuffix(suffix); // The time unit to add
|
||||
if (timeUnit != null) { // Increment the total millis
|
||||
millis+= amount * timeUnit.getMillis();
|
||||
millis += amount * timeUnit.getMillis();
|
||||
}
|
||||
}
|
||||
return millis;
|
||||
@ -107,9 +107,11 @@ public final class TimeUtils {
|
||||
/**
|
||||
* Represents a unit of time.
|
||||
*/
|
||||
@NoArgsConstructor @AllArgsConstructor
|
||||
@Getter(AccessLevel.PRIVATE) @ToString
|
||||
public enum WildTimeUnit {
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@ToString
|
||||
public enum BatTimeFormat {
|
||||
FIT,
|
||||
YEARS("Year", "y", TimeUnit.DAYS.toMillis(365L)),
|
||||
MONTHS("Month", "mo", TimeUnit.DAYS.toMillis(30L)),
|
||||
@ -123,7 +125,7 @@ public final class TimeUtils {
|
||||
/**
|
||||
* Our cached unit values.
|
||||
*/
|
||||
public static final WildTimeUnit[] VALUES = values();
|
||||
public static final BatTimeFormat[] VALUES = values();
|
||||
|
||||
/**
|
||||
* Our cached suffix pattern.
|
||||
@ -152,8 +154,8 @@ public final class TimeUtils {
|
||||
* @return the time unit, null if not found
|
||||
*/
|
||||
@Nullable
|
||||
public static WildTimeUnit fromSuffix(String suffix) {
|
||||
for (WildTimeUnit unit : VALUES) {
|
||||
public static BatTimeFormat fromSuffix(String suffix) {
|
||||
for (BatTimeFormat unit : VALUES) {
|
||||
if (unit != FIT && unit.getSuffix().equals(suffix)) {
|
||||
return unit;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public class TimerUtils {
|
||||
* Runs a repeating task on a schedule
|
||||
*
|
||||
* @param runnable the task to run
|
||||
* @param delay the delay before the task runs
|
||||
* @param delay the delay before the task runs
|
||||
*/
|
||||
public static void scheduleRepeating(Runnable runnable, long delay, long period) {
|
||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||
|
@ -27,14 +27,15 @@ public class WebRequest {
|
||||
* Gets a response from the given URL.
|
||||
*
|
||||
* @param url the url
|
||||
* @return the response
|
||||
* @param <T> the type of the response
|
||||
* @return the response
|
||||
*/
|
||||
public static <T> T getAsEntity(String url, Class<T> clazz) throws RateLimitException {
|
||||
ResponseEntity<T> responseEntity = CLIENT.get()
|
||||
.uri(url)
|
||||
.retrieve()
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {
|
||||
}) // Don't throw exceptions on error
|
||||
.toEntity(clazz);
|
||||
|
||||
if (responseEntity.getStatusCode().isError()) {
|
||||
@ -56,7 +57,8 @@ public class WebRequest {
|
||||
return CLIENT.get()
|
||||
.uri(url)
|
||||
.retrieve()
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {
|
||||
}) // Don't throw exceptions on error
|
||||
.toEntity(clazz);
|
||||
}
|
||||
|
||||
@ -70,7 +72,8 @@ public class WebRequest {
|
||||
return CLIENT.head()
|
||||
.uri(url)
|
||||
.retrieve()
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
|
||||
.onStatus(HttpStatusCode::isError, (request, response) -> {
|
||||
}) // Don't throw exceptions on error
|
||||
.toEntity(clazz);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user