Skip to content

Commit

Permalink
Handle messages from inaccessible threads and add webhook support (#2390
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MinnDevelopment committed Oct 15, 2023
1 parent 434416e commit 2d97b27
Show file tree
Hide file tree
Showing 46 changed files with 1,773 additions and 1,116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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 net.dv8tion.jda.api.entities;

/**
* Specialization of {@link WebhookClient} for incoming webhooks.
* <br>An incoming webhook, is a webhook that sends messages with a customizable name and avatar.
*
* <p>These webhooks are primarily used to integrate external services.
*/
public interface IncomingWebhookClient extends WebhookClient<Message>
{
}
215 changes: 141 additions & 74 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java

Large diffs are not rendered by default.

72 changes: 62 additions & 10 deletions src/main/java/net/dv8tion/jda/api/entities/MessageReaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;

/**
Expand All @@ -51,8 +52,10 @@
*/
public class MessageReaction
{
private final MessageChannel channel;
private final JDA jda;
private final EmojiUnion emoji;
private final MessageChannel channel;
private final long channelId;
private final long messageId;
private final boolean self;
private final int count;
Expand All @@ -64,17 +67,21 @@ public class MessageReaction
* The {@link MessageChannel} this Reaction was used in
* @param emoji
* The {@link Emoji} that was used
* @param channelId
* The channel id for this reaction
* @param messageId
* The message id this reaction is attached to
* @param self
* Whether we already reacted with this Reaction
* @param count
* The amount of people that reacted with this Reaction
*/
public MessageReaction(@Nonnull MessageChannel channel, @Nonnull EmojiUnion emoji, long messageId, boolean self, int count)
public MessageReaction(@Nonnull JDA jda, @Nullable MessageChannel channel, @Nonnull EmojiUnion emoji, long channelId, long messageId, boolean self, int count)
{
this.channel = channel;
this.jda = jda;
this.emoji = emoji;
this.channel = channel;
this.channelId = channelId;
this.messageId = messageId;
this.self = self;
this.count = count;
Expand All @@ -88,7 +95,7 @@ public MessageReaction(@Nonnull MessageChannel channel, @Nonnull EmojiUnion emoj
@Nonnull
public JDA getJDA()
{
return channel.getJDA();
return jda;
}

/**
Expand Down Expand Up @@ -118,6 +125,18 @@ public boolean hasCount()
return count >= 0;
}

/**
* Whether this reaction instance has an available {@link #getChannel()}.
*
* <p>This can be {@code false} for messages sent via webhooks, or in the context of interactions.
*
* @return True, if {@link #getChannel()} is available
*/
public boolean hasChannel()
{
return channel != null;
}

/**
* The amount of users that already reacted with this Reaction
* <br><b>This is not updated, it is a {@code final int} per Reaction instance</b>
Expand Down Expand Up @@ -147,7 +166,7 @@ public int getCount()
@Nonnull
public ChannelType getChannelType()
{
return channel.getType();
return channel != null ? channel.getType() : ChannelType.UNKNOWN;
}

/**
Expand All @@ -168,7 +187,7 @@ public boolean isFromType(@Nonnull ChannelType type)
* The {@link net.dv8tion.jda.api.entities.Guild Guild} this Reaction was used in.
*
* @throws IllegalStateException
* If {@link #getChannel()} is not a guild channel
* If {@link #getChannel()} is not a guild channel or the channel is not provided
*
* @return {@link net.dv8tion.jda.api.entities.Guild Guild} this Reaction was used in
*/
Expand All @@ -182,19 +201,24 @@ public Guild getGuild()
* The {@link MessageChannel MessageChannel}
* this Reaction was used in.
*
* @throws IllegalStateException
* If no channel instance is provided, this might be missing for messages sent from webhooks.
*
* @return The channel this Reaction was used in
*/
@Nonnull
public MessageChannelUnion getChannel()
{
return (MessageChannelUnion) channel;
if (channel != null)
return (MessageChannelUnion) channel;
throw new IllegalStateException("Cannot provide channel instance for this reaction! Use getChannelId() instead.");
}

/**
* The {@link net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel channel} this Reaction was used in.
*
* @throws IllegalStateException
* If {@link #getChannel()} is not a guild channel
* If {@link #getChannel()} is not a guild channel or the channel is not provided
*
* @return The guild channel this Reaction was used in
*/
Expand All @@ -204,6 +228,27 @@ public GuildMessageChannelUnion getGuildChannel()
return (GuildMessageChannelUnion) getChannel().asGuildMessageChannel();
}

/**
* The ID for the channel this reaction happened in.
*
* @return The channel id
*/
public long getChannelIdLong()
{
return channelId;
}

/**
* The ID for the channel this reaction happened in.
*
* @return The channel id
*/
@Nonnull
public String getChannelId()
{
return Long.toUnsignedString(channelId);
}

/**
* The {@link Emoji} of this Reaction.
* <br>This includes both {@link Emoji.Type#CUSTOM custom emojis} and {@link Emoji.Type#UNICODE unicode emoji}.
Expand Down Expand Up @@ -329,7 +374,7 @@ public RestAction<Void> removeReaction(@Nonnull User user)
{
Checks.notNull(user, "User");
boolean self = user.equals(getJDA().getSelfUser());
if (!self)
if (!self && channel != null)
{
if (!channel.getType().isGuild())
throw new PermissionException("Unable to remove Reaction of other user in non-guild channels!");
Expand All @@ -341,7 +386,7 @@ public RestAction<Void> removeReaction(@Nonnull User user)

String code = emoji.getAsReactionCode();
String target = self ? "@me" : user.getId();
Route.CompiledRoute route = Route.Messages.REMOVE_REACTION.compile(channel.getId(), getMessageId(), code, target);
Route.CompiledRoute route = Route.Messages.REMOVE_REACTION.compile(getChannelId(), getMessageId(), code, target);
return new RestActionImpl<>(getJDA(), route);
}

Expand Down Expand Up @@ -376,6 +421,12 @@ public RestAction<Void> removeReaction(@Nonnull User user)
@CheckReturnValue
public RestAction<Void> clearReactions()
{
if (channel == null)
{
Route.CompiledRoute route = Route.Messages.CLEAR_EMOJI_REACTIONS.compile(getChannelId(), getMessageId(), emoji.getAsReactionCode());
return new RestActionImpl<>(jda, route);
}

// Requires permission, only works in guilds
if (!getChannelType().isGuild())
throw new UnsupportedOperationException("Cannot clear reactions on a message sent from a private channel");
Expand All @@ -400,6 +451,7 @@ public boolean equals(Object obj)
public String toString()
{
return new EntityString(this)
.addMetadata("channelId", channelId)
.addMetadata("messageId", messageId)
.addMetadata("emoji", emoji)
.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @see Guild#retrieveWebhooks()
* @see JDA#retrieveWebhookById(String)
*/
public interface Webhook extends ISnowflake
public interface Webhook extends ISnowflake, WebhookClient<Message>
{
/**
* Pattern for a Webhook URL.
Expand Down
Loading

0 comments on commit 2d97b27

Please sign in to comment.