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

Implemented ApplicationManager #2740

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/net/dv8tion/jda/api/JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.managers.ApplicationManager;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.DirectAudioController;
import net.dv8tion.jda.api.managers.Presence;
Expand Down Expand Up @@ -2214,4 +2215,13 @@ default AuditableRestAction<Integer> installAuxiliaryPort()
else throw new IllegalStateException("No port available");
return new CompletedRestAction<>(this, port);
}

/**
* Returns the {@link net.dv8tion.jda.api.managers.ApplicationManager} that manages the application associated with the bot.
*
* @return The corresponding ApplicationManager
*/
@Nonnull
ApplicationManager getApplicationManager();

}
104 changes: 104 additions & 0 deletions src/main/java/net/dv8tion/jda/api/managers/ApplicationManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package net.dv8tion.jda.api.managers;

import net.dv8tion.jda.api.entities.ApplicationInfo;
import net.dv8tion.jda.api.entities.Icon;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

/**
* Implemented only description, icon and cover_image.
* <br>For more fields check Discord docs: <a href="https://discord.com/developers/docs/resources/application#edit-current-application">https://discord.com/developers/docs/resources/application#edit-current-application</a>
*/
public interface ApplicationManager extends Manager<ApplicationManager>
{

/**
* Used to set description
*/
long DESCRIPTION = 1;
/**
* Used to set icon
*/
long ICON = 1 << 1;
/**
* Used to set cover_image
*/
long COVER_IMAGE = 1 << 2;

/**
* The {@link net.dv8tion.jda.api.entities.ApplicationInfo ApplicationInfo} associated to the bot.
*
* @return The corresponding ApplicationInfo
*/
@Nonnull
ApplicationInfo getApplicationInfo();

/**
* Resets the fields specified by the provided bit-flag pattern.
* You can specify a combination by using a bitwise OR concat of the flag constants.
* <br>Example: {@code manager.reset(ApplicationManager.ICON | ApplicationManager.COVER_IMAGE);}
* <br>For all flags, check {@link ApplicationManager} fields.
* @param fields
* Integer value containing the flags to reset.
*
* @return ApplicationManager for chaining convenience
*/
@Nonnull
@Override
@CheckReturnValue
ApplicationManager reset(long fields);

/**
* Resets the fields specified by the provided bit-flag patterns.
* <br>Example: {@code manager.reset(ApplicationManager.ICON, ApplicationManager.COVER_IMAGE);}
* <br>For all flags, check {@link ApplicationManager} fields.
*
* @param fields
* Integer values containing the flags to reset.
*
* @return ApplicationManager for chaining convenience
*/
@Nonnull
@Override
@CheckReturnValue
ApplicationManager reset(long... fields);


/**
* Sets the description of the application.
*
* @param description
* The new description
*
* @return ApplicationManager for chaining convenience
*/
@Nonnull
@CheckReturnValue
ApplicationManager setDescription(@Nonnull String description);

/**
* Sets the icon of the application.
*
* @param icon
* The new icon
*
* @return ApplicationManager for chaining convenience
*/
@Nonnull
@CheckReturnValue
ApplicationManager setIcon(@Nonnull Icon icon);

/**
* Sets the cover image of the application.
*
* @param coverImage
* The new coverImage
*
* @return ApplicationManager for chaining convenience
*/
@Nonnull
@CheckReturnValue
ApplicationManager setCoverImage(@Nonnull Icon coverImage);

}
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/requests/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static class Misc
public static class Applications
{
public static final Route GET_BOT_APPLICATION = new Route(GET, "oauth2/applications/@me");
public static final Route EDIT_BOT_APPLICATION = new Route(PATCH, "applications/@me");
public static final Route GET_ROLE_CONNECTION_METADATA = new Route(GET, "applications/{application_id}/role-connections/metadata");
public static final Route UPDATE_ROLE_CONNECTION_METADATA = new Route(PUT, "applications/{application_id}/role-connections/metadata");
public static final Route GET_ENTITLEMENTS = new Route(GET, "applications/{application_id}/entitlements");
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/JDAImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.managers.ApplicationManager;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.Presence;
import net.dv8tion.jda.api.requests.*;
Expand All @@ -65,6 +66,7 @@
import net.dv8tion.jda.internal.hooks.EventManagerProxy;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import net.dv8tion.jda.internal.interactions.command.CommandImpl;
import net.dv8tion.jda.internal.managers.ApplicationManagerImpl;
import net.dv8tion.jda.internal.managers.AudioManagerImpl;
import net.dv8tion.jda.internal.managers.DirectAudioControllerImpl;
import net.dv8tion.jda.internal.managers.PresenceImpl;
Expand All @@ -82,6 +84,7 @@
import net.dv8tion.jda.internal.utils.config.ThreadingConfig;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.MDC;

Expand Down Expand Up @@ -142,6 +145,8 @@ public class JDAImpl implements JDA
protected final AtomicBoolean requesterShutdown = new AtomicBoolean(false);
protected final AtomicReference<ShutdownEvent> shutdownEvent = new AtomicReference<>(null);

protected final ApplicationManager applicationManager;

public JDAImpl(AuthorizationConfig authConfig)
{
this(authConfig, null, null, null, null);
Expand All @@ -162,6 +167,7 @@ public JDAImpl(
this.audioController = new DirectAudioControllerImpl(this);
this.eventCache = new EventCache();
this.eventManager = new EventManagerProxy(new InterfacedEventManager(), this.threadConfig.getEventPool());
this.applicationManager = new ApplicationManagerImpl(this);
}

public void handleEvent(@Nonnull GenericEvent event)
Expand Down Expand Up @@ -1154,6 +1160,13 @@ public RestAction<Webhook> retrieveWebhookById(@Nonnull String webhookId)
});
}

