Add responding with choices to DiscordCommandAutoCompleteInteractionEvent

This commit is contained in:
Vankka 2022-07-22 19:13:40 +03:00
parent 1ce73a2842
commit 2f3c9946b5
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
2 changed files with 42 additions and 3 deletions

View File

@ -29,8 +29,13 @@ import com.discordsrv.api.discord.entity.guild.DiscordGuildMember;
import com.discordsrv.api.discord.events.interaction.AbstractInteractionEvent; import com.discordsrv.api.discord.events.interaction.AbstractInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import java.util.LinkedHashMap;
import java.util.Map;
public class DiscordCommandAutoCompleteInteractionEvent extends AbstractInteractionEvent<CommandAutoCompleteInteractionEvent> { public class DiscordCommandAutoCompleteInteractionEvent extends AbstractInteractionEvent<CommandAutoCompleteInteractionEvent> {
private final Map<String, Object> choices = new LinkedHashMap<>();
public DiscordCommandAutoCompleteInteractionEvent( public DiscordCommandAutoCompleteInteractionEvent(
CommandAutoCompleteInteractionEvent jdaEvent, CommandAutoCompleteInteractionEvent jdaEvent,
DiscordUser user, DiscordUser user,
@ -39,4 +44,20 @@ public class DiscordCommandAutoCompleteInteractionEvent extends AbstractInteract
) { ) {
super(jdaEvent, user, member, channel); 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<String, Object> getChoices() {
return choices;
}
} }

View File

@ -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.guild.DiscordGuildMember;
import com.discordsrv.api.discord.entity.interaction.DiscordInteractionHook; import com.discordsrv.api.discord.entity.interaction.DiscordInteractionHook;
import com.discordsrv.api.discord.events.interaction.DiscordModalInteractionEvent; 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.DiscordCommandAutoCompleteInteractionEvent;
import com.discordsrv.api.discord.events.interaction.command.DiscordMessageContextInteractionEvent; 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.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.DiscordButtonInteractionEvent;
import com.discordsrv.api.discord.events.interaction.component.DiscordSelectMenuInteractionEvent; import com.discordsrv.api.discord.events.interaction.component.DiscordSelectMenuInteractionEvent;
import com.discordsrv.api.discord.events.member.role.DiscordMemberRoleAddEvent; 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.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent; import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback; 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; import java.util.stream.Collectors;
public class DiscordAPIEventModule extends AbstractModule<DiscordSRV> { public class DiscordAPIEventModule extends AbstractModule<DiscordSRV> {
@ -126,8 +130,22 @@ public class DiscordAPIEventModule extends AbstractModule<DiscordSRV> {
DiscordGuildMember guildMember = member != null ? api().getGuildMember(member) : null; DiscordGuildMember guildMember = member != null ? api().getGuildMember(member) : null;
DiscordMessageChannel channel = api().getMessageChannel(event.getMessageChannel()); DiscordMessageChannel channel = api().getMessageChannel(event.getMessageChannel());
if (event instanceof CommandAutoCompleteInteractionEvent) { if (event instanceof CommandAutoCompleteInteractionEvent) {
discordSRV.eventBus().publish(new DiscordCommandAutoCompleteInteractionEvent( DiscordCommandAutoCompleteInteractionEvent autoComplete = new DiscordCommandAutoCompleteInteractionEvent(
(CommandAutoCompleteInteractionEvent) event, user, guildMember, channel)); (CommandAutoCompleteInteractionEvent) event, user, guildMember, channel);
discordSRV.eventBus().publish(autoComplete);
List<Command.Choice> choices = new ArrayList<>();
for (Map.Entry<String, Object> 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; return;
} }