16
16
import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
17
17
import org .slf4j .Logger ;
18
18
19
+ import javax .annotation .Nonnull ;
19
20
import java .lang .reflect .InvocationTargetException ;
20
21
import java .lang .reflect .Method ;
21
22
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 .*;
26
24
27
25
public class SlashCommandInfo extends Cooldownable {
28
26
private static final Logger LOGGER = Logging .getLogger ();
@@ -43,6 +41,9 @@ public class SlashCommandInfo extends Cooldownable {
43
41
private final String path ;
44
42
45
43
private int pathComponents = 1 ;
44
+
45
+ /** guild id => localized option names */
46
+ private final Map <Long , List <String >> localizedOptionMap = new HashMap <>();
46
47
47
48
public SlashCommandInfo (SlashCommand slashCommand , Method commandMethod ) {
48
49
super (commandMethod .getAnnotation (JdaSlashCommand .class ).cooldownScope (), commandMethod .getAnnotation (JdaSlashCommand .class ).cooldown ());
@@ -114,6 +115,10 @@ public SlashCommandInfo(SlashCommand slashCommand, Method commandMethod) {
114
115
115
116
this .ownerOnly = commandMethod .isAnnotationPresent (RequireOwner .class );
116
117
}
118
+
119
+ public void putLocalizedOptions (long guildId , @ Nonnull List <String > optionNames ) {
120
+ localizedOptionMap .put (guildId , optionNames );
121
+ }
117
122
118
123
public int getPathComponents () {
119
124
return pathComponents ;
@@ -164,14 +169,20 @@ public boolean execute(BContext context, SlashCommandEvent event) {
164
169
}
165
170
}};
166
171
172
+ final List <String > optionNames = event .getGuild () != null ? localizedOptionMap .get (event .getGuild ().getIdLong ()) : null ;
167
173
for (int i = 0 , commandParametersLength = commandParameters .length ; i < commandParametersLength ; i ++) {
168
174
SlashCommandParameter parameter = commandParameters [i ];
169
175
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 );
171
182
172
183
if (optionData == null ) {
173
184
if (parameter .isOptional ()) {
174
- if (parameter .getType (). isPrimitive ()) {
185
+ if (parameter .isPrimitive ()) {
175
186
objects .add (0 );
176
187
} else {
177
188
objects .add (null );
0 commit comments