Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When searching for a non-existing clan or player #2

Open
DavidModzz opened this issue Mar 14, 2024 · 1 comment
Open

When searching for a non-existing clan or player #2

DavidModzz opened this issue Mar 14, 2024 · 1 comment

Comments

@DavidModzz
Copy link

When you try to search for a clan or play that does not exist it gives an error

Error: Api error: undefined

I'm not exactly a good programmer, but I'm trying to create a command that gives information about a player or clan

module.exports = {
  name: 'clashroyale',
  aliases: ["cr"],
  category: 'Search 🔎',
  usage: 'clashroyale < jugador/clan > < tag >',
  example: 'clashroyale jugador #LQLYU8JL',
  description: 'Te muestra estadísticas de Clash Royale sobre un jugador o clan',

  async execute(client, message, args) {
    const db = client.db.groups;
    var prefix = await db.get(`${client.from}.prefix`) ? await db.get(`${client.from}.prefix`) : client.config.prefix;

    if (!args.length) {
      return client.reply(`Debes proporcionar un argumento. Por ejemplo: ${prefix}${this.example}`);
    }

    const arg = args.join(' ');
    var type = args[0];
    var tag = args[1];

    const validArgs = ["clan", "jugador"];
    if (!validArgs.includes(type)) {
      return client.reply(`El primer argumento debe ser "clan" o "jugador".\n*Uso:* ${this.usage}\n*Ejemplo:* ${prefix}${this.example}`);
    }

    if (!tag.startsWith("#")) {
      tag = `#${args[1]}`;
    }

    try {
      const { Client } = require("../../lib/clashpi/client.js");
      const api = new Client(client.config.cr_api)
      
      if (type == "clan") {
        try {
          const clan = await api.getClan(tag);
          if (clan.name) {
            client.reply(`*Nombre:* ${clan.name}\n*Tag:* ${clan.tag}\n*Descripción:* ${clan.description}\n*Puntos del clan:* ${clan.clanScore}\n*Copas de guerra de clanes:* ${clan.clanWarTrophies}\n*Copas necesarias:* ${clan.requiredTrophies}\n*Donaciones semanales:* ${clan.donationsPerWeek}\n*Miembros:* ${clan.members}`);
          } else {
            client.reply("No se encontro el clan, comprueba si el tag es correcto");
          }
        } catch (err) {
          client.reply("Ocurrio un error");
        }
      } else if (type == "jugador") {
        try {
          const player = await api.getPlayer(tag);
          if (player.name) {
            client.reply(`*Nombre:* ${player.name}\n*Tag:* ${player.tag}\n*Nivel:* ${player.expLevel}\n*Carta favorita:* ${player.favouriteCard}\n*Clan:* ${player.clan}\n*Arena:* ${player.arena}\n*Copas:* ${player.trophies}\n*PB:* ${player.bestTrophies}\n*Nivel:* ${player.expLevel}\n*Batallas:* ${player.battleCount}\n*Victorias:* ${player.wins}\n*Derrotas:* ${player.losses}\n*Empates:* ${player.draws}\n*Donaciones:* ${player.totalDonations}`);
          } else {
           client.reply("No se encontro al jugador, comprueba si el tag es correcto");
          }
        } catch (err) {
          client.reply("Ocurrio un error");
        }
      }
    } catch (err) {
      console.log(err)
      return client.reply(`Ocurrió un error`);
    }
  }
};

When given a valid Tag it returns the information but if it is an invalid Tag it does not return anything (no error message expected in the code)

@DavidModzz
Copy link
Author

DavidModzz commented Mar 14, 2024

I managed to fix it by making it return an empty object if there was an error, but I don't think that's the best way to fix it
connector.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant