#Help with autocomplete and JSON.

1 messages · Page 1 of 1 (latest)

ashen tinsel

This code searches within 3 JSONS (This will change at the end) it searches the keys "text" (name) and "reference" (Page in book.)

As it is I need to type the exact word for the search, I would like to use the autocomplete feature but it needs to search within the jsons. The search system I did only searches the exact words. They could help me with that. ?

const fs = require("fs");
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");

function filtrarDadosComPalavraChave(objetos, palavraChave, resultados) {
  objetos.forEach((objeto) => {
    if (objeto.children) {
      filtrarDadosComPalavraChave(objeto.children, palavraChave, resultados);
    } else {
      // Verifique se as propriedades 'text' e 'reference' existem antes de acessá-las
      if (
        objeto.text &&
        objeto.reference &&
        (objeto.text.includes(palavraChave) ||
          objeto.reference.includes(palavraChave))
      ) {
        resultados.push(objeto);
      }
    }
  });
}

function carregarDados() {
  const dadosPadrao = require("../JSONS/rules.json");
  const dadosMas = require("../JSONS/rules_ma.json");
  const dadosMagic = require("../JSONS/rules_m.json");

  const dadosCombinados = [
    ...dadosPadrao.rows,
    ...dadosMas.rows,
    ...dadosMagic.rows,
  ];

  return dadosCombinados;
}

module.exports = {
  data: new SlashCommandBuilder()
    .setName("rules")
    .setDescription("Verificar página relacionada com a regra.")
    .addStringOption((option) =>
      option
        .setName("rule")
        .setDescription("A palavra-chave para pesquisa.")
        .setRequired(true)
    ),

  async execute(interaction) {
    const palavraChave = interaction.options.getString("rule");

    const dados = carregarDados();

    const resultados = [];
    filtrarDadosComPalavraChave(dados, palavraChave, resultados);

    if (resultados.length === 0) {
      const embed = new EmbedBuilder()