Skip to content

Commit

Permalink
Default allowed mentions (#1293)
Browse files Browse the repository at this point in the history
Add default allowed mentions
  • Loading branch information
TheKodeToad committed Jan 22, 2024
1 parent f93c822 commit d752eed
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 17 deletions.
18 changes: 18 additions & 0 deletions javacord-api/src/main/java/org/javacord/api/DiscordApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.javacord.api.entity.emoji.KnownCustomEmoji;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.MessageSet;
import org.javacord.api.entity.message.UncachedMessageUtil;
import org.javacord.api.entity.message.mention.AllowedMentions;
import org.javacord.api.entity.permission.Permissions;
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.server.Server;
Expand Down Expand Up @@ -115,6 +117,22 @@ public interface DiscordApi extends GloballyAttachableListenerManager {
*/
boolean canDispatchEvents();

/**
* Controls who will be mentioned if mentions exist in a message.
* This is overriden with {@link MessageBuilder#setAllowedMentions(AllowedMentions)}
*
* @param allowedMentions The mention object.
*/
void setDefaultAllowedMentions(AllowedMentions allowedMentions);

/**
* Controls who will be mentioned if mentions exist in a message.
* This is overriden with {@link MessageBuilder#setAllowedMentions(AllowedMentions)}
*
* @return The mention object.
*/
Optional<AllowedMentions> getDefaultAllowedMentions();

/**
* Gets all global commands for the application.
*
Expand Down
14 changes: 14 additions & 0 deletions javacord-api/src/main/java/org/javacord/api/DiscordApiBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.javacord.api;

import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.mention.AllowedMentions;
import org.javacord.api.event.server.ServerBecomesAvailableEvent;
import org.javacord.api.internal.DiscordApiBuilderDelegate;
import org.javacord.api.listener.ChainableGloballyAttachableListenerManager;
Expand Down Expand Up @@ -450,6 +452,18 @@ public DiscordApiBuilder setAllIntentsWhere(Predicate<Intent> condition) {
return this;
}

/**
* Controls who will be mentioned if mentions exist in a message.
* This is overriden with {@link MessageBuilder#setAllowedMentions(AllowedMentions)}
*
* @param allowedMentions The mention object.
* @return The current instance in order to chain call methods.
*/
public DiscordApiBuilder setDefaultAllowedMentions(AllowedMentions allowedMentions) {
delegate.setDefaultAllowedMentions(allowedMentions);
return this;
}

/**
* Sets whether the user cache should be enabled.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.javacord.api.entity.message;

import org.javacord.api.DiscordApi;
import org.javacord.api.entity.Icon;
import org.javacord.api.entity.Mentionable;
import org.javacord.api.entity.message.component.HighLevelComponent;
Expand Down Expand Up @@ -518,6 +519,7 @@ public T addAttachmentAsSpoiler(InputStream stream, String fileName, String desc

/**
* Controls who will be mentioned if mentions exist in the message.
* Defaults to {@link DiscordApi#getDefaultAllowedMentions()}.
*
* @param allowedMentions The mention object.
* @return The current instance in order to chain call methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.mention.AllowedMentions;
import org.javacord.api.listener.GloballyAttachableListener;
import org.javacord.api.util.auth.Authenticator;
import org.javacord.api.util.ratelimit.Ratelimiter;
Expand Down Expand Up @@ -177,6 +179,14 @@ public interface DiscordApiBuilderDelegate {
*/
void setAllIntentsWhere(Predicate<Intent> condition);

/**
* Controls who will be mentioned if mentions exist in a message.
* This is overriden with {@link MessageBuilder#setAllowedMentions(AllowedMentions)}
*
* @param allowedMentions The mention object.
*/
void setDefaultAllowedMentions(AllowedMentions allowedMentions);

/**
* Sets whether the user cache should be enabled.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.logging.log4j.Logger;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.mention.AllowedMentions;
import org.javacord.api.internal.DiscordApiBuilderDelegate;
import org.javacord.api.listener.GloballyAttachableListener;
import org.javacord.api.util.auth.Authenticator;
Expand Down Expand Up @@ -132,6 +133,11 @@ public class DiscordApiBuilderDelegateImpl implements DiscordApiBuilderDelegate
*/
private boolean userCacheEnabled = true;

/**
* Controls who will be mentioned if mentions exist in a message.
*/
private AllowedMentions allowedMentions;

/**
* The globally attachable listeners to register for every created DiscordApi instance.
*/
Expand Down Expand Up @@ -197,7 +203,8 @@ public CompletableFuture<DiscordApi> login() {
new DiscordApiImpl(token, currentShard.get(), totalShards.get(), intents,
waitForServersOnStartup, waitForUsersOnStartup, registerShutdownHook, globalRatelimiter,
gatewayIdentifyRatelimiter, proxySelector, proxy, proxyAuthenticator, trustAllCertificates,
future, null, preparedListeners, preparedUnspecifiedListeners, userCacheEnabled, dispatchEvents);
future, null, preparedListeners, preparedUnspecifiedListeners, userCacheEnabled, dispatchEvents,
allowedMentions);
}
return future;
}
Expand Down Expand Up @@ -398,6 +405,11 @@ public void setAllIntentsWhere(Predicate<Intent> condition) {
}
}

