Skip to content

Commit ddd678b

Browse files
committed
Adapt user-apps changes
1 parent cc9b1f0 commit ddd678b

File tree

3 files changed

+200
-6
lines changed

3 files changed

+200
-6
lines changed

src/main/java/net/dv8tion/jda/api/interactions/commands/build/EntryPointCommandData.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
package net.dv8tion.jda.api.interactions.commands.build;
1818

1919
import net.dv8tion.jda.api.interactions.DiscordLocale;
20+
import net.dv8tion.jda.api.interactions.IntegrationType;
21+
import net.dv8tion.jda.api.interactions.InteractionContextType;
2022
import net.dv8tion.jda.api.interactions.commands.Command;
2123
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
2224
import net.dv8tion.jda.api.interactions.commands.build.attributes.*;
2325
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
2426
import net.dv8tion.jda.api.requests.restaction.GlobalCommandListUpdateAction;
27+
import net.dv8tion.jda.api.utils.data.DataArray;
2528
import net.dv8tion.jda.api.utils.data.DataObject;
2629
import net.dv8tion.jda.api.utils.data.SerializableData;
2730
import net.dv8tion.jda.internal.interactions.EntryPointCommandDataImpl;
2831
import net.dv8tion.jda.internal.utils.Checks;
32+
import net.dv8tion.jda.internal.utils.Helpers;
2933
import net.dv8tion.jda.internal.utils.localization.LocalizationUtils;
3034

3135
import javax.annotation.Nonnull;
36+
import java.util.Collection;
3237
import java.util.Map;
3338

3439
/**
@@ -79,6 +84,28 @@ public interface EntryPointCommandData
7984
@Override
8085
EntryPointCommandData setGuildOnly(boolean guildOnly);
8186

87+
@Nonnull
88+
@Override
89+
default EntryPointCommandData setContexts(@Nonnull InteractionContextType... contexts)
90+
{
91+
return (EntryPointCommandData) IScopedCommandData.super.setContexts(contexts);
92+
}
93+
94+
@Nonnull
95+
@Override
96+
EntryPointCommandData setContexts(@Nonnull Collection<InteractionContextType> contexts);
97+
98+
@Nonnull
99+
@Override
100+
default EntryPointCommandData setIntegrationTypes(@Nonnull IntegrationType... integrationTypes)
101+
{
102+
return (EntryPointCommandData) IScopedCommandData.super.setIntegrationTypes(integrationTypes);
103+
}
104+
105+
@Nonnull
106+
@Override
107+
EntryPointCommandData setIntegrationTypes(@Nonnull Collection<IntegrationType> integrationTypes);
108+
82109
@Nonnull
83110
@Override
84111
EntryPointCommandData setNSFW(boolean nsfw);
@@ -115,7 +142,8 @@ static EntryPointCommandData fromCommand(@Nonnull Command command)
115142
throw new IllegalArgumentException("Cannot convert command of type " + command.getType() + " to EntryPointCommandData!");
116143

117144
EntryPointCommandDataImpl data = new EntryPointCommandDataImpl(command.getName(), command.getDescription());
118-
data.setGuildOnly(command.isGuildOnly());
145+
data.setContexts(command.getContexts());
146+
data.setIntegrationTypes(command.getIntegrationTypes());
119147
data.setNSFW(command.isNSFW());
120148
data.setDefaultPermissions(command.getDefaultPermissions());
121149
//Command localizations are unmodifiable, make a copy
@@ -150,7 +178,28 @@ static EntryPointCommandData fromData(@Nonnull DataObject object)
150178
String name = object.getString("name");
151179
String description = object.getString("description");
152180
EntryPointCommandDataImpl command = new EntryPointCommandDataImpl(name, description);
153-
command.setGuildOnly(!object.getBoolean("dm_permission", true));
181+
if (!object.isNull("contexts"))
182+
{
183+
command.setContexts(object.getArray("contexts")
184+
.stream(DataArray::getString)
185+
.map(InteractionContextType::fromKey)
186+
.collect(Helpers.toUnmodifiableEnumSet(InteractionContextType.class)));
187+
}
188+
else if (!object.isNull("dm_permission"))
189+
command.setGuildOnly(!object.getBoolean("dm_permission"));
190+
else
191+
command.setContexts(Helpers.unmodifiableEnumSet(InteractionContextType.GUILD, InteractionContextType.BOT_DM));
192+
193+
if (!object.isNull("integration_types"))
194+
{
195+
command.setIntegrationTypes(object.getArray("integration_types")
196+
.stream(DataArray::getString)
197+
.map(IntegrationType::fromKey)
198+
.collect(Helpers.toUnmodifiableEnumSet(IntegrationType.class)));
199+
}
200+
else
201+
command.setIntegrationTypes(Helpers.unmodifiableEnumSet(IntegrationType.GUILD_INSTALL));
202+
154203
command.setNSFW(object.getBoolean("nsfw"));
155204

156205
command.setDefaultPermissions(

src/main/java/net/dv8tion/jda/api/interactions/commands/build/attributes/IScopedCommandData.java

+103
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616

1717
package net.dv8tion.jda.api.interactions.commands.build.attributes;
1818

19+
import net.dv8tion.jda.annotations.ReplaceWith;
20+
import net.dv8tion.jda.api.interactions.IntegrationType;
21+
import net.dv8tion.jda.api.interactions.InteractionContextType;
22+
import net.dv8tion.jda.internal.utils.Checks;
23+
import org.jetbrains.annotations.UnmodifiableView;
24+
1925
import javax.annotation.Nonnull;
26+
import java.util.Arrays;
27+
import java.util.Collection;
28+
import java.util.Set;
2029

2130
/**
2231
* Builder for scoped Application Commands.
@@ -35,15 +44,109 @@ public interface IScopedCommandData extends INamedCommandData
3544
* Whether to restrict this command to guilds
3645
*
3746
* @return The builder instance, for chaining
47+
*
48+
* @deprecated Replaced with {@link #setContexts(InteractionContextType...)}
3849
*/
3950
@Nonnull
51+
@Deprecated
52+
@ReplaceWith("setContexts(InteractionContextType.GUILD)")
4053
IScopedCommandData setGuildOnly(boolean guildOnly);
4154

