From 897aa404ed700b3b23ac5a8c5a75fbf36f3ae893 Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Sun, 21 Jan 2024 01:20:12 -0800 Subject: [PATCH 1/6] added a setSilent method similar to the setEphmeral method --- .../minnced/discord/webhook/MessageFlags.java | 23 +- .../discord/webhook/send/WebhookMessage.java | 146 ++++++---- .../webhook/send/WebhookMessageBuilder.java | 258 ++++++++++-------- 3 files changed, 262 insertions(+), 165 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/MessageFlags.java b/src/main/java/club/minnced/discord/webhook/MessageFlags.java index d9a0ffc..907487a 100644 --- a/src/main/java/club/minnced/discord/webhook/MessageFlags.java +++ b/src/main/java/club/minnced/discord/webhook/MessageFlags.java @@ -17,16 +17,21 @@ package club.minnced.discord.webhook; /** - * Constants for the message flags described by the Discord Documentation. + * Constants for the message flags described by the Discord Documentation. */ @SuppressWarnings("PointlessBitwiseExpression") public class MessageFlags { - public static final int CROSSPOSTED = 1 << 0; - public static final int IS_CROSSPOSTED = 1 << 1; - public static final int SUPPRESS_EMBEDS = 1 << 2; + public static final int CROSSPOSTED = 1 << 0; + public static final int IS_CROSSPOSTED = 1 << 1; + public static final int SUPPRESS_EMBEDS = 1 << 2; public static final int SOURCE_MESSAGE_DELETED = 1 << 3; - public static final int URGENT = 1 << 4; - public static final int HAS_THREAD = 1 << 5; - public static final int EPHEMERAL = 1 << 6; - public static final int LOADING = 1 << 7; -} + public static final int URGENT = 1 << 4; + public static final int HAS_THREAD = 1 << 5; + public static final int EPHEMERAL = 1 << 6; + public static final int LOADING = 1 << 7; + public static final int FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8; + public static final int SUPPRESS_NOTIFICATIONS = 1 << 12; + public static final int IS_VOICE_MESSAGE = 1 << 13; +} \ No newline at end of file diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 78ce7c8..101bedf 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -33,8 +33,10 @@ /** * Send-only message for a {@link club.minnced.discord.webhook.WebhookClient} - *
A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent - * by first converting it to a WebhookMessage with {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. + *
+ * A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent + * by first converting it to a WebhookMessage with + * {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. */ public class WebhookMessage { /** @@ -53,9 +55,9 @@ public class WebhookMessage { protected final String threadName; protected WebhookMessage(final String username, final String avatarUrl, final String content, - final List embeds, final boolean isTTS, - final MessageAttachment[] files, final AllowedMentions allowedMentions, - final int flags, final String threadName) { + final List embeds, final boolean isTTS, + final MessageAttachment[] files, final AllowedMentions allowedMentions, + final int flags, final String threadName) { this.username = username; this.avatarUrl = avatarUrl; this.content = content; @@ -136,11 +138,13 @@ public int getFlags() { } /** - * Returns a new WebhookMessage instance with the ephemeral flag turned on/off (true/false). - *
This instance remains unchanged and a new instance is returned. + * Returns a new WebhookMessage instance with the ephemeral flag turned on/off + * (true/false). + *
+ * This instance remains unchanged and a new instance is returned. * - * @param ephemeral - * Whether to make this message ephemeral + * @param ephemeral + * Whether to make this message ephemeral * * @return New WebhookMessage instance */ @@ -151,19 +155,43 @@ public WebhookMessage asEphemeral(boolean ephemeral) { flags |= MessageFlags.EPHEMERAL; else flags &= ~MessageFlags.EPHEMERAL; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, + threadName); + } + + /** + * Returns a new WebhookMessage instance with the silent flag turned on/off + * (true/false). + *
+ * This instance remains unchanged and a new instance is returned. + * + * @param silent + * Whether to make this message silent + * + * @return New WebhookMessage instance + */ + @NotNull + public WebhookMessage asSilent(boolean silent) { + int flags = this.flags; + if (silent) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, + threadName); } /** * Converts a {@link club.minnced.discord.webhook.receive.ReadonlyMessage} to a * WebhookMessage. - *
This does not convert attachments. + *
+ * This does not convert attachments. * - * @param message - * The message to convert + * @param message + * The message to convert * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return A WebhookMessage copy */ @@ -176,6 +204,7 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { builder.setContent(message.getContent()); builder.setTTS(message.isTTS()); builder.setEphemeral((message.getFlags() & MessageFlags.EPHEMERAL) != 0); + builder.setSilent((message.getFlags() & MessageFlags.SUPPRESS_NOTIFICATIONS) != 0); builder.addEmbeds(message.getEmbeds()); return builder.build(); } @@ -185,18 +214,21 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * * @param first - * The first embed + * The first embed * @param embeds - * Optional additional embeds for the message + * Optional additional embeds for the message * * @return A WebhookMessage for the embeds * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than {@value WebhookMessage#MAX_EMBEDS} are provided + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are provided */ - @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. Edition - Item 53) + @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. + // Edition - Item 53) public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) { Objects.requireNonNull(embeds, "Embeds"); if (embeds.length >= WebhookMessage.MAX_EMBEDS) @@ -214,13 +246,15 @@ public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull Webhoo * Creates a WebhookMessage from * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * - * @param embeds - * Embeds for the message + * @param embeds + * Embeds for the message * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than {@value WebhookMessage#MAX_EMBEDS} are provided + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are provided * * @return A WebhookMessage for the embeds */ @@ -232,22 +266,25 @@ public static WebhookMessage embeds(@NotNull Collection embeds) { if (embeds.isEmpty()) throw new IllegalArgumentException("Cannot build an empty message"); embeds.forEach(Objects::requireNonNull); - return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, null); + return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, + null); } /** * Creates a WebhookMessage from the provided attachments. - *
A message can hold up to {@value #MAX_FILES} attachments + *
+ * A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - * @param attachments - * The attachments to add, keys are the alternative names - * for each attachment + * @param attachments + * The attachments to add, keys are the alternative names + * for each attachment * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or more than {@value #MAX_FILES} + * If no attachments are provided or + * more than {@value #MAX_FILES} * * @return A WebhookMessage for the attachments */ @@ -259,7 +296,8 @@ public static WebhookMessage files(@NotNull Map attachments) { if (fileAmount == 0) throw new IllegalArgumentException("Cannot build an empty message"); if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); Set> entries = attachments.entrySet(); MessageAttachment[] files = new MessageAttachment[fileAmount]; int i = 0; @@ -274,29 +312,38 @@ public static WebhookMessage files(@NotNull Map attachments) { /** * Creates a WebhookMessage from the provided attachments. - *
A message can hold up to {@value #MAX_FILES} attachments + *
+ * A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - *

The files are provided in pairs of {@literal Name->Data} similar + *

+ * The files are provided in pairs of {@literal Name->Data} similar * to the first 2 arguments. - *
The allowed data types are {@code byte[] | InputStream | File} + *
+ * The allowed data types are {@code byte[] | InputStream | File} * * @param name1 - * The alternative name of the first attachment + * The alternative name of the first attachment * @param data1 - * The first attachment, must be of type {@code byte[] | InputStream | File} + * The first attachment, must be of type + * {@code byte[] | InputStream | File} * @param attachments - * Optional additional attachments to add, pairs of {@literal String->Data} + * Optional additional attachments to add, pairs of + * {@literal String->Data} * * @return A WebhookMessage for the attachments * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or more than {@value #MAX_FILES} - * or the additional arguments are not an even count or an invalid format + * If no attachments are provided or + * more than {@value #MAX_FILES} + * or the additional arguments are + * not an even count or an invalid + * format */ - @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. Edition - Item 53) + @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. + // Edition - Item 53) public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, @NotNull Object... attachments) { Objects.requireNonNull(name1, "Name"); Objects.requireNonNull(data1, "Data"); @@ -305,14 +352,17 @@ public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, throw new IllegalArgumentException("Must provide even number of varargs arguments"); int fileAmount = 1 + attachments.length / 2; if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); MessageAttachment[] files = new MessageAttachment[fileAmount]; files[0] = convertAttachment(name1, data1); for (int i = 0, j = 1; i < attachments.length; j++, i += 2) { Object name = attachments[i]; Object data = attachments[i + 1]; if (!(name instanceof String)) - throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Expected String and found " + (name == null ? null : name.getClass().getName())); + throw new IllegalArgumentException( + "Provided arguments must be pairs for (String, Data). Expected String and found " + + (name == null ? null : name.getClass().getName())); files[j] = convertAttachment((String) name, data); } return new WebhookMessage(null, null, null, null, false, files, AllowedMentions.all(), 0, null); @@ -329,7 +379,8 @@ public boolean isFile() { /** * Provides a {@link okhttp3.RequestBody} of this message. - *
This is used internally for executing webhooks through HTTP requests. + *
+ * This is used internally for executing webhooks through HTTP requests. * * @return The request body */ @@ -383,10 +434,11 @@ else if (data instanceof InputStream) else if (data instanceof byte[]) a = new MessageAttachment(name, (byte[]) data); else - throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Unexpected data type " + data.getClass().getName()); + throw new IllegalArgumentException( + "Provided arguments must be pairs for (String, Data). Unexpected data type " + + data.getClass().getName()); return a; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index b3d7cdd..18e6b71 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -114,19 +114,19 @@ public WebhookMessageBuilder resetEmbeds() { /** * The mention whitelist. - *
See {@link AllowedMentions} for more details. + *
+ * See {@link AllowedMentions} for more details. * - * @param mentions - * The mention whitelist + * @param mentions + * The mention whitelist * * @throws NullPointerException - * If provided null + * If provided null * * @return This builder for chaining convenience */ @NotNull - public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) - { + public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) { this.allowedMentions = Objects.requireNonNull(mentions); return this; } @@ -135,14 +135,16 @@ public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mention * Adds the provided embeds to the builder * * @param embeds - * The embeds to add + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than {@value WebhookMessage#MAX_EMBEDS} are added + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { @@ -159,15 +161,17 @@ public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { /** * Adds the provided embeds to the builder * - * @param embeds - * The embeds to add + * @param embeds + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than {@value WebhookMessage#MAX_EMBEDS} are added + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull Collection embeds) { @@ -184,13 +188,14 @@ public WebhookMessageBuilder addEmbeds(@NotNull CollectionEach message by a webhook can have a different user appearance. + *
+ * Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param username - * The (nullable) username to use + * @param username + * The (nullable) username to use * * @return This builder for chaining convenience */ @@ -244,12 +251,13 @@ public WebhookMessageBuilder setUsername(@Nullable String username) { /** * The avatar url to use for this message. - *
Each message by a webhook can have a different user appearance. + *
+ * Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param avatarUrl - * The (nullable) avatar url to use + * @param avatarUrl + * The (nullable) avatar url to use * * @return This builder for chaining convenience */ @@ -262,8 +270,8 @@ public WebhookMessageBuilder setAvatarUrl(@Nullable String avatarUrl) { /** * Whether this message should use Text-to-Speech (TTS) * - * @param tts - * True, if this message should use tts + * @param tts + * True, if this message should use tts * * @return This builder for chaining convenience */ @@ -274,10 +282,11 @@ public WebhookMessageBuilder setTTS(boolean tts) { } /** - * Whether the message should be ephemeral (only works for interaction webhooks). + * Whether the message should be ephemeral (only works for interaction + * webhooks). * - * @param ephemeral - * True if the message should be ephemeral, false otherwise + * @param ephemeral + * True if the message should be ephemeral, false otherwise * * @return This builder for chaining convenience */ @@ -290,17 +299,36 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { return this; } + /** + * Whether the message should be silent + * + * @param silent + * True if the message should be sent silently, false otherwise + * + * @return This builder for chaining convenience + */ + @NotNull + public WebhookMessageBuilder setSilent(boolean silent) { + if (silent) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return this; + } + /** * Adds the provided file as an attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * * @param file - * The file to attach + * The file to attach * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null */ @NotNull public WebhookMessageBuilder addFile(@NotNull File file) { @@ -310,15 +338,17 @@ public WebhookMessageBuilder addFile(@NotNull File file) { /** * Adds the provided file as an attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used instead - * @param file - * The file to attach + * @param name + * The alternative name that should be used instead + * @param file + * The file to attach * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -326,31 +356,34 @@ public WebhookMessageBuilder addFile(@NotNull File file) { public WebhookMessageBuilder addFile(@NotNull String name, @NotNull File file) { Objects.requireNonNull(file, "File"); Objects.requireNonNull(name, "Name"); - if (!file.exists() || !file.canRead()) throw new IllegalArgumentException("File must exist and be readable"); + if (!file.exists() || !file.canRead()) + throw new IllegalArgumentException("File must exist and be readable"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, file); files[fileIndex++] = attachment; return this; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Adds the provided data as a file attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -359,7 +392,8 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) Objects.requireNonNull(data, "Data"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; @@ -368,15 +402,17 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) /** * Adds the provided data as a file attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -385,24 +421,25 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull InputStream Objects.requireNonNull(data, "InputStream"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; return this; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Sets the provided name as the name for a newly created thread. - *
This is only valid for forum/media channels. + *
+ * This is only valid for forum/media channels. * - * @param name - * The name that should be used + * @param name + * The name that should be used * * @return This builder for chaining convenience */ @@ -416,7 +453,8 @@ public WebhookMessageBuilder setThreadName(@Nullable String name) { * Constructs the {@link club.minnced.discord.webhook.send.WebhookMessage} * from the current configurations. * - * @return The resulting {@link club.minnced.discord.webhook.send.WebhookMessage} + * @return The resulting + * {@link club.minnced.discord.webhook.send.WebhookMessage} */ @NotNull public WebhookMessage build() { @@ -426,7 +464,6 @@ public WebhookMessage build() { fileIndex == 0 ? null : Arrays.copyOf(files, fileIndex), allowedMentions, flags, threadName); } - ///////////////////////////////// /// Third-party compatibility /// ///////////////////////////////// @@ -434,11 +471,11 @@ public WebhookMessage build() { /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -450,11 +487,11 @@ public static WebhookMessageBuilder fromJDA(@NotNull net.dv8tion.jda.api.entitie /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -469,26 +506,25 @@ public static WebhookMessageBuilder fromJDA(@NotNull MessageCreateData message) Set mentionedUsers = message.getMentionedUsers(); Set mentionedRoles = message.getMentionedRoles(); builder.setAllowedMentions( - AllowedMentions.none() - .withUsers(mentionedUsers) - .withRoles(mentionedRoles) - .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) - .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) - .withParseUsers(allowedMentions.contains(Message.MentionType.USER)) - ); - + AllowedMentions.none() + .withUsers(mentionedUsers) + .withRoles(mentionedRoles) + .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) + .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) + .withParseUsers(allowedMentions.contains(Message.MentionType.USER))); return builder; } /** - * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} into a compatible WebhookMessageBuilder. + * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} + * into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -501,26 +537,27 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit AllowedMentions allowedMentions = AllowedMentions.none(); allowedMentions.withUsers( - message.getMentionedUsers().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedUsers().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withRoles( - message.getMentionedRoles().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedRoles().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withParseEveryone(message.mentionsEveryone()); builder.setAllowedMentions(allowedMentions); return builder; } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param callback - * The callback used to specify the desired message settings + * @param callback + * The callback used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data * @@ -529,17 +566,20 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit @NotNull @Deprecated public static WebhookMessageBuilder fromD4J(@NotNull Consumer callback) { - throw new UnsupportedOperationException("Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); + throw new UnsupportedOperationException( + "Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message settings + * @param spec + * The message create spec used to specify the desired message + * settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -563,11 +603,10 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { builder.setTTS(tts.get()); if (!embeds.isAbsent()) { builder.addEmbeds( - embeds.get().stream() - .map(WebhookEmbedBuilder::fromD4J) - .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList()) - ); + embeds.get().stream() + .map(WebhookEmbedBuilder::fromD4J) + .map(WebhookEmbedBuilder::build) + .collect(Collectors.toList())); } if (!allowedMentions.isAbsent()) { @@ -590,13 +629,15 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message settings + * @param spec + * The message create spec used to specify the desired message + * settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -620,8 +661,7 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageEditSpec spec) { embeds.get().get().stream() .map(WebhookEmbedBuilder::fromD4J) .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList()) - ); + .collect(Collectors.toList())); } if (!allowedMentions.isAbsent() && allowedMentions.get().isPresent()) { From fdcc8b00676cc863d2d8614660b9130499681bfc Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:29:22 -0800 Subject: [PATCH 2/6] Revert "added a setSilent method similar to the setEphmeral method" This reverts commit 897aa404ed700b3b23ac5a8c5a75fbf36f3ae893. reverted weird formatting changes --- .../minnced/discord/webhook/MessageFlags.java | 23 +- .../discord/webhook/send/WebhookMessage.java | 146 ++++------ .../webhook/send/WebhookMessageBuilder.java | 258 ++++++++---------- 3 files changed, 165 insertions(+), 262 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/MessageFlags.java b/src/main/java/club/minnced/discord/webhook/MessageFlags.java index 907487a..d9a0ffc 100644 --- a/src/main/java/club/minnced/discord/webhook/MessageFlags.java +++ b/src/main/java/club/minnced/discord/webhook/MessageFlags.java @@ -17,21 +17,16 @@ package club.minnced.discord.webhook; /** - * Constants for the message flags described by the Discord Documentation. + * Constants for the message flags described by the Discord Documentation. */ @SuppressWarnings("PointlessBitwiseExpression") public class MessageFlags { - public static final int CROSSPOSTED = 1 << 0; - public static final int IS_CROSSPOSTED = 1 << 1; - public static final int SUPPRESS_EMBEDS = 1 << 2; + public static final int CROSSPOSTED = 1 << 0; + public static final int IS_CROSSPOSTED = 1 << 1; + public static final int SUPPRESS_EMBEDS = 1 << 2; public static final int SOURCE_MESSAGE_DELETED = 1 << 3; - public static final int URGENT = 1 << 4; - public static final int HAS_THREAD = 1 << 5; - public static final int EPHEMERAL = 1 << 6; - public static final int LOADING = 1 << 7; - public static final int FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8; - public static final int SUPPRESS_NOTIFICATIONS = 1 << 12; - public static final int IS_VOICE_MESSAGE = 1 << 13; -} \ No newline at end of file + public static final int URGENT = 1 << 4; + public static final int HAS_THREAD = 1 << 5; + public static final int EPHEMERAL = 1 << 6; + public static final int LOADING = 1 << 7; +} diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 101bedf..78ce7c8 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -33,10 +33,8 @@ /** * Send-only message for a {@link club.minnced.discord.webhook.WebhookClient} - *
- * A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent - * by first converting it to a WebhookMessage with - * {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. + *
A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent + * by first converting it to a WebhookMessage with {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. */ public class WebhookMessage { /** @@ -55,9 +53,9 @@ public class WebhookMessage { protected final String threadName; protected WebhookMessage(final String username, final String avatarUrl, final String content, - final List embeds, final boolean isTTS, - final MessageAttachment[] files, final AllowedMentions allowedMentions, - final int flags, final String threadName) { + final List embeds, final boolean isTTS, + final MessageAttachment[] files, final AllowedMentions allowedMentions, + final int flags, final String threadName) { this.username = username; this.avatarUrl = avatarUrl; this.content = content; @@ -138,13 +136,11 @@ public int getFlags() { } /** - * Returns a new WebhookMessage instance with the ephemeral flag turned on/off - * (true/false). - *
- * This instance remains unchanged and a new instance is returned. + * Returns a new WebhookMessage instance with the ephemeral flag turned on/off (true/false). + *
This instance remains unchanged and a new instance is returned. * - * @param ephemeral - * Whether to make this message ephemeral + * @param ephemeral + * Whether to make this message ephemeral * * @return New WebhookMessage instance */ @@ -155,43 +151,19 @@ public WebhookMessage asEphemeral(boolean ephemeral) { flags |= MessageFlags.EPHEMERAL; else flags &= ~MessageFlags.EPHEMERAL; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, - threadName); - } - - /** - * Returns a new WebhookMessage instance with the silent flag turned on/off - * (true/false). - *
- * This instance remains unchanged and a new instance is returned. - * - * @param silent - * Whether to make this message silent - * - * @return New WebhookMessage instance - */ - @NotNull - public WebhookMessage asSilent(boolean silent) { - int flags = this.flags; - if (silent) - flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; - else - flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, - threadName); + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); } /** * Converts a {@link club.minnced.discord.webhook.receive.ReadonlyMessage} to a * WebhookMessage. - *
- * This does not convert attachments. + *
This does not convert attachments. * - * @param message - * The message to convert + * @param message + * The message to convert * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return A WebhookMessage copy */ @@ -204,7 +176,6 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { builder.setContent(message.getContent()); builder.setTTS(message.isTTS()); builder.setEphemeral((message.getFlags() & MessageFlags.EPHEMERAL) != 0); - builder.setSilent((message.getFlags() & MessageFlags.SUPPRESS_NOTIFICATIONS) != 0); builder.addEmbeds(message.getEmbeds()); return builder.build(); } @@ -214,21 +185,18 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * * @param first - * The first embed + * The first embed * @param embeds - * Optional additional embeds for the message + * Optional additional embeds for the message * * @return A WebhookMessage for the embeds * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are provided + * If more than {@value WebhookMessage#MAX_EMBEDS} are provided */ - @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. - // Edition - Item 53) + @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. Edition - Item 53) public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) { Objects.requireNonNull(embeds, "Embeds"); if (embeds.length >= WebhookMessage.MAX_EMBEDS) @@ -246,15 +214,13 @@ public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull Webhoo * Creates a WebhookMessage from * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * - * @param embeds - * Embeds for the message + * @param embeds + * Embeds for the message * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are provided + * If more than {@value WebhookMessage#MAX_EMBEDS} are provided * * @return A WebhookMessage for the embeds */ @@ -266,25 +232,22 @@ public static WebhookMessage embeds(@NotNull Collection embeds) { if (embeds.isEmpty()) throw new IllegalArgumentException("Cannot build an empty message"); embeds.forEach(Objects::requireNonNull); - return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, - null); + return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, null); } /** * Creates a WebhookMessage from the provided attachments. - *
- * A message can hold up to {@value #MAX_FILES} attachments + *
A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - * @param attachments - * The attachments to add, keys are the alternative names - * for each attachment + * @param attachments + * The attachments to add, keys are the alternative names + * for each attachment * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or - * more than {@value #MAX_FILES} + * If no attachments are provided or more than {@value #MAX_FILES} * * @return A WebhookMessage for the attachments */ @@ -296,8 +259,7 @@ public static WebhookMessage files(@NotNull Map attachments) { if (fileAmount == 0) throw new IllegalArgumentException("Cannot build an empty message"); if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); Set> entries = attachments.entrySet(); MessageAttachment[] files = new MessageAttachment[fileAmount]; int i = 0; @@ -312,38 +274,29 @@ public static WebhookMessage files(@NotNull Map attachments) { /** * Creates a WebhookMessage from the provided attachments. - *
- * A message can hold up to {@value #MAX_FILES} attachments + *
A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - *

- * The files are provided in pairs of {@literal Name->Data} similar + *

The files are provided in pairs of {@literal Name->Data} similar * to the first 2 arguments. - *
- * The allowed data types are {@code byte[] | InputStream | File} + *
The allowed data types are {@code byte[] | InputStream | File} * * @param name1 - * The alternative name of the first attachment + * The alternative name of the first attachment * @param data1 - * The first attachment, must be of type - * {@code byte[] | InputStream | File} + * The first attachment, must be of type {@code byte[] | InputStream | File} * @param attachments - * Optional additional attachments to add, pairs of - * {@literal String->Data} + * Optional additional attachments to add, pairs of {@literal String->Data} * * @return A WebhookMessage for the attachments * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or - * more than {@value #MAX_FILES} - * or the additional arguments are - * not an even count or an invalid - * format + * If no attachments are provided or more than {@value #MAX_FILES} + * or the additional arguments are not an even count or an invalid format */ - @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. - // Edition - Item 53) + @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. Edition - Item 53) public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, @NotNull Object... attachments) { Objects.requireNonNull(name1, "Name"); Objects.requireNonNull(data1, "Data"); @@ -352,17 +305,14 @@ public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, throw new IllegalArgumentException("Must provide even number of varargs arguments"); int fileAmount = 1 + attachments.length / 2; if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); MessageAttachment[] files = new MessageAttachment[fileAmount]; files[0] = convertAttachment(name1, data1); for (int i = 0, j = 1; i < attachments.length; j++, i += 2) { Object name = attachments[i]; Object data = attachments[i + 1]; if (!(name instanceof String)) - throw new IllegalArgumentException( - "Provided arguments must be pairs for (String, Data). Expected String and found " - + (name == null ? null : name.getClass().getName())); + throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Expected String and found " + (name == null ? null : name.getClass().getName())); files[j] = convertAttachment((String) name, data); } return new WebhookMessage(null, null, null, null, false, files, AllowedMentions.all(), 0, null); @@ -379,8 +329,7 @@ public boolean isFile() { /** * Provides a {@link okhttp3.RequestBody} of this message. - *
- * This is used internally for executing webhooks through HTTP requests. + *
This is used internally for executing webhooks through HTTP requests. * * @return The request body */ @@ -434,11 +383,10 @@ else if (data instanceof InputStream) else if (data instanceof byte[]) a = new MessageAttachment(name, (byte[]) data); else - throw new IllegalArgumentException( - "Provided arguments must be pairs for (String, Data). Unexpected data type " - + data.getClass().getName()); + throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Unexpected data type " + data.getClass().getName()); return a; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index 18e6b71..b3d7cdd 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -114,19 +114,19 @@ public WebhookMessageBuilder resetEmbeds() { /** * The mention whitelist. - *
- * See {@link AllowedMentions} for more details. + *
See {@link AllowedMentions} for more details. * - * @param mentions - * The mention whitelist + * @param mentions + * The mention whitelist * * @throws NullPointerException - * If provided null + * If provided null * * @return This builder for chaining convenience */ @NotNull - public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) { + public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) + { this.allowedMentions = Objects.requireNonNull(mentions); return this; } @@ -135,16 +135,14 @@ public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mention * Adds the provided embeds to the builder * * @param embeds - * The embeds to add + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are added + * If more than {@value WebhookMessage#MAX_EMBEDS} are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { @@ -161,17 +159,15 @@ public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { /** * Adds the provided embeds to the builder * - * @param embeds - * The embeds to add + * @param embeds + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are added + * If more than {@value WebhookMessage#MAX_EMBEDS} are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull Collection embeds) { @@ -188,14 +184,13 @@ public WebhookMessageBuilder addEmbeds(@NotNull Collection - * Each message by a webhook can have a different user appearance. + *
Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param username - * The (nullable) username to use + * @param username + * The (nullable) username to use * * @return This builder for chaining convenience */ @@ -251,13 +244,12 @@ public WebhookMessageBuilder setUsername(@Nullable String username) { /** * The avatar url to use for this message. - *
- * Each message by a webhook can have a different user appearance. + *
Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param avatarUrl - * The (nullable) avatar url to use + * @param avatarUrl + * The (nullable) avatar url to use * * @return This builder for chaining convenience */ @@ -270,8 +262,8 @@ public WebhookMessageBuilder setAvatarUrl(@Nullable String avatarUrl) { /** * Whether this message should use Text-to-Speech (TTS) * - * @param tts - * True, if this message should use tts + * @param tts + * True, if this message should use tts * * @return This builder for chaining convenience */ @@ -282,11 +274,10 @@ public WebhookMessageBuilder setTTS(boolean tts) { } /** - * Whether the message should be ephemeral (only works for interaction - * webhooks). + * Whether the message should be ephemeral (only works for interaction webhooks). * - * @param ephemeral - * True if the message should be ephemeral, false otherwise + * @param ephemeral + * True if the message should be ephemeral, false otherwise * * @return This builder for chaining convenience */ @@ -299,36 +290,17 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { return this; } - /** - * Whether the message should be silent - * - * @param silent - * True if the message should be sent silently, false otherwise - * - * @return This builder for chaining convenience - */ - @NotNull - public WebhookMessageBuilder setSilent(boolean silent) { - if (silent) - flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; - else - flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; - return this; - } - /** * Adds the provided file as an attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * * @param file - * The file to attach + * The file to attach * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null */ @NotNull public WebhookMessageBuilder addFile(@NotNull File file) { @@ -338,17 +310,15 @@ public WebhookMessageBuilder addFile(@NotNull File file) { /** * Adds the provided file as an attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used instead - * @param file - * The file to attach + * @param name + * The alternative name that should be used instead + * @param file + * The file to attach * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -356,34 +326,31 @@ public WebhookMessageBuilder addFile(@NotNull File file) { public WebhookMessageBuilder addFile(@NotNull String name, @NotNull File file) { Objects.requireNonNull(file, "File"); Objects.requireNonNull(name, "Name"); - if (!file.exists() || !file.canRead()) - throw new IllegalArgumentException("File must exist and be readable"); + if (!file.exists() || !file.canRead()) throw new IllegalArgumentException("File must exist and be readable"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, file); files[fileIndex++] = attachment; return this; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Adds the provided data as a file attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -392,8 +359,7 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) Objects.requireNonNull(data, "Data"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; @@ -402,17 +368,15 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) /** * Adds the provided data as a file attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -421,25 +385,24 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull InputStream Objects.requireNonNull(data, "InputStream"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; return this; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Sets the provided name as the name for a newly created thread. - *
- * This is only valid for forum/media channels. + *
This is only valid for forum/media channels. * - * @param name - * The name that should be used + * @param name + * The name that should be used * * @return This builder for chaining convenience */ @@ -453,8 +416,7 @@ public WebhookMessageBuilder setThreadName(@Nullable String name) { * Constructs the {@link club.minnced.discord.webhook.send.WebhookMessage} * from the current configurations. * - * @return The resulting - * {@link club.minnced.discord.webhook.send.WebhookMessage} + * @return The resulting {@link club.minnced.discord.webhook.send.WebhookMessage} */ @NotNull public WebhookMessage build() { @@ -464,6 +426,7 @@ public WebhookMessage build() { fileIndex == 0 ? null : Arrays.copyOf(files, fileIndex), allowedMentions, flags, threadName); } + ///////////////////////////////// /// Third-party compatibility /// ///////////////////////////////// @@ -471,11 +434,11 @@ public WebhookMessage build() { /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -487,11 +450,11 @@ public static WebhookMessageBuilder fromJDA(@NotNull net.dv8tion.jda.api.entitie /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -506,25 +469,26 @@ public static WebhookMessageBuilder fromJDA(@NotNull MessageCreateData message) Set mentionedUsers = message.getMentionedUsers(); Set mentionedRoles = message.getMentionedRoles(); builder.setAllowedMentions( - AllowedMentions.none() - .withUsers(mentionedUsers) - .withRoles(mentionedRoles) - .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) - .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) - .withParseUsers(allowedMentions.contains(Message.MentionType.USER))); + AllowedMentions.none() + .withUsers(mentionedUsers) + .withRoles(mentionedRoles) + .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) + .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) + .withParseUsers(allowedMentions.contains(Message.MentionType.USER)) + ); + return builder; } /** - * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} - * into a compatible WebhookMessageBuilder. + * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -537,27 +501,26 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit AllowedMentions allowedMentions = AllowedMentions.none(); allowedMentions.withUsers( - message.getMentionedUsers().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedUsers().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withRoles( - message.getMentionedRoles().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedRoles().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withParseEveryone(message.mentionsEveryone()); builder.setAllowedMentions(allowedMentions); return builder; } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param callback - * The callback used to specify the desired message settings + * @param callback + * The callback used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data * @@ -566,20 +529,17 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit @NotNull @Deprecated public static WebhookMessageBuilder fromD4J(@NotNull Consumer callback) { - throw new UnsupportedOperationException( - "Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); + throw new UnsupportedOperationException("Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message - * settings + * @param spec + * The message create spec used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -603,10 +563,11 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { builder.setTTS(tts.get()); if (!embeds.isAbsent()) { builder.addEmbeds( - embeds.get().stream() - .map(WebhookEmbedBuilder::fromD4J) - .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList())); + embeds.get().stream() + .map(WebhookEmbedBuilder::fromD4J) + .map(WebhookEmbedBuilder::build) + .collect(Collectors.toList()) + ); } if (!allowedMentions.isAbsent()) { @@ -629,15 +590,13 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message - * settings + * @param spec + * The message create spec used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -661,7 +620,8 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageEditSpec spec) { embeds.get().get().stream() .map(WebhookEmbedBuilder::fromD4J) .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } if (!allowedMentions.isAbsent() && allowedMentions.get().isPresent()) { From d5c8baf47bccbcd04af46b4f245f3166b2d06d5b Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:34:25 -0800 Subject: [PATCH 3/6] added a setSilent method --- .../minnced/discord/webhook/MessageFlags.java | 23 +- .../discord/webhook/send/WebhookMessage.java | 146 ++++++---- .../webhook/send/WebhookMessageBuilder.java | 258 ++++++++++-------- 3 files changed, 262 insertions(+), 165 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/MessageFlags.java b/src/main/java/club/minnced/discord/webhook/MessageFlags.java index d9a0ffc..907487a 100644 --- a/src/main/java/club/minnced/discord/webhook/MessageFlags.java +++ b/src/main/java/club/minnced/discord/webhook/MessageFlags.java @@ -17,16 +17,21 @@ package club.minnced.discord.webhook; /** - * Constants for the message flags described by the Discord Documentation. + * Constants for the message flags described by the Discord Documentation. */ @SuppressWarnings("PointlessBitwiseExpression") public class MessageFlags { - public static final int CROSSPOSTED = 1 << 0; - public static final int IS_CROSSPOSTED = 1 << 1; - public static final int SUPPRESS_EMBEDS = 1 << 2; + public static final int CROSSPOSTED = 1 << 0; + public static final int IS_CROSSPOSTED = 1 << 1; + public static final int SUPPRESS_EMBEDS = 1 << 2; public static final int SOURCE_MESSAGE_DELETED = 1 << 3; - public static final int URGENT = 1 << 4; - public static final int HAS_THREAD = 1 << 5; - public static final int EPHEMERAL = 1 << 6; - public static final int LOADING = 1 << 7; -} + public static final int URGENT = 1 << 4; + public static final int HAS_THREAD = 1 << 5; + public static final int EPHEMERAL = 1 << 6; + public static final int LOADING = 1 << 7; + public static final int FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8; + public static final int SUPPRESS_NOTIFICATIONS = 1 << 12; + public static final int IS_VOICE_MESSAGE = 1 << 13; +} \ No newline at end of file diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 78ce7c8..101bedf 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -33,8 +33,10 @@ /** * Send-only message for a {@link club.minnced.discord.webhook.WebhookClient} - *
A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent - * by first converting it to a WebhookMessage with {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. + *
+ * A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent + * by first converting it to a WebhookMessage with + * {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. */ public class WebhookMessage { /** @@ -53,9 +55,9 @@ public class WebhookMessage { protected final String threadName; protected WebhookMessage(final String username, final String avatarUrl, final String content, - final List embeds, final boolean isTTS, - final MessageAttachment[] files, final AllowedMentions allowedMentions, - final int flags, final String threadName) { + final List embeds, final boolean isTTS, + final MessageAttachment[] files, final AllowedMentions allowedMentions, + final int flags, final String threadName) { this.username = username; this.avatarUrl = avatarUrl; this.content = content; @@ -136,11 +138,13 @@ public int getFlags() { } /** - * Returns a new WebhookMessage instance with the ephemeral flag turned on/off (true/false). - *
This instance remains unchanged and a new instance is returned. + * Returns a new WebhookMessage instance with the ephemeral flag turned on/off + * (true/false). + *
+ * This instance remains unchanged and a new instance is returned. * - * @param ephemeral - * Whether to make this message ephemeral + * @param ephemeral + * Whether to make this message ephemeral * * @return New WebhookMessage instance */ @@ -151,19 +155,43 @@ public WebhookMessage asEphemeral(boolean ephemeral) { flags |= MessageFlags.EPHEMERAL; else flags &= ~MessageFlags.EPHEMERAL; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, + threadName); + } + + /** + * Returns a new WebhookMessage instance with the silent flag turned on/off + * (true/false). + *
+ * This instance remains unchanged and a new instance is returned. + * + * @param silent + * Whether to make this message silent + * + * @return New WebhookMessage instance + */ + @NotNull + public WebhookMessage asSilent(boolean silent) { + int flags = this.flags; + if (silent) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, + threadName); } /** * Converts a {@link club.minnced.discord.webhook.receive.ReadonlyMessage} to a * WebhookMessage. - *
This does not convert attachments. + *
+ * This does not convert attachments. * - * @param message - * The message to convert + * @param message + * The message to convert * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return A WebhookMessage copy */ @@ -176,6 +204,7 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { builder.setContent(message.getContent()); builder.setTTS(message.isTTS()); builder.setEphemeral((message.getFlags() & MessageFlags.EPHEMERAL) != 0); + builder.setSilent((message.getFlags() & MessageFlags.SUPPRESS_NOTIFICATIONS) != 0); builder.addEmbeds(message.getEmbeds()); return builder.build(); } @@ -185,18 +214,21 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * * @param first - * The first embed + * The first embed * @param embeds - * Optional additional embeds for the message + * Optional additional embeds for the message * * @return A WebhookMessage for the embeds * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than {@value WebhookMessage#MAX_EMBEDS} are provided + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are provided */ - @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. Edition - Item 53) + @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. + // Edition - Item 53) public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) { Objects.requireNonNull(embeds, "Embeds"); if (embeds.length >= WebhookMessage.MAX_EMBEDS) @@ -214,13 +246,15 @@ public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull Webhoo * Creates a WebhookMessage from * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * - * @param embeds - * Embeds for the message + * @param embeds + * Embeds for the message * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than {@value WebhookMessage#MAX_EMBEDS} are provided + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are provided * * @return A WebhookMessage for the embeds */ @@ -232,22 +266,25 @@ public static WebhookMessage embeds(@NotNull Collection embeds) { if (embeds.isEmpty()) throw new IllegalArgumentException("Cannot build an empty message"); embeds.forEach(Objects::requireNonNull); - return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, null); + return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, + null); } /** * Creates a WebhookMessage from the provided attachments. - *
A message can hold up to {@value #MAX_FILES} attachments + *
+ * A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - * @param attachments - * The attachments to add, keys are the alternative names - * for each attachment + * @param attachments + * The attachments to add, keys are the alternative names + * for each attachment * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or more than {@value #MAX_FILES} + * If no attachments are provided or + * more than {@value #MAX_FILES} * * @return A WebhookMessage for the attachments */ @@ -259,7 +296,8 @@ public static WebhookMessage files(@NotNull Map attachments) { if (fileAmount == 0) throw new IllegalArgumentException("Cannot build an empty message"); if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); Set> entries = attachments.entrySet(); MessageAttachment[] files = new MessageAttachment[fileAmount]; int i = 0; @@ -274,29 +312,38 @@ public static WebhookMessage files(@NotNull Map attachments) { /** * Creates a WebhookMessage from the provided attachments. - *
A message can hold up to {@value #MAX_FILES} attachments + *
+ * A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - *

The files are provided in pairs of {@literal Name->Data} similar + *

+ * The files are provided in pairs of {@literal Name->Data} similar * to the first 2 arguments. - *
The allowed data types are {@code byte[] | InputStream | File} + *
+ * The allowed data types are {@code byte[] | InputStream | File} * * @param name1 - * The alternative name of the first attachment + * The alternative name of the first attachment * @param data1 - * The first attachment, must be of type {@code byte[] | InputStream | File} + * The first attachment, must be of type + * {@code byte[] | InputStream | File} * @param attachments - * Optional additional attachments to add, pairs of {@literal String->Data} + * Optional additional attachments to add, pairs of + * {@literal String->Data} * * @return A WebhookMessage for the attachments * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or more than {@value #MAX_FILES} - * or the additional arguments are not an even count or an invalid format + * If no attachments are provided or + * more than {@value #MAX_FILES} + * or the additional arguments are + * not an even count or an invalid + * format */ - @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. Edition - Item 53) + @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. + // Edition - Item 53) public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, @NotNull Object... attachments) { Objects.requireNonNull(name1, "Name"); Objects.requireNonNull(data1, "Data"); @@ -305,14 +352,17 @@ public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, throw new IllegalArgumentException("Must provide even number of varargs arguments"); int fileAmount = 1 + attachments.length / 2; if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); MessageAttachment[] files = new MessageAttachment[fileAmount]; files[0] = convertAttachment(name1, data1); for (int i = 0, j = 1; i < attachments.length; j++, i += 2) { Object name = attachments[i]; Object data = attachments[i + 1]; if (!(name instanceof String)) - throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Expected String and found " + (name == null ? null : name.getClass().getName())); + throw new IllegalArgumentException( + "Provided arguments must be pairs for (String, Data). Expected String and found " + + (name == null ? null : name.getClass().getName())); files[j] = convertAttachment((String) name, data); } return new WebhookMessage(null, null, null, null, false, files, AllowedMentions.all(), 0, null); @@ -329,7 +379,8 @@ public boolean isFile() { /** * Provides a {@link okhttp3.RequestBody} of this message. - *
This is used internally for executing webhooks through HTTP requests. + *
+ * This is used internally for executing webhooks through HTTP requests. * * @return The request body */ @@ -383,10 +434,11 @@ else if (data instanceof InputStream) else if (data instanceof byte[]) a = new MessageAttachment(name, (byte[]) data); else - throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Unexpected data type " + data.getClass().getName()); + throw new IllegalArgumentException( + "Provided arguments must be pairs for (String, Data). Unexpected data type " + + data.getClass().getName()); return a; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index b3d7cdd..18e6b71 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -114,19 +114,19 @@ public WebhookMessageBuilder resetEmbeds() { /** * The mention whitelist. - *
See {@link AllowedMentions} for more details. + *
+ * See {@link AllowedMentions} for more details. * - * @param mentions - * The mention whitelist + * @param mentions + * The mention whitelist * * @throws NullPointerException - * If provided null + * If provided null * * @return This builder for chaining convenience */ @NotNull - public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) - { + public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) { this.allowedMentions = Objects.requireNonNull(mentions); return this; } @@ -135,14 +135,16 @@ public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mention * Adds the provided embeds to the builder * * @param embeds - * The embeds to add + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than {@value WebhookMessage#MAX_EMBEDS} are added + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { @@ -159,15 +161,17 @@ public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { /** * Adds the provided embeds to the builder * - * @param embeds - * The embeds to add + * @param embeds + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than {@value WebhookMessage#MAX_EMBEDS} are added + * If more than + * {@value WebhookMessage#MAX_EMBEDS} + * are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull Collection embeds) { @@ -184,13 +188,14 @@ public WebhookMessageBuilder addEmbeds(@NotNull CollectionEach message by a webhook can have a different user appearance. + *
+ * Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param username - * The (nullable) username to use + * @param username + * The (nullable) username to use * * @return This builder for chaining convenience */ @@ -244,12 +251,13 @@ public WebhookMessageBuilder setUsername(@Nullable String username) { /** * The avatar url to use for this message. - *
Each message by a webhook can have a different user appearance. + *
+ * Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param avatarUrl - * The (nullable) avatar url to use + * @param avatarUrl + * The (nullable) avatar url to use * * @return This builder for chaining convenience */ @@ -262,8 +270,8 @@ public WebhookMessageBuilder setAvatarUrl(@Nullable String avatarUrl) { /** * Whether this message should use Text-to-Speech (TTS) * - * @param tts - * True, if this message should use tts + * @param tts + * True, if this message should use tts * * @return This builder for chaining convenience */ @@ -274,10 +282,11 @@ public WebhookMessageBuilder setTTS(boolean tts) { } /** - * Whether the message should be ephemeral (only works for interaction webhooks). + * Whether the message should be ephemeral (only works for interaction + * webhooks). * - * @param ephemeral - * True if the message should be ephemeral, false otherwise + * @param ephemeral + * True if the message should be ephemeral, false otherwise * * @return This builder for chaining convenience */ @@ -290,17 +299,36 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { return this; } + /** + * Whether the message should be silent + * + * @param silent + * True if the message should be sent silently, false otherwise + * + * @return This builder for chaining convenience + */ + @NotNull + public WebhookMessageBuilder setSilent(boolean silent) { + if (silent) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return this; + } + /** * Adds the provided file as an attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * * @param file - * The file to attach + * The file to attach * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null */ @NotNull public WebhookMessageBuilder addFile(@NotNull File file) { @@ -310,15 +338,17 @@ public WebhookMessageBuilder addFile(@NotNull File file) { /** * Adds the provided file as an attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used instead - * @param file - * The file to attach + * @param name + * The alternative name that should be used instead + * @param file + * The file to attach * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -326,31 +356,34 @@ public WebhookMessageBuilder addFile(@NotNull File file) { public WebhookMessageBuilder addFile(@NotNull String name, @NotNull File file) { Objects.requireNonNull(file, "File"); Objects.requireNonNull(name, "Name"); - if (!file.exists() || !file.canRead()) throw new IllegalArgumentException("File must exist and be readable"); + if (!file.exists() || !file.canRead()) + throw new IllegalArgumentException("File must exist and be readable"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, file); files[fileIndex++] = attachment; return this; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Adds the provided data as a file attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -359,7 +392,8 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) Objects.requireNonNull(data, "Data"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; @@ -368,15 +402,17 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) /** * Adds the provided data as a file attachment to this message. - *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. + *
+ * A single message can have up to {@value WebhookMessage#MAX_FILES} + * attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -385,24 +421,25 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull InputStream Objects.requireNonNull(data, "InputStream"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException( + "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; return this; - } - catch (IOException ex) { + } catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Sets the provided name as the name for a newly created thread. - *
This is only valid for forum/media channels. + *
+ * This is only valid for forum/media channels. * - * @param name - * The name that should be used + * @param name + * The name that should be used * * @return This builder for chaining convenience */ @@ -416,7 +453,8 @@ public WebhookMessageBuilder setThreadName(@Nullable String name) { * Constructs the {@link club.minnced.discord.webhook.send.WebhookMessage} * from the current configurations. * - * @return The resulting {@link club.minnced.discord.webhook.send.WebhookMessage} + * @return The resulting + * {@link club.minnced.discord.webhook.send.WebhookMessage} */ @NotNull public WebhookMessage build() { @@ -426,7 +464,6 @@ public WebhookMessage build() { fileIndex == 0 ? null : Arrays.copyOf(files, fileIndex), allowedMentions, flags, threadName); } - ///////////////////////////////// /// Third-party compatibility /// ///////////////////////////////// @@ -434,11 +471,11 @@ public WebhookMessage build() { /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -450,11 +487,11 @@ public static WebhookMessageBuilder fromJDA(@NotNull net.dv8tion.jda.api.entitie /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -469,26 +506,25 @@ public static WebhookMessageBuilder fromJDA(@NotNull MessageCreateData message) Set mentionedUsers = message.getMentionedUsers(); Set mentionedRoles = message.getMentionedRoles(); builder.setAllowedMentions( - AllowedMentions.none() - .withUsers(mentionedUsers) - .withRoles(mentionedRoles) - .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) - .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) - .withParseUsers(allowedMentions.contains(Message.MentionType.USER)) - ); - + AllowedMentions.none() + .withUsers(mentionedUsers) + .withRoles(mentionedRoles) + .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) + .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) + .withParseUsers(allowedMentions.contains(Message.MentionType.USER))); return builder; } /** - * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} into a compatible WebhookMessageBuilder. + * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} + * into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -501,26 +537,27 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit AllowedMentions allowedMentions = AllowedMentions.none(); allowedMentions.withUsers( - message.getMentionedUsers().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedUsers().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withRoles( - message.getMentionedRoles().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedRoles().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withParseEveryone(message.mentionsEveryone()); builder.setAllowedMentions(allowedMentions); return builder; } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param callback - * The callback used to specify the desired message settings + * @param callback + * The callback used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data * @@ -529,17 +566,20 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit @NotNull @Deprecated public static WebhookMessageBuilder fromD4J(@NotNull Consumer callback) { - throw new UnsupportedOperationException("Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); + throw new UnsupportedOperationException( + "Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message settings + * @param spec + * The message create spec used to specify the desired message + * settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -563,11 +603,10 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { builder.setTTS(tts.get()); if (!embeds.isAbsent()) { builder.addEmbeds( - embeds.get().stream() - .map(WebhookEmbedBuilder::fromD4J) - .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList()) - ); + embeds.get().stream() + .map(WebhookEmbedBuilder::fromD4J) + .map(WebhookEmbedBuilder::build) + .collect(Collectors.toList())); } if (!allowedMentions.isAbsent()) { @@ -590,13 +629,15 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible + * WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message settings + * @param spec + * The message create spec used to specify the desired message + * settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -620,8 +661,7 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageEditSpec spec) { embeds.get().get().stream() .map(WebhookEmbedBuilder::fromD4J) .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList()) - ); + .collect(Collectors.toList())); } if (!allowedMentions.isAbsent() && allowedMentions.get().isPresent()) { From b9b73a432ccd6632c182fdaf1142dea736004797 Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:38:53 -0800 Subject: [PATCH 4/6] Revert "added a setSilent method" This reverts commit d5c8baf47bccbcd04af46b4f245f3166b2d06d5b. --- .../minnced/discord/webhook/MessageFlags.java | 23 +- .../discord/webhook/send/WebhookMessage.java | 146 ++++------ .../webhook/send/WebhookMessageBuilder.java | 258 ++++++++---------- 3 files changed, 165 insertions(+), 262 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/MessageFlags.java b/src/main/java/club/minnced/discord/webhook/MessageFlags.java index 907487a..d9a0ffc 100644 --- a/src/main/java/club/minnced/discord/webhook/MessageFlags.java +++ b/src/main/java/club/minnced/discord/webhook/MessageFlags.java @@ -17,21 +17,16 @@ package club.minnced.discord.webhook; /** - * Constants for the message flags described by the Discord Documentation. + * Constants for the message flags described by the Discord Documentation. */ @SuppressWarnings("PointlessBitwiseExpression") public class MessageFlags { - public static final int CROSSPOSTED = 1 << 0; - public static final int IS_CROSSPOSTED = 1 << 1; - public static final int SUPPRESS_EMBEDS = 1 << 2; + public static final int CROSSPOSTED = 1 << 0; + public static final int IS_CROSSPOSTED = 1 << 1; + public static final int SUPPRESS_EMBEDS = 1 << 2; public static final int SOURCE_MESSAGE_DELETED = 1 << 3; - public static final int URGENT = 1 << 4; - public static final int HAS_THREAD = 1 << 5; - public static final int EPHEMERAL = 1 << 6; - public static final int LOADING = 1 << 7; - public static final int FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8; - public static final int SUPPRESS_NOTIFICATIONS = 1 << 12; - public static final int IS_VOICE_MESSAGE = 1 << 13; -} \ No newline at end of file + public static final int URGENT = 1 << 4; + public static final int HAS_THREAD = 1 << 5; + public static final int EPHEMERAL = 1 << 6; + public static final int LOADING = 1 << 7; +} diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 101bedf..78ce7c8 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -33,10 +33,8 @@ /** * Send-only message for a {@link club.minnced.discord.webhook.WebhookClient} - *
- * A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent - * by first converting it to a WebhookMessage with - * {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. + *
A {@link club.minnced.discord.webhook.receive.ReadonlyMessage} can be sent + * by first converting it to a WebhookMessage with {@link #from(club.minnced.discord.webhook.receive.ReadonlyMessage)}. */ public class WebhookMessage { /** @@ -55,9 +53,9 @@ public class WebhookMessage { protected final String threadName; protected WebhookMessage(final String username, final String avatarUrl, final String content, - final List embeds, final boolean isTTS, - final MessageAttachment[] files, final AllowedMentions allowedMentions, - final int flags, final String threadName) { + final List embeds, final boolean isTTS, + final MessageAttachment[] files, final AllowedMentions allowedMentions, + final int flags, final String threadName) { this.username = username; this.avatarUrl = avatarUrl; this.content = content; @@ -138,13 +136,11 @@ public int getFlags() { } /** - * Returns a new WebhookMessage instance with the ephemeral flag turned on/off - * (true/false). - *
- * This instance remains unchanged and a new instance is returned. + * Returns a new WebhookMessage instance with the ephemeral flag turned on/off (true/false). + *
This instance remains unchanged and a new instance is returned. * - * @param ephemeral - * Whether to make this message ephemeral + * @param ephemeral + * Whether to make this message ephemeral * * @return New WebhookMessage instance */ @@ -155,43 +151,19 @@ public WebhookMessage asEphemeral(boolean ephemeral) { flags |= MessageFlags.EPHEMERAL; else flags &= ~MessageFlags.EPHEMERAL; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, - threadName); - } - - /** - * Returns a new WebhookMessage instance with the silent flag turned on/off - * (true/false). - *
- * This instance remains unchanged and a new instance is returned. - * - * @param silent - * Whether to make this message silent - * - * @return New WebhookMessage instance - */ - @NotNull - public WebhookMessage asSilent(boolean silent) { - int flags = this.flags; - if (silent) - flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; - else - flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; - return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, - threadName); + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); } /** * Converts a {@link club.minnced.discord.webhook.receive.ReadonlyMessage} to a * WebhookMessage. - *
- * This does not convert attachments. + *
This does not convert attachments. * - * @param message - * The message to convert + * @param message + * The message to convert * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return A WebhookMessage copy */ @@ -204,7 +176,6 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { builder.setContent(message.getContent()); builder.setTTS(message.isTTS()); builder.setEphemeral((message.getFlags() & MessageFlags.EPHEMERAL) != 0); - builder.setSilent((message.getFlags() & MessageFlags.SUPPRESS_NOTIFICATIONS) != 0); builder.addEmbeds(message.getEmbeds()); return builder.build(); } @@ -214,21 +185,18 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * * @param first - * The first embed + * The first embed * @param embeds - * Optional additional embeds for the message + * Optional additional embeds for the message * * @return A WebhookMessage for the embeds * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are provided + * If more than {@value WebhookMessage#MAX_EMBEDS} are provided */ - @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. - // Edition - Item 53) + @NotNull // forcing first embed as we expect at least one entry (Effective Java 3rd. Edition - Item 53) public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) { Objects.requireNonNull(embeds, "Embeds"); if (embeds.length >= WebhookMessage.MAX_EMBEDS) @@ -246,15 +214,13 @@ public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull Webhoo * Creates a WebhookMessage from * the provided embeds. A message can hold up to {@value #MAX_EMBEDS} embeds. * - * @param embeds - * Embeds for the message + * @param embeds + * Embeds for the message * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are provided + * If more than {@value WebhookMessage#MAX_EMBEDS} are provided * * @return A WebhookMessage for the embeds */ @@ -266,25 +232,22 @@ public static WebhookMessage embeds(@NotNull Collection embeds) { if (embeds.isEmpty()) throw new IllegalArgumentException("Cannot build an empty message"); embeds.forEach(Objects::requireNonNull); - return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, - null); + return new WebhookMessage(null, null, null, new ArrayList<>(embeds), false, null, AllowedMentions.all(), 0, null); } /** * Creates a WebhookMessage from the provided attachments. - *
- * A message can hold up to {@value #MAX_FILES} attachments + *
A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - * @param attachments - * The attachments to add, keys are the alternative names - * for each attachment + * @param attachments + * The attachments to add, keys are the alternative names + * for each attachment * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or - * more than {@value #MAX_FILES} + * If no attachments are provided or more than {@value #MAX_FILES} * * @return A WebhookMessage for the attachments */ @@ -296,8 +259,7 @@ public static WebhookMessage files(@NotNull Map attachments) { if (fileAmount == 0) throw new IllegalArgumentException("Cannot build an empty message"); if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); Set> entries = attachments.entrySet(); MessageAttachment[] files = new MessageAttachment[fileAmount]; int i = 0; @@ -312,38 +274,29 @@ public static WebhookMessage files(@NotNull Map attachments) { /** * Creates a WebhookMessage from the provided attachments. - *
- * A message can hold up to {@value #MAX_FILES} attachments + *
A message can hold up to {@value #MAX_FILES} attachments * and a total of 8MiB of data. * - *

- * The files are provided in pairs of {@literal Name->Data} similar + *

The files are provided in pairs of {@literal Name->Data} similar * to the first 2 arguments. - *
- * The allowed data types are {@code byte[] | InputStream | File} + *
The allowed data types are {@code byte[] | InputStream | File} * * @param name1 - * The alternative name of the first attachment + * The alternative name of the first attachment * @param data1 - * The first attachment, must be of type - * {@code byte[] | InputStream | File} + * The first attachment, must be of type {@code byte[] | InputStream | File} * @param attachments - * Optional additional attachments to add, pairs of - * {@literal String->Data} + * Optional additional attachments to add, pairs of {@literal String->Data} * * @return A WebhookMessage for the attachments * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalArgumentException - * If no attachments are provided or - * more than {@value #MAX_FILES} - * or the additional arguments are - * not an even count or an invalid - * format + * If no attachments are provided or more than {@value #MAX_FILES} + * or the additional arguments are not an even count or an invalid format */ - @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. - // Edition - Item 53) + @NotNull // forcing first pair as we expect at least one entry (Effective Java 3rd. Edition - Item 53) public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, @NotNull Object... attachments) { Objects.requireNonNull(name1, "Name"); Objects.requireNonNull(data1, "Data"); @@ -352,17 +305,14 @@ public static WebhookMessage files(@NotNull String name1, @NotNull Object data1, throw new IllegalArgumentException("Must provide even number of varargs arguments"); int fileAmount = 1 + attachments.length / 2; if (fileAmount > WebhookMessage.MAX_FILES) - throw new IllegalArgumentException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); + throw new IllegalArgumentException("Cannot add more than " + WebhookMessage.MAX_FILES + " files to a message"); MessageAttachment[] files = new MessageAttachment[fileAmount]; files[0] = convertAttachment(name1, data1); for (int i = 0, j = 1; i < attachments.length; j++, i += 2) { Object name = attachments[i]; Object data = attachments[i + 1]; if (!(name instanceof String)) - throw new IllegalArgumentException( - "Provided arguments must be pairs for (String, Data). Expected String and found " - + (name == null ? null : name.getClass().getName())); + throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Expected String and found " + (name == null ? null : name.getClass().getName())); files[j] = convertAttachment((String) name, data); } return new WebhookMessage(null, null, null, null, false, files, AllowedMentions.all(), 0, null); @@ -379,8 +329,7 @@ public boolean isFile() { /** * Provides a {@link okhttp3.RequestBody} of this message. - *
- * This is used internally for executing webhooks through HTTP requests. + *
This is used internally for executing webhooks through HTTP requests. * * @return The request body */ @@ -434,11 +383,10 @@ else if (data instanceof InputStream) else if (data instanceof byte[]) a = new MessageAttachment(name, (byte[]) data); else - throw new IllegalArgumentException( - "Provided arguments must be pairs for (String, Data). Unexpected data type " - + data.getClass().getName()); + throw new IllegalArgumentException("Provided arguments must be pairs for (String, Data). Unexpected data type " + data.getClass().getName()); return a; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index 18e6b71..b3d7cdd 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -114,19 +114,19 @@ public WebhookMessageBuilder resetEmbeds() { /** * The mention whitelist. - *
- * See {@link AllowedMentions} for more details. + *
See {@link AllowedMentions} for more details. * - * @param mentions - * The mention whitelist + * @param mentions + * The mention whitelist * * @throws NullPointerException - * If provided null + * If provided null * * @return This builder for chaining convenience */ @NotNull - public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) { + public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mentions) + { this.allowedMentions = Objects.requireNonNull(mentions); return this; } @@ -135,16 +135,14 @@ public WebhookMessageBuilder setAllowedMentions(@NotNull AllowedMentions mention * Adds the provided embeds to the builder * * @param embeds - * The embeds to add + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are added + * If more than {@value WebhookMessage#MAX_EMBEDS} are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { @@ -161,17 +159,15 @@ public WebhookMessageBuilder addEmbeds(@NotNull WebhookEmbed... embeds) { /** * Adds the provided embeds to the builder * - * @param embeds - * The embeds to add + * @param embeds + * The embeds to add * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * @throws java.lang.IllegalStateException - * If more than - * {@value WebhookMessage#MAX_EMBEDS} - * are added + * If more than {@value WebhookMessage#MAX_EMBEDS} are added */ @NotNull public WebhookMessageBuilder addEmbeds(@NotNull Collection embeds) { @@ -188,14 +184,13 @@ public WebhookMessageBuilder addEmbeds(@NotNull Collection - * Each message by a webhook can have a different user appearance. + *
Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param username - * The (nullable) username to use + * @param username + * The (nullable) username to use * * @return This builder for chaining convenience */ @@ -251,13 +244,12 @@ public WebhookMessageBuilder setUsername(@Nullable String username) { /** * The avatar url to use for this message. - *
- * Each message by a webhook can have a different user appearance. + *
Each message by a webhook can have a different user appearance. * If this is not set it will default the user appearance in the settings of * the webhook. * - * @param avatarUrl - * The (nullable) avatar url to use + * @param avatarUrl + * The (nullable) avatar url to use * * @return This builder for chaining convenience */ @@ -270,8 +262,8 @@ public WebhookMessageBuilder setAvatarUrl(@Nullable String avatarUrl) { /** * Whether this message should use Text-to-Speech (TTS) * - * @param tts - * True, if this message should use tts + * @param tts + * True, if this message should use tts * * @return This builder for chaining convenience */ @@ -282,11 +274,10 @@ public WebhookMessageBuilder setTTS(boolean tts) { } /** - * Whether the message should be ephemeral (only works for interaction - * webhooks). + * Whether the message should be ephemeral (only works for interaction webhooks). * - * @param ephemeral - * True if the message should be ephemeral, false otherwise + * @param ephemeral + * True if the message should be ephemeral, false otherwise * * @return This builder for chaining convenience */ @@ -299,36 +290,17 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { return this; } - /** - * Whether the message should be silent - * - * @param silent - * True if the message should be sent silently, false otherwise - * - * @return This builder for chaining convenience - */ - @NotNull - public WebhookMessageBuilder setSilent(boolean silent) { - if (silent) - flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; - else - flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; - return this; - } - /** * Adds the provided file as an attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * * @param file - * The file to attach + * The file to attach * * @return This builder for chaining convenience * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null */ @NotNull public WebhookMessageBuilder addFile(@NotNull File file) { @@ -338,17 +310,15 @@ public WebhookMessageBuilder addFile(@NotNull File file) { /** * Adds the provided file as an attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used instead - * @param file - * The file to attach + * @param name + * The alternative name that should be used instead + * @param file + * The file to attach * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -356,34 +326,31 @@ public WebhookMessageBuilder addFile(@NotNull File file) { public WebhookMessageBuilder addFile(@NotNull String name, @NotNull File file) { Objects.requireNonNull(file, "File"); Objects.requireNonNull(name, "Name"); - if (!file.exists() || !file.canRead()) - throw new IllegalArgumentException("File must exist and be readable"); + if (!file.exists() || !file.canRead()) throw new IllegalArgumentException("File must exist and be readable"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, file); files[fileIndex++] = attachment; return this; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Adds the provided data as a file attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -392,8 +359,7 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) Objects.requireNonNull(data, "Data"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; @@ -402,17 +368,15 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull byte[] data) /** * Adds the provided data as a file attachment to this message. - *
- * A single message can have up to {@value WebhookMessage#MAX_FILES} - * attachments. + *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. * - * @param name - * The alternative name that should be used - * @param data - * The data to attach as a file + * @param name + * The alternative name that should be used + * @param data + * The data to attach as a file * * @throws java.lang.NullPointerException - * If provided with null + * If provided with null * * @return This builder for chaining convenience */ @@ -421,25 +385,24 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull InputStream Objects.requireNonNull(data, "InputStream"); Objects.requireNonNull(name, "Name"); if (fileIndex >= WebhookMessage.MAX_FILES) - throw new IllegalStateException( - "Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); + throw new IllegalStateException("Cannot add more than " + WebhookMessage.MAX_FILES + " attachments to a message"); try { MessageAttachment attachment = new MessageAttachment(name, data); files[fileIndex++] = attachment; return this; - } catch (IOException ex) { + } + catch (IOException ex) { throw new IllegalArgumentException(ex); } } /** * Sets the provided name as the name for a newly created thread. - *
- * This is only valid for forum/media channels. + *
This is only valid for forum/media channels. * - * @param name - * The name that should be used + * @param name + * The name that should be used * * @return This builder for chaining convenience */ @@ -453,8 +416,7 @@ public WebhookMessageBuilder setThreadName(@Nullable String name) { * Constructs the {@link club.minnced.discord.webhook.send.WebhookMessage} * from the current configurations. * - * @return The resulting - * {@link club.minnced.discord.webhook.send.WebhookMessage} + * @return The resulting {@link club.minnced.discord.webhook.send.WebhookMessage} */ @NotNull public WebhookMessage build() { @@ -464,6 +426,7 @@ public WebhookMessage build() { fileIndex == 0 ? null : Arrays.copyOf(files, fileIndex), allowedMentions, flags, threadName); } + ///////////////////////////////// /// Third-party compatibility /// ///////////////////////////////// @@ -471,11 +434,11 @@ public WebhookMessage build() { /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -487,11 +450,11 @@ public static WebhookMessageBuilder fromJDA(@NotNull net.dv8tion.jda.api.entitie /** * Converts a JDA {@link Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -506,25 +469,26 @@ public static WebhookMessageBuilder fromJDA(@NotNull MessageCreateData message) Set mentionedUsers = message.getMentionedUsers(); Set mentionedRoles = message.getMentionedRoles(); builder.setAllowedMentions( - AllowedMentions.none() - .withUsers(mentionedUsers) - .withRoles(mentionedRoles) - .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) - .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) - .withParseUsers(allowedMentions.contains(Message.MentionType.USER))); + AllowedMentions.none() + .withUsers(mentionedUsers) + .withRoles(mentionedRoles) + .withParseEveryone(allowedMentions.contains(Message.MentionType.EVERYONE)) + .withParseRoles(allowedMentions.contains(Message.MentionType.ROLE)) + .withParseUsers(allowedMentions.contains(Message.MentionType.USER)) + ); + return builder; } /** - * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} - * into a compatible WebhookMessageBuilder. + * Converts a Javacord {@link org.javacord.api.entity.message.Message Message} into a compatible WebhookMessageBuilder. * - * @param message - * The message + * @param message + * The message * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -537,27 +501,26 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit AllowedMentions allowedMentions = AllowedMentions.none(); allowedMentions.withUsers( - message.getMentionedUsers().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedUsers().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withRoles( - message.getMentionedRoles().stream() - .map(DiscordEntity::getIdAsString) - .collect(Collectors.toList())); + message.getMentionedRoles().stream() + .map(DiscordEntity::getIdAsString) + .collect(Collectors.toList())); allowedMentions.withParseEveryone(message.mentionsEveryone()); builder.setAllowedMentions(allowedMentions); return builder; } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param callback - * The callback used to specify the desired message settings + * @param callback + * The callback used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data * @@ -566,20 +529,17 @@ public static WebhookMessageBuilder fromJavacord(@NotNull org.javacord.api.entit @NotNull @Deprecated public static WebhookMessageBuilder fromD4J(@NotNull Consumer callback) { - throw new UnsupportedOperationException( - "Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); + throw new UnsupportedOperationException("Cannot build messages via consumers in Discord4J 3.2.0! Please change to fromD4J(spec)"); } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message - * settings + * @param spec + * The message create spec used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -603,10 +563,11 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { builder.setTTS(tts.get()); if (!embeds.isAbsent()) { builder.addEmbeds( - embeds.get().stream() - .map(WebhookEmbedBuilder::fromD4J) - .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList())); + embeds.get().stream() + .map(WebhookEmbedBuilder::fromD4J) + .map(WebhookEmbedBuilder::build) + .collect(Collectors.toList()) + ); } if (!allowedMentions.isAbsent()) { @@ -629,15 +590,13 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageCreateSpec spec) { } /** - * Converts a Discord4J {@link MessageCreateSpec} into a compatible - * WebhookMessageBuilder. + * Converts a Discord4J {@link MessageCreateSpec} into a compatible WebhookMessageBuilder. * - * @param spec - * The message create spec used to specify the desired message - * settings + * @param spec + * The message create spec used to specify the desired message settings * * @throws NullPointerException - * If null is provided + * If null is provided * * @return WebhookMessageBuilder with the converted data */ @@ -661,7 +620,8 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageEditSpec spec) { embeds.get().get().stream() .map(WebhookEmbedBuilder::fromD4J) .map(WebhookEmbedBuilder::build) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } if (!allowedMentions.isAbsent() && allowedMentions.get().isPresent()) { From 5109645b41c5fc430ec6815b6d2d212f66a449a9 Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Mon, 22 Jan 2024 12:47:31 -0800 Subject: [PATCH 5/6] added a setSilent function (this time without messing up all the formatting) --- .../minnced/discord/webhook/MessageFlags.java | 21 +++++++++++-------- .../discord/webhook/send/WebhookMessage.java | 12 +++++++++++ .../webhook/send/WebhookMessageBuilder.java | 17 +++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/MessageFlags.java b/src/main/java/club/minnced/discord/webhook/MessageFlags.java index d9a0ffc..5d46098 100644 --- a/src/main/java/club/minnced/discord/webhook/MessageFlags.java +++ b/src/main/java/club/minnced/discord/webhook/MessageFlags.java @@ -21,12 +21,15 @@ */ @SuppressWarnings("PointlessBitwiseExpression") public class MessageFlags { - public static final int CROSSPOSTED = 1 << 0; - public static final int IS_CROSSPOSTED = 1 << 1; - public static final int SUPPRESS_EMBEDS = 1 << 2; - public static final int SOURCE_MESSAGE_DELETED = 1 << 3; - public static final int URGENT = 1 << 4; - public static final int HAS_THREAD = 1 << 5; - public static final int EPHEMERAL = 1 << 6; - public static final int LOADING = 1 << 7; -} + public static final int CROSSPOSTED = 1 << 0; + public static final int IS_CROSSPOSTED = 1 << 1; + public static final int SUPPRESS_EMBEDS = 1 << 2; + public static final int SOURCE_MESSAGE_DELETED = 1 << 3; + public static final int URGENT = 1 << 4; + public static final int HAS_THREAD = 1 << 5; + public static final int EPHEMERAL = 1 << 6; + public static final int LOADING = 1 << 7; + public static final int FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8; + public static final int SUPPRESS_NOTIFICATIONS = 1 << 12; + public static final int IS_VOICE_MESSAGE = 1 << 13; +} \ No newline at end of file diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 78ce7c8..8d08d8e 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -154,6 +154,17 @@ public WebhookMessage asEphemeral(boolean ephemeral) { return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); } + //copy the above function but change it to be for the silent flag + @NotNull + public WebhookMessage asSilent(boolean silent) { + int flags = this.flags; + if (silent) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); + } + /** * Converts a {@link club.minnced.discord.webhook.receive.ReadonlyMessage} to a * WebhookMessage. @@ -176,6 +187,7 @@ public static WebhookMessage from(@NotNull ReadonlyMessage message) { builder.setContent(message.getContent()); builder.setTTS(message.isTTS()); builder.setEphemeral((message.getFlags() & MessageFlags.EPHEMERAL) != 0); + builder.setSilent((message.getFlags() & MessageFlags.SUPPRESS_NOTIFICATIONS) != 0); builder.addEmbeds(message.getEmbeds()); return builder.build(); } diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index b3d7cdd..e2ea3a5 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -290,6 +290,23 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { return this; } + /** + * Whether the message should be silent + * + * @param silent + * True if the message should be silent, false otherwise + * + * @return This builder for chaining convenience + */ + @NotNull + public WebhookMessageBuilder setSilent(boolean suppressNotifications) { + if (suppressNotifications) + flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; + else + flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS; + return this; + } + /** * Adds the provided file as an attachment to this message. *
A single message can have up to {@value WebhookMessage#MAX_FILES} attachments. From 72aaa74f4fa40b896498f8b955b64ce808e7f7bf Mon Sep 17 00:00:00 2001 From: William Beemer <37132465+MrBubbles06@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:29:43 -0800 Subject: [PATCH 6/6] Added documentation for asSilent(), and fixed formatting issues. --- .../minnced/discord/webhook/send/WebhookMessage.java | 10 +++++++++- .../discord/webhook/send/WebhookMessageBuilder.java | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java index 8d08d8e..033b975 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessage.java @@ -154,7 +154,15 @@ public WebhookMessage asEphemeral(boolean ephemeral) { return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags, threadName); } - //copy the above function but change it to be for the silent flag + /** + * Returns a new WebhookMessage instance with the silent flag turned on/off (true/false). + *
This instance remains unchanged and a new instance is returned. + * + * @param silent + * Whether to make this message silent + * + * @return New WebhookMessage instance + */ @NotNull public WebhookMessage asSilent(boolean silent) { int flags = this.flags; diff --git a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java index e2ea3a5..e7d6ef8 100644 --- a/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java +++ b/src/main/java/club/minnced/discord/webhook/send/WebhookMessageBuilder.java @@ -294,13 +294,13 @@ public WebhookMessageBuilder setEphemeral(boolean ephemeral) { * Whether the message should be silent * * @param silent - * True if the message should be silent, false otherwise + * True if the message should be silent, false otherwise * * @return This builder for chaining convenience */ @NotNull - public WebhookMessageBuilder setSilent(boolean suppressNotifications) { - if (suppressNotifications) + public WebhookMessageBuilder setSilent(boolean silent) { + if (silent) flags |= MessageFlags.SUPPRESS_NOTIFICATIONS; else flags &= ~MessageFlags.SUPPRESS_NOTIFICATIONS;