Skip to content

Commit ec09f79

Browse files
authored
Add support for user-installable apps (#2633)
* Add contexts and integration types * Update tests and examples * Add ApplicationInfo fields * Add Interaction fields (context, integration owners) * Add Message fields (interaction metadata) * Add IDetachableEntity * Pull up IPermissionHolder#hasPermission overloads with Collection * Add concrete entity mixins * Add Interaction#isFromAttachedGuild * Add InteractionEntityBuilder, adapt entity deserialization * Add group channel support * Add ErrorResponse#MAX_FOLLOW_UP_MESSAGES_HIT * Add user-installed slash command example * Add approximate user install count * Add Helpers#unmodifiableEnumSet * Add IntegrationOwners#isXIntegration()/getXIntegration() * Reword #setEphemeral note
1 parent f71cab1 commit ec09f79

File tree

132 files changed

+8633
-714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+8633
-714
lines changed

src/examples/java/SlashBotExample.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
2424
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
2525
import net.dv8tion.jda.api.hooks.ListenerAdapter;
26+
import net.dv8tion.jda.api.interactions.IntegrationType;
27+
import net.dv8tion.jda.api.interactions.InteractionContextType;
2628
import net.dv8tion.jda.api.interactions.InteractionHook;
2729
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
2830
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
@@ -45,7 +47,7 @@ public static void main(String[] args)
4547
.addEventListeners(new SlashBotExample())
4648
.build();
4749

48-
// These commands might take a few minutes to be active after creation/update/delete
50+
// You might need to reload your Discord client if you don't see the commands
4951
CommandListUpdateAction commands = jda.updateCommands();
5052

5153
// Moderation commands with required options
@@ -56,27 +58,33 @@ public static void main(String[] args)
5658
.addOptions(new OptionData(INTEGER, "del_days", "Delete messages from the past days.") // This is optional
5759
.setRequiredRange(0, 7)) // Only allow values between 0 and 7 (inclusive)
5860
.addOptions(new OptionData(STRING, "reason", "The ban reason to use (default: Banned by <user>)")) // optional reason
59-
.setGuildOnly(true) // This way the command can only be executed from a guild, and not the DMs
61+
.setContexts(InteractionContextType.GUILD) // This way the command can only be executed from a guild, and not the DMs
6062
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.BAN_MEMBERS)) // Only members with the BAN_MEMBERS permission are going to see this command
6163
);
6264

6365
// Simple reply commands
6466
commands.addCommands(
6567
Commands.slash("say", "Makes the bot say what you tell it to")
68+
.setContexts(InteractionContextType.ALL) // Allow the command to be used anywhere (Bot DMs, Guild, Friend DMs, Group DMs)
69+
.setIntegrationTypes(IntegrationType.ALL) // Allow the command to be installed anywhere (Guilds, Users)
6670
.addOption(STRING, "content", "What the bot should say", true) // you can add required options like this too
6771
);
6872

6973
// Commands without any inputs
7074
commands.addCommands(
7175
Commands.slash("leave", "Make the bot leave the server")
72-
.setGuildOnly(true) // this doesn't make sense in DMs
76+
// The default integration types are GUILD_INSTALL.
77+
// Can't use this in DMs, and in guilds the bot isn't in.
78+
.setContexts(InteractionContextType.GUILD)
7379
.setDefaultPermissions(DefaultMemberPermissions.DISABLED) // only admins should be able to use this command.
7480
);
7581

7682
commands.addCommands(
7783
Commands.slash("prune", "Prune messages from this channel")
7884
.addOption(INTEGER, "amount", "How many messages to prune (Default 100)") // simple optional argument
79-
.setGuildOnly(true)
85+
// The default integration types are GUILD_INSTALL.
86+
// Can't use this in DMs, and in guilds the bot isn't in.
87+
.setContexts(InteractionContextType.GUILD)
8088
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE))
8189
);
8290