4255
/**
4356
* Whether the command can only be used inside a guild.
4457
* <br>Always true for guild commands.
4558
*
4659
* @return True, if this command is restricted to guilds.
60+
*
61+
* @deprecated Replaced with {@link #getContexts()}
4762
*/
63+
@Deprecated
64+
@ReplaceWith("getContexts().equals(EnumSet.of(InteractionContextType.GUILD))")
4865
boolean isGuildOnly();
66+
67+
/**
68+
* Sets the contexts in which this command can be used (Default: Guild and Bot DMs).
69+
* <br>This only has an effect if this command is registered globally.
70+
*
71+
* @param contexts
72+
* The contexts in which this command can be used
73+
*
74+
* @throws IllegalArgumentException
75+
* If {@code null} or no interaction context types were passed
76+
*
77+
* @return The builder instance, for chaining
78+
*/
79+
@Nonnull
80+
default IScopedCommandData setContexts(@Nonnull InteractionContextType... contexts)
81+
{
82+
Checks.notEmpty(contexts, "Contexts");
83+
return setContexts(Arrays.asList(contexts));
84+
}
85+
86+
/**
87+
* Sets the contexts in which this command can be used (Default: Guild and Bot DMs).
88+
* <br>This only has an effect if this command is registered globally.
89+
*
90+
* @param contexts
91+
* The contexts in which this command can be used
92+
*
93+
* @throws IllegalArgumentException
94+
* If {@code null} or no interaction context types were passed
95+
*
96+
* @return The builder instance, for chaining
97+
*/
98+
@Nonnull
99+
IScopedCommandData setContexts(@Nonnull Collection<InteractionContextType> contexts);
100+
101+
/**
102+
* Sets the integration types on which this command can be installed on (Default: Guilds).
103+
* <br>This only has an effect if this command is registered globally.
104+
*
105+
* @param integrationTypes
106+
* The integration types on which this command can be installed on
107+
*
108+
* @throws IllegalArgumentException
109+
* If {@code null} or no integration types were passed
110+
*
111+
* @return The builder instance, for chaining
112+
*/
113+
@Nonnull
114+
default IScopedCommandData setIntegrationTypes(@Nonnull IntegrationType... integrationTypes)
115+
{
116+
Checks.notEmpty(integrationTypes, "Integration types");
117+
return setIntegrationTypes(Arrays.asList(integrationTypes));
118+
}
119+
120+
/**
121+
* Sets the integration types on which this command can be installed on (Default: Guilds).
122+
* <br>This only has an effect if this command is registered globally.
123+
*
124+
* @param integrationTypes
125+
* The integration types on which this command can be installed on
126+
*
127+
* @throws IllegalArgumentException
128+
* If {@code null} or no integration types were passed
129+
*
130+
* @return The builder instance, for chaining
131+
*/
132+
@Nonnull
133+
IScopedCommandData setIntegrationTypes(@Nonnull Collection<IntegrationType> integrationTypes);
134+
135+
/**
136+
* The contexts in which this command can be used.
137+
*
138+
* @return The contexts in which this command can be used
139+
*/
140+
@Nonnull
141+
@UnmodifiableView
142+
Set<InteractionContextType> getContexts();
143+
144+
/**
145+
* Gets the integration types on which this command can be installed on.
146+
*
147+
* @return The integration types on which this command can be installed on
148+
*/
149+
@Nonnull
150+
@UnmodifiableView
151+
Set<IntegrationType> getIntegrationTypes();
49152
}

