-
Notifications
You must be signed in to change notification settings - Fork 33
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
Changes from 5 commits
682d3de
726fc34
28943c3
69d4d56
74301a4
e0a0775
b0e5c69
14b27fb
9ccbb9d
c8a879a
da5a0a6
006f85c
5fe79f1
1fca72a
f728856
dd45b87
3040224
e8aaa8d
7a2d7a8
3b19b81
b2473c1
0fbd680
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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 | ||
*/ | ||
@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 | ||
* | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
@@ -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; | ||
|
@@ -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()) | ||
); | ||
} | ||
|
||
|
@@ -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 { | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
* use true to disable button | ||||||||
*/ | ||||||||
void withDisabled(boolean disabled); | ||||||||
|
||||||||
/** | ||||||||
* @return true if the button is disabled | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing description |
||||||||
*/ | ||||||||
boolean isDisabled(); | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} |
There was a problem hiding this comment.
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: