import { Client, CmdContext, 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(ctx: CmdContext) {
    if (!ctx.args[0]) return this.client.commands.get('help').run(new CmdContext(ctx.message, [this.name]));
    const check = await this.client.db.mongo.Redirect.findOne({ key: ctx.args[0].toLowerCase() });
    if (!check) return this.error(ctx.message.channel, `Redirect key ${ctx.args[0].toLowerCase()} doesn't exist.`);
    await this.client.db.mongo.Redirect.deleteOne({ key: ctx.args[0].toLowerCase() });
    return this.success(ctx.message.channel, `Deleted redirect https://loc.sh/${ctx.args[0].toLowerCase()}.`);
  }
}