src/main/java/net/dv8tion/jda/internal/interactions/EntryPointCommandDataImpl.java

+46-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package net.dv8tion.jda.internal.interactions;
1818

1919
import net.dv8tion.jda.api.interactions.DiscordLocale;
20+
import net.dv8tion.jda.api.interactions.IntegrationType;
21+
import net.dv8tion.jda.api.interactions.InteractionContextType;
2022
import net.dv8tion.jda.api.interactions.commands.Command;
2123
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
2224
import net.dv8tion.jda.api.interactions.commands.build.EntryPointCommandData;
@@ -27,9 +29,13 @@
2729
import net.dv8tion.jda.internal.interactions.command.localization.LocalizationMapper;
2830
import net.dv8tion.jda.internal.interactions.mixin.attributes.IDescribedCommandDataMixin;
2931
import net.dv8tion.jda.internal.utils.Checks;
32+
import net.dv8tion.jda.internal.utils.Helpers;
3033

3134
import javax.annotation.Nonnull;
35+
import java.util.Collection;
36+
import java.util.EnumSet;
3237
import java.util.Map;
38+
import java.util.stream.Collectors;
3339

3440
public class EntryPointCommandDataImpl implements EntryPointCommandData, IDescribedCommandDataMixin
3541
{
@@ -38,7 +44,8 @@ public class EntryPointCommandDataImpl implements EntryPointCommandData, IDescri
3844
private final LocalizationMap nameLocalizations = new LocalizationMap(this::checkName);
3945
private final LocalizationMap descriptionLocalizations = new LocalizationMap(this::checkDescription);
4046

41-
private boolean guildOnly = false;
47+
private EnumSet<InteractionContextType> contexts = EnumSet.of(InteractionContextType.GUILD, InteractionContextType.BOT_DM);
48+
private EnumSet<IntegrationType> integrationTypes = EnumSet.of(IntegrationType.GUILD_INSTALL);
4249
private boolean nsfw = false;
4350
private DefaultMemberPermissions defaultMemberPermissions = DefaultMemberPermissions.ENABLED;
4451
private Handler handler;
@@ -62,7 +69,8 @@ public DataObject toData()
6269
.put("description", description)
6370
.put("description_localizations", descriptionLocalizations)
6471
.put("nsfw", nsfw)
65-
.put("dm_permission", !guildOnly)
72+
.put("contexts", contexts.stream().map(InteractionContextType::getType).collect(Collectors.toList()))
73+
.put("integration_types", integrationTypes.stream().map(IntegrationType::getType).collect(Collectors.toList()))
6674
.put("default_member_permissions", defaultMemberPermissions == DefaultMemberPermissions.ENABLED
6775
? null
6876
: Long.toUnsignedString(defaultMemberPermissions.getPermissionsRaw()))
@@ -86,7 +94,21 @@ public DefaultMemberPermissions getDefaultPermissions()
8694
@Override
8795
public boolean isGuildOnly()
8896
{
89-
return guildOnly;
97+
return contexts.size() == 1 && contexts.contains(InteractionContextType.GUILD);
98+
}
99+
100+
@Nonnull
101+
@Override
102+
public EnumSet<InteractionContextType> getContexts()
103+
{
104+
return contexts;
105+
}
106+
107+
@Nonnull
108+
@Override
109+
public EnumSet<IntegrationType> getIntegrationTypes()
110+
{
111+
return integrationTypes;
90112
}
91113

92114
@Override
@@ -108,7 +130,27 @@ public EntryPointCommandDataImpl setDefaultPermissions(@Nonnull DefaultMemberPer
108130
@Override
109131
public EntryPointCommandDataImpl setGuildOnly(boolean guildOnly)
110132
{
111-
this.guildOnly = guildOnly;
133+
setContexts(guildOnly
134+
? EnumSet.of(InteractionContextType.GUILD)
135+
: EnumSet.of(InteractionContextType.GUILD, InteractionContextType.BOT_DM));
136+
return this;
137+
}
138+
139+
@Nonnull
140+
@Override
141+
public EntryPointCommandDataImpl setContexts(@Nonnull Collection<InteractionContextType> contexts)
142+
{
143+
Checks.notEmpty(contexts, "Contexts");
144+
this.contexts = Helpers.copyEnumSet(InteractionContextType.class, contexts);
145+
return this;
146+
}
147+
148+
@Nonnull
149+
@Override
150+
public EntryPointCommandDataImpl setIntegrationTypes(@Nonnull Collection<IntegrationType> integrationTypes)
151+
{
152+
Checks.notEmpty(contexts, "Contexts");
153+
this.integrationTypes = Helpers.copyEnumSet(IntegrationType.class, integrationTypes);
112154
return this;
113155
}
114156

0 commit comments

Comments
 (0)