add command suggestions i guess

This commit is contained in:
ChomeNS
2023-04-28 12:14:07 +07:00
parent 9610850424
commit c5df71e792
3 changed files with 74 additions and 13 deletions

View File

@@ -63,6 +63,7 @@ public class Bot {
@Getter private MazePlugin maze;
@Getter private ExploitsPlugin exploits;
@Getter private FilterPlugin filter;
@Getter private CommandSuggestionPlugin commandSuggestion;
public Bot (Configuration.BotOption botOption, List<Bot> bots, Configuration config) {
this.host = botOption.host;
@@ -105,6 +106,7 @@ public class Bot {
this.maze = new MazePlugin(this);
this.exploits = new ExploitsPlugin(this);
this.filter = new FilterPlugin(this);
this.commandSuggestion = new CommandSuggestionPlugin(this);
reconnect();
}

View File

@@ -0,0 +1,61 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import java.util.ArrayList;
import java.util.List;
public class CommandSuggestionPlugin extends ChatPlugin.ChatListener {
private final Bot bot;
@Getter @Setter private String id = "chomens_bot_command_suggestion";
public CommandSuggestionPlugin (Bot bot) {
this.bot = bot;
bot.chat().addListener(this);
}
@Override
public void systemMessageReceived(Component component) {
try {
final String tag = ((TextComponent) component).content();
if (!tag.equals(id)) return;
final List<Component> children = component.children();
if (children.size() < 2) return;
final int transactionId = Integer.parseInt(((TextComponent) children.get(0)).content());
final String player = ((TextComponent) children.get(1)).content();
Component inputComponent;
try {
inputComponent = children.get(2);
} catch (IndexOutOfBoundsException e) {
inputComponent = Component.text("");
}
final String input = ((TextComponent) inputComponent).content();
System.out.println("input is \"" + input + "\"");
final List<Component> output = new ArrayList<>();
output.add(Component.text(id));
output.add(Component.text(transactionId));
for (Command command : bot.commandHandler().commands()) {
if (!command.name().startsWith(input)) continue;
output.add(Component.text(command.name()));
}
bot.chat().tellraw(Component.join(JoinConfiguration.noSeparators(), output), player);
} catch (Exception ignored) {}
}
}