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

export default class Kick extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'kick';
    this.description = 'Kicks a member from the guild.';
    this.usage = 'kick <member> [reason]';
    this.permissions = 3;
    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]);
      let user: Member = this.client.util.resolveMember(args[0], this.mainGuild);
      if (!user) {
        try {
          user = await this.client.getRESTGuildMember(this.mainGuild.id, args[0]);
        } catch {
          return this.error(message.channel, 'Cannot find user.');
        }
      }
      if (user && !this.client.util.moderation.checkPermissions(user, message.member)) return this.error(message.channel, 'Permission Denied.');
      message.delete();

      const reason: string = args.slice(1).join(' ');
      if (reason.length > 512) return this.error(message.channel, 'Kick reasons cannot be longer than 512 characters.');
      await this.client.util.moderation.kick(user, message.member, reason);
      return this.success(message.channel, `${user.username}#${user.discriminator} has been kicked.`);
    } catch (err) {
      return this.client.util.handleError(err, message, this, false);
    }
  }
}
