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

export default class DJS extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'djs';
    this.description = 'Get information about Discord.js.';
    this.usage = 'djs <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<EmbedOptions>;
    try {
      res = await axios.get(`https://djsdocs.sorta.moe/v2/embed?src=master&q=${ctx.args[0]}`);
    } catch (err) {
      return this.error(ctx.message.channel, 'Please try again later, something unexpected happened.');
    }

    if (!res.data) return this.error(ctx.message.channel, 'Could not find information. Try something else.');

    const embed = new RichEmbed(res.data);
    embed.setFooter(this.client.user.username, this.client.user.avatarURL);
    embed.setTimestamp();
    return ctx.uniCreateMessage({ embed });
  }
}
