Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added Support for Webhook Components #63

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
682d3de
Transferred changes from `RohanGoyalDev/discord-webhooks:master`
jasonlessenich Jul 24, 2022
726fc34
fixed casing (yeah, that really bothered me)
jasonlessenich Jul 24, 2022
28943c3
Removed annoying URL param for `Button#link`
jasonlessenich Jul 24, 2022
69d4d56
Added Debug sout for testing
jasonlessenich Jul 24, 2022
74301a4
Fixed Components not using their correct id
jasonlessenich Jul 24, 2022
e0a0775
Update src/main/java/club/minnced/discord/webhook/send/component/Acti…
jasonlessenich Aug 7, 2022
b0e5c69
Update src/main/java/club/minnced/discord/webhook/send/component/Acti…
jasonlessenich Aug 7, 2022
14b27fb
Update src/main/java/club/minnced/discord/webhook/send/component/Acti…
jasonlessenich Aug 7, 2022
9ccbb9d
Update src/main/java/club/minnced/discord/webhook/send/component/Butt…
jasonlessenich Aug 7, 2022
c8a879a
Update src/main/java/club/minnced/discord/webhook/send/component/Comp…
jasonlessenich Aug 7, 2022
da5a0a6
Update src/main/java/club/minnced/discord/webhook/send/component/Comp…
jasonlessenich Aug 7, 2022
006f85c
Update src/main/java/club/minnced/discord/webhook/send/component/Comp…
jasonlessenich Aug 7, 2022
5fe79f1
Update src/main/java/club/minnced/discord/webhook/send/component/Layo…
jasonlessenich Aug 7, 2022
1fca72a
Update src/main/java/club/minnced/discord/webhook/send/component/Layo…
jasonlessenich Aug 7, 2022
f728856
Update src/main/java/club/minnced/discord/webhook/send/component/Part…
jasonlessenich Aug 7, 2022
dd45b87
Update src/main/java/club/minnced/discord/webhook/send/component/Part…
jasonlessenich Aug 7, 2022
3040224
Update src/main/java/club/minnced/discord/webhook/send/component/Part…
jasonlessenich Aug 7, 2022
e8aaa8d
Update src/main/java/club/minnced/discord/webhook/send/component/Sele…
jasonlessenich Aug 7, 2022
7a2d7a8
Update src/main/java/club/minnced/discord/webhook/send/component/Sele…
jasonlessenich Aug 7, 2022
3b19b81
Update src/main/java/club/minnced/discord/webhook/send/component/Sele…
jasonlessenich Aug 7, 2022
b2473c1
Update src/main/java/club/minnced/discord/webhook/send/component/Layo…
jasonlessenich Aug 7, 2022
0fbd680
Update src/main/java/club/minnced/discord/webhook/send/component/Acti…
jasonlessenich Aug 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import club.minnced.discord.webhook.IOUtil;
import club.minnced.discord.webhook.MessageFlags;
import club.minnced.discord.webhook.receive.ReadonlyMessage;
import club.minnced.discord.webhook.send.component.LayoutComponent;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -46,19 +47,21 @@ public class WebhookMessage {

protected final String username, avatarUrl, content;
protected final List<WebhookEmbed> embeds;
protected final List<LayoutComponent> components;
protected final boolean isTTS;
protected final MessageAttachment[] attachments;
protected final AllowedMentions allowedMentions;
protected final int flags;

protected WebhookMessage(final String username, final String avatarUrl, final String content,
final List<WebhookEmbed> embeds, final boolean isTTS,
final List<WebhookEmbed> embeds, List<LayoutComponent> components, final boolean isTTS,
final MessageAttachment[] files, final AllowedMentions allowedMentions,
final int flags) {
this.username = username;
this.avatarUrl = avatarUrl;
this.content = content;
this.embeds = embeds;
this.components = components;
this.isTTS = isTTS;
this.attachments = files;
this.allowedMentions = allowedMentions;
Expand Down Expand Up @@ -149,7 +152,7 @@ public WebhookMessage asEphemeral(boolean ephemeral) {
flags |= MessageFlags.EPHEMERAL;
else
flags &= ~MessageFlags.EPHEMERAL;
return new WebhookMessage(username, avatarUrl, content, embeds, isTTS, attachments, allowedMentions, flags);
return new WebhookMessage(username, avatarUrl, content, embeds, components, isTTS, attachments, allowedMentions, flags);
}

/**
Expand Down Expand Up @@ -205,7 +208,7 @@ public static WebhookMessage embeds(@NotNull WebhookEmbed first, @NotNull Webhoo
List<WebhookEmbed> list = new ArrayList<>(1 + embeds.length);
list.add(first);
Collections.addAll(list, embeds);
return new WebhookMessage(null, null, null, list, false, null, AllowedMentions.all(), 0);
return new WebhookMessage(null, null, null, list, null, false, null, AllowedMentions.all(), 0);
}

/**
Expand All @@ -230,7 +233,7 @@ public static WebhookMessage embeds(@NotNull Collection<WebhookEmbed> 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);
return new WebhookMessage(null, null, null, new ArrayList<>(embeds), null, false, null, AllowedMentions.all(), 0);
}

/**
Expand Down Expand Up @@ -267,7 +270,7 @@ public static WebhookMessage files(@NotNull Map<String, ?> attachments) {
Object data = attachment.getValue();
files[i++] = convertAttachment(name, data);
}
return new WebhookMessage(null, null, null, null, false, files, AllowedMentions.all(), 0);
return new WebhookMessage(null, null, null, null, null, false, files, AllowedMentions.all(), 0);
}

/**
Expand Down Expand Up @@ -313,7 +316,7 @@ public static WebhookMessage files(@NotNull String name1, @NotNull Object data1,
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);
return new WebhookMessage(null, null, null, null, null, false, files, AllowedMentions.all(), 0);
}

/**
Expand Down Expand Up @@ -344,6 +347,15 @@ public RequestBody getBody() {
} else {
payload.put("embeds", new JSONArray());
}
if (components != null && !components.isEmpty()) {
final JSONArray array = new JSONArray();
for (LayoutComponent component : components) {
array.put(component);
}
payload.put("components", array);
} else {
payload.put("components", new JSONArray());
}
if (avatarUrl != null)
payload.put("avatar_url", avatarUrl);
if (username != null)
Expand Down Expand Up @@ -386,4 +398,4 @@ else if (data instanceof byte[])
throw new IllegalArgumentException(ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package club.minnced.discord.webhook.send;

import club.minnced.discord.webhook.MessageFlags;
import club.minnced.discord.webhook.send.component.LayoutComponent;
import discord4j.core.spec.MessageCreateSpec;
import discord4j.core.spec.MessageEditSpec;
import discord4j.discordjson.json.AllowedMentionsData;
Expand Down Expand Up @@ -48,6 +49,7 @@
public class WebhookMessageBuilder {
protected final StringBuilder content = new StringBuilder();
protected final List<WebhookEmbed> embeds = new LinkedList<>();
protected final List<LayoutComponent> components = new ArrayList<>();
protected final MessageAttachment[] files = new MessageAttachment[WebhookMessage.MAX_FILES];
protected AllowedMentions allowedMentions = AllowedMentions.all();
protected String username, avatarUrl;
Expand Down Expand Up @@ -183,6 +185,55 @@ public WebhookMessageBuilder addEmbeds(@NotNull Collection<? extends WebhookEmbe
return this;
}

/**
* Adds the provided embeds to the builder
*
* @param components
* The layout components to add
*
* @return This builder for chaining convenience
*
* @throws java.lang.NullPointerException
* If provided with null
* @throws java.lang.IllegalStateException
* If more than {@value LayoutComponent#MAX_COMPONENTS} are added
Comment on lines +196 to +199
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throws docs should be after param and before return:

/*
 * description text ...
 *
 * @param  ...
 *         description
 *
 * @throws ...
 *         description
 *
 * @return ...
 */

*/
@NotNull
public WebhookMessageBuilder addComponents(@NotNull LayoutComponent... components) {
Objects.requireNonNull(components, "Components");
if (this.components.size() + components.length > LayoutComponent.MAX_COMPONENTS)
throw new IllegalStateException("Cannot have more than " + LayoutComponent.MAX_COMPONENTS + " component layouts in a message");
for (LayoutComponent component : components) {
Objects.requireNonNull(component, "Component");
this.components.add(component);
}
return this;
}

/**
* Adds the provided embeds to the builder
*
* @param components
* The layout components to add
*
* @return This builder for chaining convenience
*
* @throws java.lang.NullPointerException
* If provided with null
* @throws java.lang.IllegalStateException
* If more than {@value LayoutComponent#MAX_COMPONENTS} are added
*/
@NotNull
public WebhookMessageBuilder addComponents(@NotNull Collection<? extends LayoutComponent> components) {
Objects.requireNonNull(components, "Components");
if (this.components.size() + components.size() > LayoutComponent.MAX_COMPONENTS)
throw new IllegalStateException("Cannot have more than " + LayoutComponent.MAX_COMPONENTS + " component layouts in a message");
for (LayoutComponent component : components) {
Objects.requireNonNull(component, "Component");
this.components.add(component);
}
return this;
}
/**
* Configures the content for this builder
*
Expand Down Expand Up @@ -409,7 +460,7 @@ public WebhookMessageBuilder addFile(@NotNull String name, @NotNull InputStream
public WebhookMessage build() {
if (isEmpty())
throw new IllegalStateException("Cannot build an empty message!");
return new WebhookMessage(username, avatarUrl, content.toString(), embeds, isTTS,
return new WebhookMessage(username, avatarUrl, content.toString(), embeds, components, isTTS,
fileIndex == 0 ? null : Arrays.copyOf(files, fileIndex), allowedMentions, flags);
}

Expand Down Expand Up @@ -452,13 +503,13 @@ public static WebhookMessageBuilder fromJDA(@NotNull net.dv8tion.jda.api.entitie
AllowedMentions allowedMentions = AllowedMentions.none();
Mentions mentions = message.getMentions();
allowedMentions.withRoles(
mentions.getRoles().stream()
.map(Role::getId)
.collect(Collectors.toList()));
mentions.getRoles().stream()
.map(Role::getId)
.collect(Collectors.toList()));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was all this indentation changed?

allowedMentions.withUsers(
mentions.getUsers().stream()
.map(User::getId)
.collect(Collectors.toList()));
mentions.getUsers().stream()
.map(User::getId)
.collect(Collectors.toList()));
allowedMentions.withParseEveryone(mentions.mentionsEveryone());
builder.setAllowedMentions(allowedMentions);
builder.setEphemeral(message.isEphemeral());
Expand Down Expand Up @@ -486,13 +537,13 @@ 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;
Expand Down Expand Up @@ -548,10 +599,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())
);
}

Expand Down Expand Up @@ -627,4 +678,4 @@ public static WebhookMessageBuilder fromD4J(@NotNull MessageEditSpec spec) {

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018-2020 Florian Spie�
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package club.minnced.discord.webhook.send.component;

import org.jetbrains.annotations.Nullable;

/**
* Interactive components that can be inserted inside a {@link LayoutComponent}
*
* @see LayoutComponent#addComponent(ActionComponent)
*/
public interface ActionComponent extends Component {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

/**
* The custom id of the component.
* <br>This can be used for handling interactions with this component.
*
* @return Custom id of the component, or null for link style buttons
*/
@Nullable String getCustomId();
jasonlessenich marked this conversation as resolved.
Show resolved Hide resolved

/**
* Changes the disabled status of button
* @param disabled
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param disabled
*
* @param disabled

* use true to disable button
*/
void withDisabled(boolean disabled);

/**
* @return true if the button is disabled
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description

*/
boolean isDisabled();

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}
Loading