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 SUPPRESS_NOTIFICATIONS flag for message #2393

Merged
merged 12 commits into from
Feb 12, 2023
16 changes: 15 additions & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,15 @@ default MessageCreateAction replyFiles(@Nonnull Collection<? extends FileUpload>
*/
boolean isEphemeral();

/**
* Whether this message is silent.
* <br>The message being silent means it will not trigger push and desktop notifications
* <br>This is a shortcut method for checking if {@link #getFlags()} contains {@link MessageFlag#NOTIFICATIONS_SUPPRESSED}
*
* @return Whether the message is silent
*/
boolean isSilent();
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a possibly {@code null} {@link ThreadChannel ThreadChannel} that was started from this message.
* This can be {@code null} due to no ThreadChannel being started from it or the ThreadChannel later being deleted.
Expand Down Expand Up @@ -2129,7 +2138,12 @@ enum MessageFlag
/**
* Indicates, that this Message is an interaction response and the bot is "thinking"
*/
LOADING(7);
LOADING(7),
/**
* Indicates, that this message will not trigger push and desktop notifications
* @see Message#isSilent
*/
NOTIFICATIONS_SUPPRESSED(12);

private final int value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public R clear()
this.components.clear();
this.content.setLength(0);
this.mentions.clear();
this.messageFlags = 0;
return (R) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ public MessageCreateBuilder setTTS(boolean tts)
return this;
}

@Nonnull
@Override
public MessageCreateBuilder setSilent(boolean silent)
{
if(silent)
messageFlags |= Message.MessageFlag.NOTIFICATIONS_SUPPRESSED.getValue();
else
messageFlags &= ~Message.MessageFlag.NOTIFICATIONS_SUPPRESSED.getValue();
return this;
}

@Override
public boolean isEmpty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ public boolean isTTS()
return tts;
}

/**
* Whether this message is silent.
*
* @return True, if the message will not trigger push and desktop notifications
*/
public boolean isSilent()
{
return (flags & Message.MessageFlag.NOTIFICATIONS_SUPPRESSED.getValue()) != 0;
}

/**
* The IDs for users which are allowed to be mentioned, or an empty list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ default R addFiles(@Nonnull FileUpload... files)
@Nonnull
R setTTS(boolean tts);

/**
* Set whether this message should trigger push/desktop notifications to other users.
* <br>When a message is silent, it will not trigger push/desktop notifications.
*
* @param silent
* True, if this message should not trigger push/desktop notifications
*
* @return The same reply action, for chaining convenience
*/
@Nonnull
R setSilent(boolean silent);

/**
* Applies the provided {@link MessageCreateData} to this request.
*
Expand All @@ -336,6 +348,7 @@ default R applyData(@Nonnull MessageCreateData data)
.mentionRepliedUser(data.isMentionRepliedUser())
.setEmbeds(data.getEmbeds())
.setTTS(data.isTTS())
.setSilent(data.isSilent())
.setComponents(layoutComponents)
.setFiles(data.getFiles());
}
Expand All @@ -352,6 +365,7 @@ default R applyMessage(@Nonnull Message message)
return setContent(message.getContentRaw())
.setEmbeds(embeds)
.setTTS(message.isTTS())
.setSilent(message.isSilent())
.setComponents(message.getActionRows());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@ public boolean isEphemeral()
return false;
}

@Override
public boolean isSilent()
{
unsupported();
return false;
}

@Nullable
@Override
public ThreadChannel getStartedThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,12 @@ public boolean isEphemeral()
return (this.flags & MessageFlag.EPHEMERAL.getValue()) != 0;
}

@Override
public boolean isSilent()
{
return (this.flags & MessageFlag.NOTIFICATIONS_SUPPRESSED.getValue()) != 0;
}

@Nullable
@Override
public ThreadChannel getStartedThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ default List<FileUpload> getAttachments()
{
return getBuilder().getAttachments();
}

@Nonnull
@Override
default R setSilent(boolean silent)
{
getBuilder().setSilent(silent);
return (R) this;
}
}