import { EmbedOptions } from 'eris';
import axios, { AxiosResponse } from 'axios';
import { Client, CmdContext, Command } from '../class';

export default class Eris extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'eris';
    this.description = 'Get information about Eris.';
    this.usage = `${this.client.config.prefix}eris <query`;
    this.permissions = 0;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    if (!ctx.args[0]) return this.client.commands.get('help').run(new CmdContext(ctx.message, [this.name]));

    let res: AxiosResponse<{embed: EmbedOptions}>;
    try {
      res = await axios.get('https://erisdocs.cloud.libraryofcode.org/docs', { params: { search: ctx.args[0] } });
    } catch (err) {
      if (err.code === 404) return this.error(ctx.message.channel, 'Could not find information. Try something else.');
      return this.error(ctx.message.channel, 'Please try again later, something unexpected happened.');
    }

    return ctx.uniCreateMessage(res.data);
  }
}
