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

export default class DelRank extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'delrank';
    this.description = 'Deletes an existing self-assignable role. This doesn\'t delete the role itself.';
    this.usage = `${this.client.config.prefix}delrank <role>`;
    this.permissions = 6;
    this.guildOnly = true;
    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 role = this.client.util.resolveRole(ctx.args[0], this.mainGuild);
    if (!role) return this.error(ctx.message.channel, 'The role you specified doesn\'t appear to exist.');

    const check = await this.client.db.mongo.Rank.findOne({ roleID: role.id });
    if (!check) return this.error(ctx.message.channel, 'The entry doesn\'t appear to exist.');

    await this.client.db.mongo.Rank.deleteOne({ roleID: role.id });
    return this.success(ctx.message.channel, `Role ${role.name} is no longer self-assignable.`);
  }
}
