diff --git a/src/main/java/net/dv8tion/jda/api/requests/restaction/MessageCreateAction.java b/src/main/java/net/dv8tion/jda/api/requests/restaction/MessageCreateAction.java index b15e92444e..b60895186d 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/restaction/MessageCreateAction.java +++ b/src/main/java/net/dv8tion/jda/api/requests/restaction/MessageCreateAction.java @@ -21,6 +21,7 @@ import net.dv8tion.jda.api.entities.sticker.GuildSticker; import net.dv8tion.jda.api.entities.sticker.Sticker; import net.dv8tion.jda.api.entities.sticker.StickerSnowflake; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.requests.FluentRestAction; import net.dv8tion.jda.api.utils.messages.MessageCreateData; import net.dv8tion.jda.api.utils.messages.MessageCreateRequest; @@ -54,14 +55,14 @@ static void setDefaultFailOnInvalidReply(boolean fail) } /** - * Unique string/number used to identify messages using {@link Message#getNonce()} in events. - *
This can be useful to handle round-trip messages. + * Unique string/number used to identify messages using {@link Message#getNonce()} in {@link MessageReceivedEvent}. * - *

Discord also uses the nonce to dedupe messages for users, but this is not currently supported for bots. - * However, for future proofing, it is highly recommended to use a unique nonce for each message. + *

The nonce can be used for deduping messages and marking them for use with {@link MessageReceivedEvent}. + * JDA will automatically generate a unique nonce per message, it is not necessary to do this manually. * * @param nonce - * The nonce string to use + * The nonce string to use, must be unique per message. + * A unique nonce will be generated automatically if this is null. * * @throws IllegalArgumentException * If the provided nonce is longer than {@value Message#MAX_NONCE_LENGTH} characters diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageCreateActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageCreateActionImpl.java index 395366df56..6f80cd8167 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageCreateActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/MessageCreateActionImpl.java @@ -35,6 +35,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -43,6 +44,7 @@ public class MessageCreateActionImpl extends RestActionImpl implements MessageCreateAction, MessageCreateBuilderMixin { + protected static final SecureRandom nonceGenerator = new SecureRandom(); protected static boolean defaultFailOnInvalidReply = false; private final MessageChannel channel; @@ -83,8 +85,11 @@ protected RequestBody finalizeData() try (MessageCreateData data = builder.build()) { DataObject json = data.toData(); - if (nonce != null) + json.put("enforce_nonce", true); + if (nonce != null && !nonce.isEmpty()) json.put("nonce", nonce); + else + json.put("nonce", Long.toUnsignedString(nonceGenerator.nextLong())); if (stickers != null && !stickers.isEmpty()) json.put("sticker_ids", stickers); if (messageReferenceId != null)