@Override
public void setDefaultAllowedMentions(AllowedMentions allowedMentions) {
this.allowedMentions = allowedMentions;
}

@Override
public void setUserCacheEnabled(boolean enabled) {
userCacheEnabled = enabled;
Expand Down
25 changes: 22 additions & 3 deletions javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageSet;
import org.javacord.api.entity.message.UncachedMessageUtil;
import org.javacord.api.entity.message.mention.AllowedMentions;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.server.invite.Invite;
import org.javacord.api.entity.sticker.Sticker;
Expand Down Expand Up @@ -254,6 +255,11 @@ public class DiscordApiImpl implements DiscordApi, DispatchQueueSelector {
*/
private boolean dispatchEvents = true;

/**
* Controls who is mentioned if a message contains a ping.
*/
private AllowedMentions defaultAllowedMentions;

/**
* Whether Javacord should wait for all servers to become available on startup or not.
*/
Expand Down Expand Up @@ -475,7 +481,7 @@ public DiscordApiImpl(
) {
this(token, currentShard, totalShards, intents, waitForServersOnStartup, waitForUsersOnStartup,
true, globalRatelimiter, gatewayIdentifyRatelimiter, proxySelector, proxy, proxyAuthenticator,
trustAllCertificates, ready, null, Collections.emptyMap(), Collections.emptyList(), false, true);
trustAllCertificates, ready, null, Collections.emptyMap(), Collections.emptyList(), false, true, null);
}

/**
Expand Down Expand Up @@ -520,7 +526,7 @@ private DiscordApiImpl(
Dns dns) {
this(token, currentShard, totalShards, intents, waitForServersOnStartup, waitForUsersOnStartup,
true, globalRatelimiter, gatewayIdentifyRatelimiter, proxySelector, proxy, proxyAuthenticator,
trustAllCertificates, ready, dns, Collections.emptyMap(), Collections.emptyList(), false, true);
trustAllCertificates, ready, dns, Collections.emptyMap(), Collections.emptyList(), false, true, null);
}

/**
Expand Down Expand Up @@ -552,6 +558,7 @@ private DiscordApiImpl(
* @param unspecifiedListeners The listeners of unspecified types to pre-register.
* @param userCacheEnabled Whether the user cache should be enabled.
* @param dispatchEvents Whether events can be dispatched.
* @param defaultAllowedMentions Controls who will be mentioned if mentions exist in a message.
*/
@SuppressWarnings("unchecked")
public DiscordApiImpl(
Expand All @@ -575,7 +582,8 @@ public DiscordApiImpl(
> listenerSourceMap,
List<Function<DiscordApi, GloballyAttachableListener>> unspecifiedListeners,
boolean userCacheEnabled,
boolean dispatchEvents
boolean dispatchEvents,
AllowedMentions defaultAllowedMentions
) {
this.token = token;
this.currentShard = currentShard;
Expand All @@ -590,6 +598,7 @@ public DiscordApiImpl(
this.trustAllCertificates = trustAllCertificates;
this.userCacheEnabled = userCacheEnabled;
this.dispatchEvents = dispatchEvents;
this.defaultAllowedMentions = defaultAllowedMentions;
this.reconnectDelayProvider = x ->
(int) Math.round(Math.pow(x, 1.5) - (1 / (1 / (0.1 * x) + 1)) * Math.pow(x, 1.5));
//Always add the GUILDS intent unless it is not required anymore for Javacord to be functional.
Expand Down Expand Up @@ -736,6 +745,16 @@ public boolean canDispatchEvents() {
return dispatchEvents;
}

@Override
public void setDefaultAllowedMentions(AllowedMentions allowedMentions) {
this.defaultAllowedMentions = allowedMentions;
}

@Override
public Optional<AllowedMentions> getDefaultAllowedMentions() {
return Optional.ofNullable(defaultAllowedMentions);
}

/**
* Gets the used {@link OkHttpClient http client} for this api instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageFlag;
import org.javacord.api.entity.message.embed.EmbedBuilder;
Expand Down Expand Up @@ -38,7 +39,7 @@ public CompletableFuture<Void> sendInitialResponse(InteractionBase interaction)
ObjectNode topBody = JsonNodeFactory.instance.objectNode();
topBody.put("type", InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE.getId());
ObjectNode body = topBody.putObject("data");
prepareInteractionWebhookBodyParts(body);
prepareInteractionWebhookBodyParts(interaction.getApi(), body);

return new RestRequest<Void>(interaction.getApi(),
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
Expand Down Expand Up @@ -85,7 +86,7 @@ public CompletableFuture<Message> sendFollowupMessage(InteractionBase interactio
public CompletableFuture<Void> updateOriginalMessage(InteractionBase interaction) {
ObjectNode topBody = JsonNodeFactory.instance.objectNode();
ObjectNode data = JsonNodeFactory.instance.objectNode();
prepareCommonWebhookMessageBodyParts(data);
prepareCommonWebhookMessageBodyParts(interaction.getApi(), data);
prepareComponents(data, true);
topBody.put("type", InteractionCallbackType.UPDATE_MESSAGE.getId());
topBody.set("data", data);
Expand Down Expand Up @@ -131,13 +132,13 @@ public void copy(InteractionBase interaction) {

private CompletableFuture<Message> executeResponse(RestRequest<Message> request) {
ObjectNode body = JsonNodeFactory.instance.objectNode();
prepareInteractionWebhookBodyParts(body);
prepareInteractionWebhookBodyParts(request.getApi(), body);

return checkForAttachmentsAndExecuteRequest(request, body);
}

private void prepareInteractionWebhookBodyParts(ObjectNode body) {
prepareCommonWebhookMessageBodyParts(body);
private void prepareInteractionWebhookBodyParts(DiscordApi api, ObjectNode body) {
prepareCommonWebhookMessageBodyParts(api, body);
prepareComponents(body, true);
if (null != messageFlags) {
body.put("flags", messageFlags.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public CompletableFuture<Message> send(TextChannel channel) {
.put("tts", tts);
body.putArray("mentions");

prepareAllowedMentions(body);
prepareAllowedMentions(channel.getApi(), body);

prepareEmbeds(body, false);

Expand Down Expand Up @@ -512,7 +512,7 @@ public CompletableFuture<Message> send(IncomingWebhook webhook) {
protected CompletableFuture<Message> send(String webhookId, String webhookToken, String displayName, URL avatarUrl,
boolean wait, DiscordApi api) {
ObjectNode body = JsonNodeFactory.instance.objectNode();
prepareCommonWebhookMessageBodyParts(body);
prepareCommonWebhookMessageBodyParts(api, body);

if (displayName != null) {
body.put("username", displayName);
Expand Down Expand Up @@ -603,7 +603,7 @@ public CompletableFuture<Message> edit(Message message, boolean updateAll) {
body.put("content", strBuilder.toString());
}

prepareAllowedMentions(body);
prepareAllowedMentions(message.getApi(), body);

prepareEmbeds(body, updateAll || embedsChanged);

Expand Down Expand Up @@ -672,9 +672,13 @@ private CompletableFuture<Message> executeRequestWithoutNewAttachments(TextChann
result -> ((DiscordApiImpl) channel.getApi()).getOrCreateMessage(channel, result.getJsonBody()));
}

private void prepareAllowedMentions(ObjectNode body) {
if (allowedMentions != null) {
((AllowedMentionsImpl) allowedMentions).toJsonNode(body.putObject("allowed_mentions"));
private void prepareAllowedMentions(DiscordApi api, ObjectNode body) {
AllowedMentions value = allowedMentions;
if (value == null) {
value = api.getDefaultAllowedMentions().orElse(null);
}
if (value != null) {
((AllowedMentionsImpl) value).toJsonNode(body.putObject("allowed_mentions"));
}
}

Expand Down Expand Up @@ -738,12 +742,12 @@ protected void addMultipartBodyToRequest(RestRequest<?> request, ObjectNode body
request.setMultipartBody(multipartBodyBuilder.build());
}

protected void prepareCommonWebhookMessageBodyParts(ObjectNode body) {
protected void prepareCommonWebhookMessageBodyParts(DiscordApi api, ObjectNode body) {
body.put("tts", this.tts);
if (strBuilder.length() != 0) {
body.put("content", strBuilder.toString());
}
prepareAllowedMentions(body);
prepareAllowedMentions(api, body);
prepareEmbeds(body, embedsChanged);
}

Expand Down

0 comments on commit d752eed

Please sign in to comment.