Skip to content

Commit 246ffe8

Browse files
committed
Fix option resolving
1 parent 7e9d728 commit 246ffe8

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/main/java/com/freya02/botcommands/slash/SlashCommandInfo.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1717
import org.slf4j.Logger;
1818

19+
import javax.annotation.Nonnull;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Parameter;
22-
import java.util.ArrayList;
23-
import java.util.Collections;
24-
import java.util.EnumSet;
25-
import java.util.List;
23+
import java.util.*;
2624

2725
public class SlashCommandInfo extends Cooldownable {
2826
private static final Logger LOGGER = Logging.getLogger();
@@ -43,6 +41,9 @@ public class SlashCommandInfo extends Cooldownable {
4341
private final String path;
4442

4543
private int pathComponents = 1;
44+
45+
/** guild id => localized option names */
46+
private final Map<Long, List<String>> localizedOptionMap = new HashMap<>();
4647

4748
public SlashCommandInfo(SlashCommand slashCommand, Method commandMethod) {
4849
super(commandMethod.getAnnotation(JdaSlashCommand.class).cooldownScope(), commandMethod.getAnnotation(JdaSlashCommand.class).cooldown());
@@ -114,6 +115,10 @@ public SlashCommandInfo(SlashCommand slashCommand, Method commandMethod) {
114115

115116
this.ownerOnly = commandMethod.isAnnotationPresent(RequireOwner.class);
116117
}
118+
119+
public void putLocalizedOptions(long guildId, @Nonnull List<String> optionNames) {
120+
localizedOptionMap.put(guildId, optionNames);
121+
}
117122

118123
public int getPathComponents() {
119124
return pathComponents;
@@ -164,14 +169,20 @@ public boolean execute(BContext context, SlashCommandEvent event) {
164169
}
165170
}};
166171

172+
final List<String> optionNames = event.getGuild() != null ? localizedOptionMap.get(event.getGuild().getIdLong()) : null;
167173
for (int i = 0, commandParametersLength = commandParameters.length; i < commandParametersLength; i++) {
168174
SlashCommandParameter parameter = commandParameters[i];
169175

170-
final OptionMapping optionData = event.getOptions().get(i);
176+
String optionName = optionNames == null ? parameter.getEffectiveName() : optionNames.get(i);
177+
if (optionName == null) {
178+
throw new IllegalArgumentException(String.format("Option name #%d (%s) could not be resolved for %s", i, parameter.getEffectiveName(), getCommandMethod()));
179+
}
180+
181+
final OptionMapping optionData = event.getOption(optionName);
171182

172183
if (optionData == null) {
173184
if (parameter.isOptional()) {
174-
if (parameter.getType().isPrimitive()) {
185+
if (parameter.isPrimitive()) {
175186
objects.add(0);
176187
} else {
177188
objects.add(null);

src/main/java/com/freya02/botcommands/slash/SlashCommandParameter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.freya02.botcommands.parameters.SlashParameterResolver;
77

88
public class SlashCommandParameter {
9-
private final boolean optional;
9+
private final boolean optional, primitive;
1010
private final String effectiveName;
1111
private final SlashParameterResolver resolver;
1212
private final Class<?> type;
@@ -15,6 +15,7 @@ public SlashCommandParameter(boolean optional, String effectiveName, Class<?> ty
1515
this.optional = optional;
1616
this.effectiveName = effectiveName;
1717

18+
this.primitive = type.isPrimitive();
1819
this.type = Utils.getBoxedType(type);
1920
ParameterResolver resolver = ParameterResolvers.of(this.type);
2021
if (resolver == null) {
@@ -26,6 +27,10 @@ public SlashCommandParameter(boolean optional, String effectiveName, Class<?> ty
2627
this.resolver = (SlashParameterResolver) resolver;
2728
}
2829

30+
public boolean isPrimitive() {
31+
return primitive;
32+
}
33+
2934
public Class<?> getType() {
3035
return type;
3136
}

src/main/java/com/freya02/botcommands/slash/SlashCommandsUpdater.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ private void computeCommands(@NotNull BContextImpl context, @Nullable Guild guil
142142
try {
143143
final List<String> optionNames = getMethodOptionNames(info);
144144
final LocalizedSlashCommandData localizedCommandData = getLocalizedCommandData(guild, info, optionNames);
145+
146+
//Put localized option names in order to resolve them when called
147+
if (guild != null) {
148+
info.putLocalizedOptions(guild.getIdLong(), optionNames);
149+
}
145150

146151
final List<OptionData> methodOptions = getMethodOptions(info, localizedCommandData);
147152

0 commit comments

Comments
 (0)