Skip to content

Commit 4166286

Browse files
committed
Autocomplete tag command
1 parent 03ae8a8 commit 4166286

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/main/java/org/geysermc/discordbot/commands/TagCommand.java

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
2+
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to deal
@@ -28,13 +28,18 @@
2828
import com.jagrosh.jdautilities.command.SlashCommand;
2929
import com.jagrosh.jdautilities.command.SlashCommandEvent;
3030
import net.dv8tion.jda.api.EmbedBuilder;
31+
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
32+
import net.dv8tion.jda.api.interactions.commands.Command;
3133
import net.dv8tion.jda.api.interactions.commands.OptionType;
3234
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
3335
import org.geysermc.discordbot.tags.SlashTag;
3436
import org.geysermc.discordbot.tags.TagsManager;
3537
import org.geysermc.discordbot.util.BotColors;
38+
import org.geysermc.discordbot.util.DicesCoefficient;
3639

40+
import java.util.ArrayList;
3741
import java.util.Collections;
42+
import java.util.List;
3843

3944
public class TagCommand extends SlashCommand {
4045

@@ -46,6 +51,7 @@ public TagCommand() {
4651

4752
this.options = Collections.singletonList(
4853
new OptionData(OptionType.STRING, "name", "The tag to get (Supports aliases)", true)
54+
.setAutoComplete(true)
4955
);
5056
}
5157

@@ -79,7 +85,60 @@ protected void execute(SlashCommandEvent event) {
7985
.setDescription("Missing requested tag")
8086
.build()).queue();
8187
}
88+
}
89+
90+
@Override
91+
public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
92+
// Get the query
93+
String query = event.getFocusedOption().getValue();
94+
95+
// Get the providers
96+
List<String> tags = potentialTags(query);
97+
98+
event.replyChoices(tags.stream()
99+
.distinct()
100+
.map(tag -> new Command.Choice(tag, tag))
101+
.limit(25)
102+
.toArray(Command.Choice[]::new))
103+
.queue();
104+
}
105+
106+
List<String> potentialTags(String query) {
107+
List<String> potential = new ArrayList<>();
108+
109+
for (SlashTag slashTag : TagsManager.getEmbedTags()) {
110+
// Check if the name starts with the query
111+
if (slashTag.getName().toLowerCase().startsWith(query.toLowerCase())) {
112+
potential.add(slashTag.getName());
113+
continue;
114+
}
115+
116+
// Check if the name is similar
117+
double similar = DicesCoefficient.diceCoefficientOptimized(query.toLowerCase(), slashTag.getName().toLowerCase());
118+
if (similar > 0.2d) {
119+
potential.add(slashTag.getName());
120+
continue;
121+
}
122+
123+
// Check the aliases
124+
if (slashTag.getAliases() != null && !slashTag.getAliases().isEmpty()) {
125+
for (String alias : slashTag.getAliases().split(",")) {
126+
// Check if the alias starts with the query
127+
if (alias.toLowerCase().startsWith(query.toLowerCase())) {
128+
potential.add(alias);
129+
break;
130+
}
82131

132+
// Check if the alias is similar
133+
double aliasSimilar = DicesCoefficient.diceCoefficientOptimized(query.toLowerCase(), alias.toLowerCase());
134+
if (aliasSimilar > 0.2d) {
135+
potential.add(alias);
136+
break;
137+
}
138+
}
139+
}
140+
}
83141

142+
return potential;
84143
}
85144
}

0 commit comments

Comments
 (0)