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

Add support for message forwarding #2744

Merged
merged 11 commits into from
Oct 5, 2024
Prev Previous commit
Next Next commit
Fix channel id handling and add test
MinnDevelopment committed Oct 4, 2024

Verified

This commit was signed with the committer’s verified signature.
MinnDevelopment Florian Spieß
commit 8df6caa55b35873139fe0497696edb8db98ecee6
Original file line number Diff line number Diff line change
@@ -141,7 +141,7 @@ public MessageCreateAction setMessageReference(@Nonnull MessageReference.Message
Checks.isSnowflake(channelId, "Channel ID");
Checks.isSnowflake(messageId, "Message ID");
Checks.check(type != MessageReference.MessageReferenceType.UNKNOWN, "Cannot create a message reference of UNKNOWN type");
this.messageReference = new MessageReferenceData(type, guildId, messageId);
this.messageReference = new MessageReferenceData(type, guildId, channelId, messageId);
return this;
}

@@ -154,7 +154,7 @@ public MessageCreateAction setMessageReference(@Nullable String messageId)
String guildId = null;
if (channel instanceof GuildChannel)
guildId = ((GuildChannel) channel).getGuild().getId();
this.messageReference = new MessageReferenceData(MessageReference.MessageReferenceType.DEFAULT, guildId, messageId);
this.messageReference = new MessageReferenceData(MessageReference.MessageReferenceType.DEFAULT, guildId, channel.getId(), messageId);
return this;
}

@@ -216,13 +216,15 @@ private class MessageReferenceData implements SerializableData
{
private final MessageReference.MessageReferenceType type;
private final String messageId;
private final String channelId;
private final String guildId;

private MessageReferenceData(MessageReference.MessageReferenceType type, String guildId, String messageId)
private MessageReferenceData(MessageReference.MessageReferenceType type, String guildId, String channelId, String messageId)
{
this.type = type;
this.messageId = messageId;
this.guildId = guildId;
this.channelId = channelId;
}

@Nonnull
@@ -232,7 +234,7 @@ public DataObject toData()
DataObject data = DataObject.empty()
.put("type", type.getId())
.put("message_id", messageId)
.put("channel_id", channel.getId());
.put("channel_id", channelId);
if (guildId != null)
data.put("guild_id", guildId);
return data;
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.MessageReference;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.ActionRow;
@@ -32,6 +33,7 @@
import net.dv8tion.jda.api.utils.messages.MessagePollBuilder;
import net.dv8tion.jda.api.utils.messages.MessagePollData;
import net.dv8tion.jda.internal.requests.restaction.MessageCreateActionImpl;
import net.dv8tion.jda.test.Constants;
import net.dv8tion.jda.test.IntegrationTest;
import okhttp3.MediaType;
import org.junit.jupiter.api.BeforeEach;
@@ -64,9 +66,16 @@ public class MessageCreateActionTest extends IntegrationTest
@Mock
protected MessageChannel channel;

private static DataObject defaultMessageRequest()
private static DataObject minimalMessageRequest()
{
return DataObject.empty()
.put("enforce_nonce", true)
.put("flags", 0)
.put("nonce", FIXED_NONCE);
}
private static DataObject defaultMessageRequest()
{
return minimalMessageRequest()
.put("allowed_mentions", DataObject.empty()
.put("parse", DataArray.empty()
.add("users")
@@ -77,9 +86,6 @@ private static DataObject defaultMessageRequest()
.put("content", "")
.put("embeds", DataArray.empty())
.put("poll", null)
.put("enforce_nonce", true)
.put("flags", 0)
.put("nonce", FIXED_NONCE)
.put("tts", false);
}

@@ -193,6 +199,54 @@ void testSuppressVoiceMessage()
).whenQueueCalled();
}

@Test
void testReplyWithContent()
{
MessageCreateActionImpl action = new MessageCreateActionImpl(channel);

long messageId = random.nextLong();
action.setMessageReference(messageId);
action.setContent("test content");
action.failOnInvalidReply(true);

assertThatRequestFrom(action)
.hasBodyEqualTo(defaultMessageRequest()
.put("content", "test content")
.put("message_reference", DataObject.empty()
.put("type", 0)
.put("channel_id", channel.getId())
.put("message_id", Long.toUnsignedString(messageId))
.put("fail_if_not_exists", true)))
.whenQueueCalled();
}

@Test
void testForwardWithFlags()
{
MessageCreateActionImpl action = new MessageCreateActionImpl(channel);

long messageId = random.nextLong();
action.setMessageReference(
MessageReference.MessageReferenceType.FORWARD,
Constants.GUILD_ID,
Constants.CHANNEL_ID,
messageId
);

action.setSuppressedNotifications(true);

assertThatRequestFrom(action)
.hasBodyEqualTo(minimalMessageRequest()
.put("flags", 1 << 12)
.put("message_reference", DataObject.empty()
.put("type", 1)
.put("guild_id", Long.toUnsignedString(Constants.GUILD_ID))
.put("channel_id", Long.toUnsignedString(Constants.CHANNEL_ID))
.put("message_id", Long.toUnsignedString(messageId))
.put("fail_if_not_exists", false)))
.whenQueueCalled();
}

@Test
void testFullFromBuilder()
{