import { Message } from 'eris';
import { Client, Command } from '../class';

export default class DelRedirect extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'delredirect';
    this.description = 'Delete a redirect link for \'loc.sh\'';
    this.usage = 'delredirect <key>';
    this.aliases = ['dr'];
    this.permissions = 6;
    this.enabled = true;
  }

  public async run(message: Message, args: string[]) {
    try {
      if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
      const check = await this.client.db.Redirect.findOne({ key: args[0].toLowerCase() });
      if (!check) return this.error(message.channel, `Redirect key ${args[0].toLowerCase()} doesn't exist.`);
      await this.client.db.Redirect.deleteOne({ key: args[0].toLowerCase() });
      return this.success(message.channel, `Deleted redirect https://loc.sh/${args[0].toLowerCase()}.`);
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
