-
Notifications
You must be signed in to change notification settings - Fork 35
Added support for components for webhooks #56
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/club/minnced/discord/webhook/send/component/ActionComponent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
Owner
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. Add |
||
|
|
||
| /** | ||
| * 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(); | ||
|
|
||
| /** | ||
| * Changes the disabled status of button | ||
| * @param disabled | ||
| * use true to disable button | ||
| */ | ||
| void withDisabled(boolean disabled); | ||
|
|
||
| /** | ||
| * @return true if the button is disabled | ||
| */ | ||
| boolean isDisabled(); | ||
|
|
||
| } | ||
114 changes: 114 additions & 0 deletions
114
src/main/java/club/minnced/discord/webhook/send/component/ActionRow.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * 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.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| public class ActionRow implements LayoutComponent { | ||
|
Owner
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. Add some class level docs here |
||
|
|
||
| private final List<ActionComponent> components; | ||
|
|
||
| private ActionRow(@NotNull List<ActionComponent> components) { | ||
| validate(components); | ||
| this.components = components; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an action row with the given list of components | ||
| * | ||
| * @param components | ||
| * The components to be added to the action row | ||
| * @throws IllegalStateException | ||
| * If a select menu is added with any other component | ||
| * @throws IllegalStateException | ||
| * If more than {@value LayoutComponent#MAX_COMPONENTS} buttons are added in the same action row | ||
| * @return An action row containing provided components, or null if the collection is empty or null | ||
| */ | ||
| @Nullable | ||
| public ActionRow of(@NotNull Collection<? extends ActionComponent> components) { | ||
| if (components.size() == 0) return null; | ||
| return new ActionRow(new ArrayList<>(components)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an action row with the given list of components | ||
| * | ||
| * @param components | ||
| * The components to be added to the action row | ||
| * @throws IllegalStateException | ||
| * If a select menu is added with any other component | ||
| * @throws IllegalStateException | ||
| * If more than {@value LayoutComponent#MAX_COMPONENTS} buttons are added in the same action row | ||
| * @return An action row containing provided components, or null if the provided array is empty or null | ||
| */ | ||
| @Nullable | ||
| public static ActionRow of(@NotNull ActionComponent... components) { | ||
| if (components == null || components.length == 0) return null; | ||
| return new ActionRow(Arrays.asList(components)); | ||
| } | ||
|
|
||
| @Override | ||
| @NotNull | ||
| public Type getType() { | ||
| return Type.ACTION_ROW; | ||
| } | ||
|
|
||
| @Override | ||
| @NotNull | ||
| public List<ActionComponent> getComponents() { | ||
| return this.components; | ||
| } | ||
|
|
||
| @Override | ||
| @NotNull | ||
| public LayoutComponent addComponent(ActionComponent component) { | ||
| List<ActionComponent> newList = new ArrayList<>(this.components); | ||
| newList.add(component); | ||
| validate(newList); | ||
| this.components.add(component); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public String toJSONString() { | ||
| JSONObject json = new JSONObject(); | ||
| json.put("type", this.getType()); | ||
| json.put("components", this.components); | ||
| return json.toString(); | ||
| } | ||
|
|
||
| private void validate(List<ActionComponent> components) { | ||
| int buttonCount = 0; | ||
| boolean hasSelectMenu = false; | ||
| for (ActionComponent component : components) { | ||
| if (component instanceof Button) buttonCount++; | ||
| else if (component instanceof SelectMenu) hasSelectMenu = true; | ||
| else throw new IllegalArgumentException("Provided component not an instance of Button or SelectMenu"); | ||
| } | ||
| if (hasSelectMenu && components.size() > 1) | ||
| throw new IllegalArgumentException("An action row containing a select menu cant have have more than 1 component"); | ||
| if (buttonCount > Button.MAX_BUTTONS) | ||
| throw new IllegalArgumentException("An action row cannot contain more than " + Button.MAX_BUTTONS + " buttons"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Copyright header should be on every file