Skip to content

Commit

Permalink
fixed autocomplete listener
Browse files Browse the repository at this point in the history
  • Loading branch information
codejanovic committed Apr 28, 2024
1 parent 788d254 commit 66b0850
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Release artifact
<dependency>
<groupId>com.github.zrdj</groupId>
<artifactId>javachord</artifactId>
<version>3.8.0.8</version>
<version>3.8.0.9</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.zrdj</groupId>
<artifactId>javachord</artifactId>
<version>3.8.0.8</version>
<version>3.8.0.9</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/github/zrdj/javachord/Javachord.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ interface Command {
interface Option {
interface Autocomplete {
interface Optional {
static OptionalOption<String> stringOption(final String name, final String description, final Function<java.util.Optional<String>, List<Choice<String>>> autocomplete) {
return new StringAutocompleteOptionalOption(name, description) {
static OptionalOption<String> stringOption(final String name, final String description, final ApplicationCommand parent,final Function<java.util.Optional<String>, List<Choice<String>>> autocomplete) {
return new StringAutocompleteOptionalOption(name, description, parent) {

@Override
protected List<Choice<String>> onAutocompleteChoice(final java.util.Optional<String> option) {
Expand All @@ -112,8 +112,8 @@ protected List<Choice<String>> onAutocompleteChoice(final java.util.Optional<Str
};
}

static OptionalOption<Long> longOption(final String name, final String description, final Function<java.util.Optional<Long>, List<Choice<Long>>> autocomplete) {
return new LongAutocompleteOptionalOption(name, description) {
static OptionalOption<Long> longOption(final String name, final String description, final ApplicationCommand parent, final Function<java.util.Optional<Long>, List<Choice<Long>>> autocomplete) {
return new LongAutocompleteOptionalOption(name, description, parent) {
@Override
protected List<Choice<Long>> onAutocompleteChoice(final java.util.Optional<Long> option) {
return autocomplete.apply(option);
Expand All @@ -123,17 +123,17 @@ protected List<Choice<Long>> onAutocompleteChoice(final java.util.Optional<Long>

}
interface Required {
static RequiredOption<String> stringOption(final String name, final String description, final Function<java.util.Optional<String>, List<Choice<String>>> autocomplete) {
return new StringAutocompleteRequiredOption(name, description) {
static RequiredOption<String> stringOption(final String name, final String description, final ApplicationCommand parent, final Function<java.util.Optional<String>, List<Choice<String>>> autocomplete) {
return new StringAutocompleteRequiredOption(name, description, parent) {
@Override
protected List<Choice<String>> onAutocompleteChoice(final java.util.Optional<String> option) {
return autocomplete.apply(option);
}
};
}

static RequiredOption<Long> longOption(final String name, final String description, final Function<java.util.Optional<Long>, List<Choice<Long>>> autocomplete) {
return new LongAutocompleteRequiredOption(name, description) {
static RequiredOption<Long> longOption(final String name, final String description, final ApplicationCommand parent,final Function<java.util.Optional<Long>, List<Choice<Long>>> autocomplete) {
return new LongAutocompleteRequiredOption(name, description, parent) {
@Override
protected List<Choice<Long>> onAutocompleteChoice(final java.util.Optional<Long> option) {
return autocomplete.apply(option);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.Javachord;
import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.OptionalOption;
import org.javacord.api.event.interaction.AutocompleteCreateEvent;
import org.javacord.api.interaction.*;
Expand All @@ -12,8 +13,12 @@
import java.util.function.BiFunction;

abstract class AutocompleteOptionalOption<E> extends OptionalOption<E> implements AutocompleteCreateListener {
public AutocompleteOptionalOption(final String name, final String description, final SlashCommandOptionType type, final BiFunction<String, SlashCommandInteraction, Optional<E>> mapper) {
private final ApplicationCommand _parent;

public AutocompleteOptionalOption(final String name, final String description, final SlashCommandOptionType type,
final ApplicationCommand parent, final BiFunction<String, SlashCommandInteraction, Optional<E>> mapper) {
super(name, description, type, mapper);
this._parent = parent;
Javachord.Instance.Get.addListener(this);
}

Expand All @@ -24,6 +29,9 @@ protected final SlashCommandOptionBuilder buildSlashCommandOption(final SlashCom

@Override
public final void onAutocompleteCreate(final AutocompleteCreateEvent event) {
if (!_parent.fqdn().equalsIgnoreCase(event.getAutocompleteInteraction().getFullCommandName())) {
return;
}
var choices = onAutocomplete(event.getAutocompleteInteraction().getArgumentByName(_name));
event.getAutocompleteInteraction()
.respondWithChoices(choices)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.Javachord;
import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.RequiredOption;
import org.javacord.api.event.interaction.AutocompleteCreateEvent;
import org.javacord.api.interaction.*;
Expand All @@ -12,8 +13,12 @@
import java.util.function.BiFunction;

abstract class AutocompleteRequiredOption<E> extends RequiredOption<E> implements AutocompleteCreateListener {
public AutocompleteRequiredOption(final String name, final String description, final SlashCommandOptionType type, final BiFunction<String, SlashCommandInteraction, E> mapper) {
private final ApplicationCommand _parent;

public AutocompleteRequiredOption(final String name, final String description, final SlashCommandOptionType type,
final ApplicationCommand parent, final BiFunction<String, SlashCommandInteraction, E> mapper) {
super(name, description, type, mapper);
this._parent = parent;
Javachord.Instance.Get.addListener(this);
}

Expand All @@ -24,6 +29,9 @@ protected final SlashCommandOptionBuilder buildSlashCommandOption(final SlashCom

@Override
public final void onAutocompleteCreate(final AutocompleteCreateEvent event) {
if (!_parent.fqdn().equalsIgnoreCase(event.getAutocompleteInteraction().getFullCommandName())) {
return;
}
var choices = onAutocomplete(event.getAutocompleteInteraction().getArgumentByName(_name));
event.getAutocompleteInteraction()
.respondWithChoices(choices)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.choice.Choice;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionChoice;
Expand All @@ -10,8 +11,8 @@
import java.util.stream.Collectors;

public abstract class LongAutocompleteOptionalOption extends AutocompleteOptionalOption<Long>{
public LongAutocompleteOptionalOption(final String name, final String description) {
super(name, description, SlashCommandOptionType.LONG, (id, interaction) -> interaction.getArgumentLongValueByName(id));
public LongAutocompleteOptionalOption(final String name, final String description, final ApplicationCommand parent) {
super(name, description, SlashCommandOptionType.LONG, parent,(id, interaction) -> interaction.getArgumentLongValueByName(id));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.choice.Choice;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionChoice;
Expand All @@ -10,8 +11,8 @@
import java.util.stream.Collectors;

public abstract class LongAutocompleteRequiredOption extends AutocompleteRequiredOption<Long> {
public LongAutocompleteRequiredOption(final String name, final String description) {
super(name, description, SlashCommandOptionType.LONG, (id, interaction) -> interaction.getArgumentLongValueByName(id).orElseThrow());
public LongAutocompleteRequiredOption(final String name, final String description, final ApplicationCommand parent) {
super(name, description, SlashCommandOptionType.LONG, parent,(id, interaction) -> interaction.getArgumentLongValueByName(id).orElseThrow());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.choice.Choice;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionChoice;
Expand All @@ -10,8 +11,8 @@
import java.util.stream.Collectors;

public abstract class StringAutocompleteOptionalOption extends AutocompleteOptionalOption<String> {
public StringAutocompleteOptionalOption(final String name, final String description) {
super(name, description, SlashCommandOptionType.STRING, (id, interaction) -> interaction.getArgumentStringValueByName(id));
public StringAutocompleteOptionalOption(final String name, final String description, final ApplicationCommand parent) {
super(name, description, SlashCommandOptionType.STRING, parent, (id, interaction) -> interaction.getArgumentStringValueByName(id));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zrdj.javachord.command.option.autocomplete;

import com.github.zrdj.javachord.command.ApplicationCommand;
import com.github.zrdj.javachord.command.option.choice.Choice;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionChoice;
Expand All @@ -10,8 +11,8 @@
import java.util.stream.Collectors;

public abstract class StringAutocompleteRequiredOption extends AutocompleteRequiredOption<String> {
public StringAutocompleteRequiredOption(final String name, final String description) {
super(name, description, SlashCommandOptionType.STRING, (id, interaction) -> interaction.getArgumentStringValueByName(id).orElseThrow());
public StringAutocompleteRequiredOption(final String name, final String description, final ApplicationCommand parent) {
super(name, description, SlashCommandOptionType.STRING, parent,(id, interaction) -> interaction.getArgumentStringValueByName(id).orElseThrow());
}

@Override
Expand Down

0 comments on commit 66b0850

Please sign in to comment.