import { Member } from 'eris';
import { Client, CmdContext, 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(ctx: CmdContext) {
    if (!ctx.args[0]) return this.client.commands.get('help').run(new CmdContext(ctx.message, [this.name]));
    let user: Member = this.client.util.resolveMember(ctx.args[0], this.mainGuild);
    if (!user) {
      try {
        user = await this.client.getRESTGuildMember(this.mainGuild.id, ctx.args[0]);
      } catch {
        return this.error(ctx.message.channel, 'Cannot find user.');
      }
    }
    if (user && !this.client.util.moderation.checkPermissions(user, ctx.message.member)) return this.error(ctx.message.channel, 'Permission Denied.');
    ctx.message.delete();

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