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

chore: optimize usage of webhooks #261

Merged
merged 5 commits into from
Jan 6, 2025
Merged
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
3 changes: 2 additions & 1 deletion src/main/java/minevalley/core/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.net.URL;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
Expand Down Expand Up @@ -1179,7 +1180,7 @@ public static Reminder createReminder(@Nonnegative int hours, @Nonnegative int m
* @throws IllegalArgumentException if the URL is null
*/
@Nonnull
public static Webhook createWebhook(@Nonnull String url) throws IllegalArgumentException {
public static Webhook createWebhook(@Nonnull URL url) throws IllegalArgumentException {
return server.createWebhook(url);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minevalley/core/api/CoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.net.URL;
import java.sql.SQLException;
import java.time.DayOfWeek;
import java.util.List;
Expand Down Expand Up @@ -261,7 +262,7 @@ public interface CoreServer {
Reminder createReminder(int hours, int minutes, @Nonnull Runnable callback, List<DayOfWeek> weekdays) throws IllegalArgumentException;

@Nonnull
Webhook createWebhook(@Nonnull String url) throws IllegalArgumentException;
Webhook createWebhook(@Nonnull URL url) throws IllegalArgumentException;

@Nonnull
EmbeddedMessage createEmbeddedMessage();
Expand Down
110 changes: 101 additions & 9 deletions src/main/java/minevalley/core/api/discord/EmbeddedMessage.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,116 @@
package minevalley.core.api.discord;

import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;
import java.awt.*;

@SuppressWarnings("unused")
public interface EmbeddedMessage {

EmbeddedMessage setTitle(String title);
/**
* Sets the title of the embedded message.
*
* @param title The title of the embedded message.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the title is null.
*/
@Nonnull
@Contract("_ -> this")
EmbeddedMessage setTitle(@Nonnull String title) throws IllegalArgumentException;

EmbeddedMessage setDescription(String description);
/**
* Sets the description of the embedded message.
*
* @param description The description of the embedded message.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the description is null.
*/
@Nonnull
@Contract("_ -> this")
EmbeddedMessage setDescription(@Nonnull String description) throws IllegalArgumentException;

EmbeddedMessage setAuthor(String name, String url, String iconUrl);
/**
* Sets the author of the embedded message.
*
* @param name The name of the author.
* @param url The URL of the author.
* @param iconUrl The URL of the author's icon.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the name, URL or icon URL is null.
*/
@Nonnull
@Contract("_, _, _ -> this")
EmbeddedMessage setAuthor(@Nonnull String name, @Nonnull String url, @Nonnull String iconUrl) throws IllegalArgumentException;

EmbeddedMessage setThumbnail(String url);
/**
* Sets the thumbnail of the embedded message.
*
* @param url The URL of the thumbnail.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the URL is null.
*/
@Nonnull
@Contract("_ -> this")
EmbeddedMessage setThumbnail(@Nonnull String url) throws IllegalArgumentException;

EmbeddedMessage setColor(Color color);
/**
* Sets the color of the embedded message.
*
* @param color The color of the embedded message.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the color is null.
*/
@Nonnull
@Contract("_ -> this")
EmbeddedMessage setColor(@Nonnull Color color) throws IllegalArgumentException;

EmbeddedMessage addField(String title, String text, boolean inline);
/**
* Adds a field to the embedded message.
*
* @param title The title of the field.
* @param text The text of the field.
* @param inline Whether the field should be inline or not.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the title or text is null.
*/
@Nonnull
@Contract("_, _, _ -> this")
EmbeddedMessage addField(@Nonnull String title, @Nonnull String text, boolean inline) throws IllegalArgumentException;

EmbeddedMessage setImage(String url);
/**
* Sets the image of the embedded message.
*
* @param url The URL of the image.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the URL is null.
*/
@Nonnull
@Contract("_ -> this")
EmbeddedMessage setImage(@Nonnull String url) throws IllegalArgumentException;

EmbeddedMessage setFooter(String footer, String iconUrl);
/**
* Sets the footer of the embedded message.
*
* @param footer The footer of the embedded message.
* @param iconUrl The URL of the footer's icon.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the footer is null.
*/
@Nonnull
@Contract("_, _ -> this")
EmbeddedMessage setFooter(@Nonnull String footer, @Nonnull String iconUrl) throws IllegalArgumentException;

EmbeddedMessage setFooter(String footer);
/**
* Sets the footer of the embedded message.
*
* @param footer The footer of the embedded message.
* @return The current instance of the embedded message.
* @throws IllegalArgumentException If the footer is null.
*/
@Nonnull
@Contract("_ -> this")
default EmbeddedMessage setFooter(@Nonnull String footer) throws IllegalArgumentException {
return setFooter(footer, "");
}
}
38 changes: 34 additions & 4 deletions src/main/java/minevalley/core/api/discord/Webhook.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
package minevalley.core.api.discord;

import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
* This Webhook-builder is meant to help you to create discord-webhooks.
* You can find helpful information here: <a href="https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html">...</a>
* You can find helpful information <a href="https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html">here</a>.
*/
@SuppressWarnings("unused")
public interface Webhook {

Webhook setUsername(String username);
/**
* Sets the username of the webhook.
*
* @param username The username of the webhook.
* @return The current instance of the webhook.
* @throws IllegalArgumentException If the username is null.
*/
@Nonnull
@Contract("_ -> this")
Webhook setUsername(@Nonnull String username) throws IllegalArgumentException;

Webhook setAvatar(String url);
/**
* Sets the avatar of the webhook.
*
* @param url The URL of the avatar.
* @return The current instance of the webhook.
* @throws IllegalArgumentException If the URL is null.
*/
@Nonnull
@Contract("_ -> this")
Webhook setAvatar(@Nonnull String url) throws IllegalArgumentException;

void send(EmbeddedMessage... message) throws IOException;
/**
* Sends the given messages to the discord-webhook.
*
* @param message The messages to send.
* @throws IOException If an I/O error occurs.
* @throws IllegalArgumentException If the message is null, or the given {@code EmbeddedMessage} is another implementation than our internal.
*/
@Contract(pure = true)
void send(@Nonnull EmbeddedMessage... message) throws IOException, IllegalArgumentException;
}
Loading