@Nonnull
@Override
public ApplicationManager getApplicationManager()
{
return this.applicationManager;
}

@Nonnull
@Override
public RestAction<ApplicationInfo> retrieveApplicationInfo()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package net.dv8tion.jda.internal.managers;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.ApplicationInfo;
import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.managers.ApplicationManager;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.utils.data.DataObject;
import okhttp3.RequestBody;
import org.jetbrains.annotations.NotNull;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

/**
* When implement new fields, update also {@link #reset(long)}, {@link #reset(long...)}, {@link #reset()} and {@link #finalizeData()}
*/
public class ApplicationManagerImpl extends ManagerBase<ApplicationManager> implements ApplicationManager
{

protected String description;
protected Icon icon;
protected Icon coverImage;

public ApplicationManagerImpl(JDA jda)
{
super(jda, Route.Applications.EDIT_BOT_APPLICATION.compile());
}

@NotNull
@Override
public ApplicationInfo getApplicationInfo()
{
return this.getJDA().retrieveApplicationInfo().complete();
}

@Nonnull
@Override
@CheckReturnValue
public ApplicationManagerImpl reset(long fields)
{
super.reset(fields);

if((fields & DESCRIPTION) == DESCRIPTION)
description = null;
if((fields & ICON) == ICON)
icon = null;
if((fields & COVER_IMAGE) == COVER_IMAGE)
coverImage = null;

return this;
}

@Nonnull
@Override
@CheckReturnValue
public ApplicationManagerImpl reset(long... fields)
{
super.reset(fields);

for(long field : fields)
{

if(field == DESCRIPTION)
description = null;
else if(field == ICON)
icon = null;
else if(field == COVER_IMAGE)
coverImage = null;

}

return this;
}

@Nonnull
@Override
@CheckReturnValue
public ApplicationManager reset()
{
super.reset();
this.description = null;
this.icon = null;
this.coverImage = null;
return this;
}

@NotNull
@Override
public ApplicationManager setDescription(@NotNull String description)
{
this.description = description.trim();
set |= DESCRIPTION;
return this;
}

@NotNull
@Override
public ApplicationManager setIcon(@NotNull Icon icon)
{
this.icon = icon;
set |= ICON;
return this;
}

@NotNull
@Override
public ApplicationManager setCoverImage(@NotNull Icon coverImage)
{
this.coverImage = coverImage;
set |= COVER_IMAGE;
return this;
}

@Override
protected RequestBody finalizeData()
{
DataObject body = DataObject.empty();

if(shouldUpdate(DESCRIPTION))
body.put("description", this.description);
if(shouldUpdate(ICON))
body.put("icon", this.icon == null ? null : this.icon.getEncoding());
if(shouldUpdate(COVER_IMAGE))
body.put("cover_image", this.coverImage == null ? null : this.coverImage.getEncoding());

reset();
return getRequestBody(body);
}

@Override
protected void handleSuccess(Response response, Request<Void> request)
{
request.onSuccess(null);
}

}
Loading