1
0
forked from Fascinated/Bat

much stuff

This commit is contained in:
Lee
2024-06-24 13:56:01 +01:00
parent eeb09ee1fd
commit 0b176c3b2a
36 changed files with 1493 additions and 69 deletions

@ -0,0 +1,23 @@
package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@UtilityClass
public class DateUtils {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT;
/**
* Gets the date from a string.
*
* @param date The date string.
* @return The date.
*/
public static Date getDateFromString(String date) {
return Date.from(Instant.from(FORMATTER.parse(date)));
}
}

@ -0,0 +1,50 @@
package cc.fascinated.bat.common;
import net.dv8tion.jda.api.EmbedBuilder;
import java.time.LocalDateTime;
/**
* @author Fascinated (fascinated7)
*/
public class EmbedUtils {
/**
* Builds a generic embed
*
* @param description the description of the embed
* @return the embed builder
*/
public static EmbedBuilder buildGenericEmbed(String description) {
return new EmbedBuilder()
.setDescription(description)
.setTimestamp(LocalDateTime.now())
.setColor(0x2F3136);
}
/**
* Builds an error embed
*
* @param description the description of the embed
* @return the embed builder
*/
public static EmbedBuilder buildErrorEmbed(String description) {
return new EmbedBuilder()
.setDescription(description)
.setTimestamp(LocalDateTime.now())
.setColor(0xFF0000);
}
/**
* Builds a success embed
*
* @param description the description of the embed
* @return the embed builder
*/
public static EmbedBuilder buildSuccessEmbed(String description) {
return new EmbedBuilder()
.setDescription(description)
.setTimestamp(LocalDateTime.now())
.setColor(0x00FF00);
}
}

@ -0,0 +1,16 @@
package cc.fascinated.bat.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.data.annotation.Transient;
/**
* @author Fascinated (fascinated7)
*/
@AllArgsConstructor @Getter
public class Profile {
/**
* The key of the profile.
*/
@Transient private final String profileKey;
}

@ -0,0 +1,77 @@
package cc.fascinated.bat.common;
import cc.fascinated.bat.exception.RateLimitException;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
@UtilityClass
public class WebRequest {
/**
* The web client.
*/
private static final RestClient CLIENT;
static {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(2500); // 2.5 seconds
CLIENT = RestClient.builder()
.requestFactory(requestFactory)
.build();
}
/**
* Gets a response from the given URL.
*
* @param url the url
* @return the response
* @param <T> the type of 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
.toEntity(clazz);
if (responseEntity.getStatusCode().isError()) {
return null;
}
if (responseEntity.getStatusCode().isSameCodeAs(HttpStatus.TOO_MANY_REQUESTS)) {
throw new RateLimitException("Rate limit reached");
}
return responseEntity.getBody();
}
/**
* Gets a response from the given URL.
*
* @param url the url
* @return the response
*/
public static ResponseEntity<?> get(String url, Class<?> clazz) {
return CLIENT.get()
.uri(url)
.retrieve()
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
.toEntity(clazz);
}
/**
* Gets a response from the given URL.
*
* @param url the url
* @return the response
*/
public static ResponseEntity<?> head(String url, Class<?> clazz) {
return CLIENT.head()
.uri(url)
.retrieve()
.onStatus(HttpStatusCode::isError, (request, response) -> {}) // Don't throw exceptions on error
.toEntity(clazz);
}
}