src/main/java/net/dv8tion/jda/api/entities/ApplicationInfo.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
import net.dv8tion.jda.api.JDA;
2020
import net.dv8tion.jda.api.Permission;
21+
import net.dv8tion.jda.api.interactions.IntegrationType;
2122
import net.dv8tion.jda.api.utils.ImageProxy;
2223
import net.dv8tion.jda.internal.utils.Checks;
2324
import org.jetbrains.annotations.Unmodifiable;
2425

2526
import javax.annotation.Nonnull;
2627
import javax.annotation.Nullable;
27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
import java.util.EnumSet;
30-
import java.util.List;
28+
import java.util.*;
3129

3230
/**
3331
* Represents a Discord Application from its bot's point of view.
@@ -116,7 +114,7 @@ default ImageProxy getIcon()
116114

117115
/**
118116
* Configures the required scopes applied to the {@link #getInviteUrl(Permission...)} and similar methods.
119-
* <br>To use slash commands you must add {@code "applications.commands"} to these scopes. The scope {@code "bot"} is always applied.
117+
* <br>The scope {@code "bot"} is always applied.
120118
*
121119
* @param scopes
122120
* The scopes to use with {@link #getInviteUrl(Permission...)} and the likes
@@ -135,7 +133,7 @@ default ApplicationInfo setRequiredScopes(@Nonnull String... scopes)
135133

136134
/**
137135
* Configures the required scopes applied to the {@link #getInviteUrl(Permission...)} and similar methods.
138-
* <br>To use slash commands you must add {@code "applications.commands"} to these scopes. The scope {@code "bot"} is always applied.
136+
* <br>The scope {@code "bot"} is always applied.
139137
*
140138
* @param scopes
141139
* The scopes to use with {@link #getInviteUrl(Permission...)} and the likes
@@ -408,6 +406,64 @@ default EnumSet<Flag> getFlags()
408406
*/
409407
long getFlagsRaw();
410408

409+
/**
410+
* The approximate count of users that have installed this application,
411+
* or {@code -1} if it is unknown.
412+
*
413+
* @return The approximate count of users that have installed this application.
414+
*/
415+
long getUserInstallCount();
416+
417+
/**
418+
* The configurations for each {@link IntegrationType} set on the application.
419+
*
420+
* @return The configurations for each integration type
421+
*/
422+
@Nonnull
423+
Map<IntegrationType, IntegrationTypeConfiguration> getIntegrationTypesConfig();
424+
425+
/**
426+
* Configuration of a single {@link IntegrationType}.
427+
*
428+
* @see ApplicationInfo#getIntegrationTypesConfig()
429+
*/
430+
interface IntegrationTypeConfiguration
431+
{
432+
/**
433+
* The OAuth2 install parameters for the default in-app authorization link.
434+
* <br>When a user invites your application in the Discord app, these will be the parameters of the invite url.
435+
*
436+
* @return The OAuth2 install parameters for the default in-app authorization link
437+
*/
438+
@Nullable
439+
InstallParameters getInstallParameters();
440+
}
441+
442+
/**
443+
* OAuth2 install parameter for the default in-app authorization link.
444+
*
445+
* @see IntegrationTypeConfiguration#getInstallParameters()
446+
*/
447+
interface InstallParameters
448+
{
449+
/**
450+
* Gets the required scopes granted to the bot when invited.
451+
*
452+
* @return The required scopes granted to the bot when invited
453+
*/
454+
@Nonnull
455+
List<String> getScopes();
456+
457+
/**
458+
* Gets the permissions your bot asks for when invited.
459+
* <br><b>Note:</b> Users can choose to disable permissions before and after inviting your bot.
460+
*
461+
* @return The permissions your bot asks for when invited
462+
*/
463+
@Nonnull
464+
Set<Permission> getPermissions();
465+
}
466+
411467
/**
412468
* Flag constants corresponding to the <a href="https://discord.com/developers/docs/resources/application#application-object-application-flags" target="_blank">Discord Enum</a>
413469
*

0 commit comments

Comments
 (0)