Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge upstream #1

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import xyz.srnyx.gradlegalaxy.utility.setupPublishing

plugins {
application
`java-library`
id("xyz.srnyx.gradle-galaxy") version "1.2.0"
id("io.github.goooler.shadow") version "8.1.7"
id("xyz.srnyx.gradle-galaxy") version "1.2.3"
id("com.gradleup.shadow") version "8.3.0"
}

setupJava("xyz.srnyx", "3.1.0", "A simple library for JDA Discord bots", JavaVersion.VERSION_19)
Expand All @@ -20,15 +19,13 @@ addCompilerArgs("-parameters")

repository(Repository.ALESSIO_DP, Repository.MAVEN_CENTRAL, Repository.JITPACK)
dependencies {
implementation("net.byteflux", "libby-core", "1.3.0")
compileOnlyApi("org.jetbrains", "annotations", "24.1.0")
compileOnlyApi("net.dv8tion", "JDA", "5.0.0-beta.24")
compileOnlyApi("xyz.srnyx", "java-utilities", "a71551bc2d")
compileOnlyApi("io.github.freya022", "BotCommands", "2.10.3")
compileOnlyApi("org.spongepowered", "configurate-yaml", "4.1.2")
compileOnly("org.postgresql", "postgresql", "42.7.3")
compileOnly("com.zaxxer", "HikariCP", "5.1.0")
compileOnly("ch.qos.logback", "logback-classic", "1.5.3")
compileOnly("net.dv8tion", "JDA", "5.0.2") // JDA
implementation("xyz.srnyx", "java-utilities", "a073202b43") // General Java utility library
implementation("io.github.freya022", "BotCommands", "2.10.3") // Command framework
implementation("org.spongepowered", "configurate-yaml", "4.1.2") // Config manager
implementation("org.postgresql", "postgresql", "42.7.3") // Database
implementation("com.zaxxer", "HikariCP", "5.1.0") // Database
implementation("ch.qos.logback", "logback-classic", "1.5.6") // Logging
}

