Skip to content

Add support for components #73

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 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
9286cb9
Add initial support for components
MinnDevelopment Jan 1, 2023
00b8750
Fix tests
MinnDevelopment Jan 4, 2023
0b213b3
Add WebhookMessage#components
MinnDevelopment Jan 4, 2023
345fd27
Fix warnings/errors
MinnDevelopment Jan 4, 2023
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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repositories {
val versions = mapOf(
"slf4j" to "1.7.32",
"okhttp" to "4.10.0",
"json" to "20210307",
"json" to "20220924",
"jda" to "5.0.0-alpha.13",
"discord4j" to "3.2.2",
"javacord" to "3.4.0",
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/club/minnced/discord/webhook/DiscordEmoji.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import org.json.JSONString;

import java.util.Objects;

/**
* Emoji class used for components.
*
* <p>The {@link #toString()} method returns the message content serialized format, e.g. {@code <:name:id>} for custom emoji.
*/
public class DiscordEmoji implements JSONString {
private final String name;
private final long id;
private final boolean animated;

private DiscordEmoji(@NotNull String name, long id, boolean animated) {
this.name = name;
this.id = id;
this.animated = animated;
}

/**
* Emoji instance for custom emoji, with an id and optional animations.
* <br>Webhooks are limited to emoji from the same guild.
*
* @param name
* the name of the emoji
* @param id
* the id of the emoji
* @param animated
* true if the emoji is animated
*
* @throws java.lang.NullPointerException
* If null is provided
*
* @return An instance of emoji, usable in components
*/
@NotNull
public static DiscordEmoji custom(@NotNull String name, long id, boolean animated) {
return new DiscordEmoji(Objects.requireNonNull(name), id, animated);
}

/**
* Emoji instance for unicode codepoints.
* <br>This does not support aliases such as {@code :smiley:}.
* You must use the correct unicode characters.
*
* @param codepoints
* The unicode codepoints
*
* @return An instance of emoji, usable in components
*/
@NotNull
public static DiscordEmoji unicode(@NotNull String codepoints) {
return new DiscordEmoji(Objects.requireNonNull(codepoints), 0, false);
}

/**
* The emoji name, or unicode codepoints
*
* @return The name of the emoji
*/
public String getName() {
return name;
}

/**
* The snowflake id of the custom emoji, or {@code 0} for unicode
*
* @return The snowflake id of the emoji
*/
public long getId() {
return id;
}

/**
* Whether this is an animated custom emoji.
*
* @return true if the emoji is animated
*/
public boolean isAnimated() {
return animated;
}

@Override
public String toJSONString() {
JSONObject json = new JSONObject();
json.put("name", this.name);
if (id != 0) {
json.put("id", this.id);
json.put("animated", this.animated);
}
return json.toString();
}

@Override
public String toString() {
if (id == 0)
return name;
else
return (animated ? "<a:" : "<:") + name + ":" + id + ">";
}
}
83 changes: 83 additions & 0 deletions src/main/java/club/minnced/discord/webhook/WebhookClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import club.minnced.discord.webhook.send.WebhookEmbed;
import club.minnced.discord.webhook.send.WebhookMessage;
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
import club.minnced.discord.webhook.send.component.ComponentLayout;
import club.minnced.discord.webhook.util.ThreadPools;
import club.minnced.discord.webhook.util.WebhookErrorHandler;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -406,8 +407,11 @@ public CompletableFuture<ReadonlyMessage> send(@NotNull InputStream data, @NotNu
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*
* @deprecated Use {@link #sendEmbeds(club.minnced.discord.webhook.send.WebhookEmbed, club.minnced.discord.webhook.send.WebhookEmbed...)} instead
*/
@NotNull
@Deprecated
public CompletableFuture<ReadonlyMessage> send(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) {
return send(WebhookMessage.embeds(first, embeds));
}
Expand All @@ -424,12 +428,91 @@ public CompletableFuture<ReadonlyMessage> send(@NotNull WebhookEmbed first, @Not
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*
* @deprecated Use {@link #sendEmbeds(java.util.Collection)} instead
*/
@NotNull
@Deprecated
public CompletableFuture<ReadonlyMessage> send(@NotNull Collection<WebhookEmbed> embeds) {
return send(WebhookMessage.embeds(embeds));
}

/**
* Sends the provided {@link club.minnced.discord.webhook.send.WebhookEmbed} to the webhook.
* <br>The returned future receives {@code null} if {@link club.minnced.discord.webhook.WebhookClientBuilder#setWait(boolean)}
* was set to false.
*
* @param first
* The first embed to send
* @param embeds
* Optional additional embeds to send, up to 10
*
* @return {@link java.util.concurrent.CompletableFuture}
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*/
@NotNull
public CompletableFuture<ReadonlyMessage> sendEmbeds(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds) {
return send(WebhookMessage.embeds(first, embeds));
}

/**
* Sends the provided {@link club.minnced.discord.webhook.send.WebhookEmbed} to the webhook.
* <br>The returned future receives {@code null} if {@link club.minnced.discord.webhook.WebhookClientBuilder#setWait(boolean)}
* was set to false.
*
* @param embeds
* The embeds to send
*
* @return {@link java.util.concurrent.CompletableFuture}
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*/
@NotNull
public CompletableFuture<ReadonlyMessage> sendEmbeds(@NotNull Collection<WebhookEmbed> embeds) {
return send(WebhookMessage.embeds(embeds));
}

/**
* Sends the provided {@link club.minnced.discord.webhook.send.component.ComponentLayout} to the webhook.
* <br>The returned future receives {@code null} if {@link club.minnced.discord.webhook.WebhookClientBuilder#setWait(boolean)}
* was set to false.
*
* @param first
* The first embed to send
* @param components
* Optional additional components to send, up to 5
*
* @return {@link java.util.concurrent.CompletableFuture}
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*/
@NotNull
public CompletableFuture<ReadonlyMessage> sendComponents(@NotNull ComponentLayout first, @NotNull ComponentLayout... components) {
return send(WebhookMessage.components(first, components));
}

/**
* Sends the provided {@link club.minnced.discord.webhook.send.component.ComponentLayout} to the webhook.
* <br>The returned future receives {@code null} if {@link club.minnced.discord.webhook.WebhookClientBuilder#setWait(boolean)}
* was set to false.
*
* @param components
* The components to send
*
* @return {@link java.util.concurrent.CompletableFuture}
*
* @see #isWait()
* @see #send(club.minnced.discord.webhook.send.WebhookMessage)
*/
@NotNull
public CompletableFuture<ReadonlyMessage> sendComponents(@NotNull Collection<? extends ComponentLayout> components) {
return send(WebhookMessage.components(components));
}

/**
* Sends the provided content as normal message to the webhook.
* <br>The returned future receives {@code null} if {@link club.minnced.discord.webhook.WebhookClientBuilder#setWait(boolean)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull WebhookEmbed
@NotNull
public List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull Collection<WebhookEmbed> embeds) {
return webhooks.stream()
.map(w -> w.send(embeds))
.map(w -> w.sendEmbeds(embeds))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.external;

import club.minnced.discord.webhook.WebhookClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.external;

import club.minnced.discord.webhook.WebhookClient;
Expand Down Expand Up @@ -135,7 +151,7 @@ public CompletableFuture<ReadonlyMessage> send(@NotNull net.dv8tion.jda.api.enti
*/
@NotNull
public CompletableFuture<ReadonlyMessage> send(@NotNull net.dv8tion.jda.api.entities.MessageEmbed embed) {
return send(WebhookEmbedBuilder.fromJDA(embed).build());
return sendEmbeds(WebhookEmbedBuilder.fromJDA(embed).build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.external;

import club.minnced.discord.webhook.WebhookClient;
Expand Down Expand Up @@ -134,7 +150,7 @@ public CompletableFuture<ReadonlyMessage> send(@NotNull org.javacord.api.entity.
*/
@NotNull
public CompletableFuture<ReadonlyMessage> send(@NotNull org.javacord.api.entity.message.embed.Embed embed) {
return send(WebhookEmbedBuilder.fromJavacord(embed).build());
return sendEmbeds(WebhookEmbedBuilder.fromJavacord(embed).build());
}

/**
Expand Down
Loading