diff --git a/api/src/main/java/com/discordsrv/api/discord/events/interaction/command/DiscordCommandAutoCompleteInteractionEvent.java b/api/src/main/java/com/discordsrv/api/discord/events/interaction/command/DiscordCommandAutoCompleteInteractionEvent.java index 84cee304..01074fd9 100644 --- a/api/src/main/java/com/discordsrv/api/discord/events/interaction/command/DiscordCommandAutoCompleteInteractionEvent.java +++ b/api/src/main/java/com/discordsrv/api/discord/events/interaction/command/DiscordCommandAutoCompleteInteractionEvent.java @@ -29,8 +29,13 @@ import com.discordsrv.api.discord.entity.guild.DiscordGuildMember; import com.discordsrv.api.discord.events.interaction.AbstractInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import java.util.LinkedHashMap; +import java.util.Map; + public class DiscordCommandAutoCompleteInteractionEvent extends AbstractInteractionEvent { + private final Map choices = new LinkedHashMap<>(); + public DiscordCommandAutoCompleteInteractionEvent( CommandAutoCompleteInteractionEvent jdaEvent, DiscordUser user, @@ -39,4 +44,20 @@ public class DiscordCommandAutoCompleteInteractionEvent extends AbstractInteract ) { super(jdaEvent, user, member, channel); } + + public void addChoice(String key, String value) { + this.choices.put(key, value); + } + + public void addChoice(String key, double value) { + this.choices.put(key, value); + } + + public void addChoice(String key, long value) { + this.choices.put(key, value); + } + + public Map getChoices() { + return choices; + } } diff --git a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIEventModule.java b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIEventModule.java index 2acc9fd5..8c7a05bb 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIEventModule.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIEventModule.java @@ -23,10 +23,10 @@ import com.discordsrv.api.discord.entity.channel.DiscordMessageChannel; import com.discordsrv.api.discord.entity.guild.DiscordGuildMember; import com.discordsrv.api.discord.entity.interaction.DiscordInteractionHook; import com.discordsrv.api.discord.events.interaction.DiscordModalInteractionEvent; +import com.discordsrv.api.discord.events.interaction.command.DiscordChatInputInteractionEvent; import com.discordsrv.api.discord.events.interaction.command.DiscordCommandAutoCompleteInteractionEvent; import com.discordsrv.api.discord.events.interaction.command.DiscordMessageContextInteractionEvent; import com.discordsrv.api.discord.events.interaction.command.DiscordUserContextInteractionEvent; -import com.discordsrv.api.discord.events.interaction.command.DiscordChatInputInteractionEvent; import com.discordsrv.api.discord.events.interaction.component.DiscordButtonInteractionEvent; import com.discordsrv.api.discord.events.interaction.component.DiscordSelectMenuInteractionEvent; import com.discordsrv.api.discord.events.member.role.DiscordMemberRoleAddEvent; @@ -55,7 +55,11 @@ import net.dv8tion.jda.api.events.message.MessageDeleteEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageUpdateEvent; import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback; +import net.dv8tion.jda.api.interactions.commands.Command; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class DiscordAPIEventModule extends AbstractModule { @@ -126,8 +130,22 @@ public class DiscordAPIEventModule extends AbstractModule { DiscordGuildMember guildMember = member != null ? api().getGuildMember(member) : null; DiscordMessageChannel channel = api().getMessageChannel(event.getMessageChannel()); if (event instanceof CommandAutoCompleteInteractionEvent) { - discordSRV.eventBus().publish(new DiscordCommandAutoCompleteInteractionEvent( - (CommandAutoCompleteInteractionEvent) event, user, guildMember, channel)); + DiscordCommandAutoCompleteInteractionEvent autoComplete = new DiscordCommandAutoCompleteInteractionEvent( + (CommandAutoCompleteInteractionEvent) event, user, guildMember, channel); + discordSRV.eventBus().publish(autoComplete); + List choices = new ArrayList<>(); + for (Map.Entry entry : autoComplete.getChoices().entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + if (value instanceof String) { + choices.add(new Command.Choice(key, (String) value)); + } else if (value instanceof Double || value instanceof Float) { + choices.add(new Command.Choice(key, ((Number) value).doubleValue())); + } else { + choices.add(new Command.Choice(key, ((Number) value).longValue())); + } + } + ((CommandAutoCompleteInteractionEvent) event).replyChoices(choices).queue(); return; }