setupPublishing(
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
7 changes: 5 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
104 changes: 58 additions & 46 deletions src/main/java/xyz/srnyx/lazylibrary/LazyEmbed.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,15 @@ public LazyEmbed(@NotNull ConfigurationNode node) {
* @param lazyEmbed the {@link LazyEmbed} to copy
*/
public LazyEmbed(@NotNull LazyEmbed lazyEmbed) {
this.color = lazyEmbed.color;
this.authorName = lazyEmbed.authorName;
this.authorUrl = lazyEmbed.authorUrl;
this.authorIcon = lazyEmbed.authorIcon;
this.titleText = lazyEmbed.titleText;
this.titleUrl = lazyEmbed.titleUrl;
this.description = lazyEmbed.description;
this.thumbnail = lazyEmbed.thumbnail;
this.image = lazyEmbed.image;
this.fields.addAll(lazyEmbed.fields);
this.footerText = lazyEmbed.footerText;
this.footerIcon = lazyEmbed.footerIcon;
this.timestamp = lazyEmbed.timestamp;
setColor(lazyEmbed.color);
setAuthor(lazyEmbed.authorName, lazyEmbed.authorUrl, lazyEmbed.authorIcon);
setTitle(lazyEmbed.titleText, lazyEmbed.titleUrl);
setDescription(lazyEmbed.description);
setThumbnail(lazyEmbed.thumbnail);
setImage(lazyEmbed.image);
addFields(lazyEmbed.fields);
setFooter(lazyEmbed.footerText, lazyEmbed.footerIcon);
setTimestamp(lazyEmbed.timestamp);
}

/**
Expand Down Expand Up @@ -330,42 +326,46 @@ public LazyEmbed replace(@NotNull String key, @Nullable Object value) {
*/
@NotNull
public MessageEmbed build(@NotNull LazyLibrary library) {
// Get replaceable values
String authorNameReplace = authorName;
String titleTextReplace = titleText;
String descriptionReplace = description;
String footerTextReplace = footerText;

// Parse replacements for the values
for (final Map.Entry<String, String> entry : replacements.entrySet()) {
if (authorNameReplace != null) authorNameReplace = authorNameReplace.replace(entry.getKey(), entry.getValue());
if (titleTextReplace != null) titleTextReplace = titleTextReplace.replace(entry.getKey(), entry.getValue());
if (descriptionReplace != null) descriptionReplace = descriptionReplace.replace(entry.getKey(), entry.getValue());
if (footerTextReplace != null) footerTextReplace = footerTextReplace.replace(entry.getKey(), entry.getValue());
}

// Set the values
setAuthor(authorNameReplace, authorUrl, authorIcon);
setTitle(titleTextReplace, titleUrl);
setDescription(descriptionReplace);
setFooter(footerTextReplace, footerIcon);

// Fields
final List<MessageEmbed.Field> fieldsCopy = new ArrayList<>(fields);
clearFields();
for (final MessageEmbed.Field field : fieldsCopy) {
// Get name and value
String name = field.getName();
String value = field.getValue();

// Parse replacements for the name and value
// Replacements
if (!replacements.isEmpty()) {
// Get replaceable values
String authorNameReplace = authorName;
String titleTextReplace = titleText;
String descriptionReplace = description;
String footerTextReplace = footerText;

// Parse replacements for the values
for (final Map.Entry<String, String> entry : replacements.entrySet()) {
if (name != null) name = name.replace(entry.getKey(), entry.getValue());
if (value != null) value = value.replace(entry.getKey(), entry.getValue());
if (authorNameReplace != null) authorNameReplace = authorNameReplace.replace(entry.getKey(), entry.getValue());
if (titleTextReplace != null) titleTextReplace = titleTextReplace.replace(entry.getKey(), entry.getValue());
if (descriptionReplace != null) descriptionReplace = descriptionReplace.replace(entry.getKey(), entry.getValue());
if (footerTextReplace != null) footerTextReplace = footerTextReplace.replace(entry.getKey(), entry.getValue());
}

// Add the new field
if (name != null && value != null) addField(name, value, field.isInline());
// Set the new values
setAuthor(authorNameReplace, authorUrl, authorIcon);
setTitle(titleTextReplace, titleUrl);
setDescription(descriptionReplace);
setFooter(footerTextReplace, footerIcon);

// Fields
final List<MessageEmbed.Field> newFields = new ArrayList<>(fields);
clearFields();
for (final MessageEmbed.Field field : newFields) {
// Get name and value
String name = field.getName();
String value = field.getValue();
if (name == null || value == null) continue;

// Parse replacements for the name and value
for (final Map.Entry<String, String> entry : replacements.entrySet()) {
name = name.replace(entry.getKey(), entry.getValue());
value = value.replace(entry.getKey(), entry.getValue());
}

// Add the new field
addField(name, value, field.isInline());
}
}

// Set defaults
Expand Down Expand Up @@ -635,6 +635,18 @@ public LazyEmbed setTimestamp(@Nullable Long timestamp) {
return setTimestamp(timestamp == null ? null : Instant.ofEpochMilli(timestamp));
}

/**
* Sets the timestamp of the embed
*
* @param timestamp the timestamp of the embed
*
* @return this
*/
@NotNull
public LazyEmbed setTimestamp(@Nullable Date timestamp) {
return setTimestamp(timestamp == null ? null : timestamp.getTime());
}

/**
* All possible (defaultable) keys an {@link LazyEmbed embed} can have ({@link LazySettings#embedDefaults})
*/
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/xyz/srnyx/lazylibrary/utility/LazyMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import xyz.srnyx.javautilities.MiscUtility;

import java.util.Optional;


/**
* A utility class for converting {@link Object Objects} to other types
Expand All @@ -18,10 +20,10 @@ public class LazyMapper {
*
* @param object the {@link String} to parse
*
* @return the {@link Long snowflake} or {@code null}
* @return the {@link Long snowflake} or empty
*/
@Nullable
public static Long parseSnowflake(@NotNull Object object) {
@NotNull
public static Optional<Long> parseSnowflake(@NotNull Object object) {
return MiscUtility.handleException(() -> MiscUtil.parseSnowflake(object.toString()));
}

Expand All @@ -30,14 +32,11 @@ public static Long parseSnowflake(@NotNull Object object) {
*
* @param object the {@link Object} to convert
*
* @return the {@link UserSnowflake} or {@code null}
* @return the {@link UserSnowflake} or empty
*/
@Nullable
public static UserSnowflake toUserSnowflake(@Nullable Object object) {
if (object == null) return null;
final Long snowflake = parseSnowflake(object);
if (snowflake == null) return null;
return MiscUtility.handleException(() -> UserSnowflake.fromId(snowflake));
@NotNull
public static Optional<UserSnowflake> toUserSnowflake(@Nullable Object object) {
return object == null ? Optional.empty() : parseSnowflake(object).flatMap(snowflake -> MiscUtility.handleException(() -> UserSnowflake.fromId(snowflake)));
}

private LazyMapper() {
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/xyz/srnyx/lazylibrary/utility/LazyUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.jetbrains.annotations.NotNull;

import xyz.srnyx.javautilities.MiscUtility;
import xyz.srnyx.javautilities.manipulation.Mapper;

import xyz.srnyx.lazylibrary.LazyEmoji;
Expand Down Expand Up @@ -56,21 +55,23 @@ public static PaginatorBuilder getDefaultPaginator() {
*/
public static boolean userHasChannelPermission(@NotNull Interaction interaction, @NotNull Permission... permissions) {
final Member member = interaction.getMember();
final GuildChannel channel = MiscUtility.handleException(() -> (GuildChannel) interaction.getChannel());
if (member == null || channel == null) return false;
return member.hasPermission(channel, permissions);
return member != null && Mapper.to(interaction.getChannel(), GuildChannel.class)
.map(channel -> member.hasPermission(channel, permissions))
.orElse(false);
}

@NotNull private static final Function<BoundExtractedResult<Command.Choice>, Command.Choice> STRING_MAPPING = BoundExtractedResult::getReferent;
@NotNull private static final Function<BoundExtractedResult<Command.Choice>, Command.Choice> INTEGER_MAPPING = result -> {
final Command.Choice choice = result.getReferent();
final Long value = Mapper.toLong(choice.getAsString());
return value == null ? null : new Command.Choice(choice.getName(), value);
return Mapper.toLong(choice.getAsString())
.map(value -> new Command.Choice(choice.getName(), value))
.orElse(null);
};
@NotNull private static final Function<BoundExtractedResult<Command.Choice>, Command.Choice> NUMBER_MAPPING = result -> {
final Command.Choice choice = result.getReferent();
final Double value = Mapper.toDouble(choice.getAsString());
return value == null ? null : new Command.Choice(choice.getName(), value);
return Mapper.toDouble(choice.getAsString())
.map(value -> new Command.Choice(choice.getName(), value))
.orElse(null);
};

/**
Expand Down
Loading