import { Message } from 'eris';
import { Client, 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(message: Message, args: string[]) {
    try {
      if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
      const role = this.client.util.resolveRole(args[0], this.mainGuild);
      if (!role) return this.error(message.channel, 'The role you specified doesn\'t appear to exist.');

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

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