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

export default class Unmute extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'unmute';
    this.description = 'Unmutes a member.';
    this.usage = 'unmute <member> [reason]';
    this.permissions = 2;
    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 member = this.client.util.resolveMember(ctx.args[0], this.mainGuild);
    if (!member) return this.error(ctx.message.channel, 'Cannot find user.');

    try {
      const res1 = await this.client.db.local.muted.get<boolean>(`muted-${member.id}`);
      if (!res1 || !this.mainGuild.members.get(member.id).roles.includes('478373942638149643')) return this.error(ctx.message.channel, 'This user is already unmuted.');
    } catch {} // eslint-disable-line no-empty
    if (member && !this.client.util.moderation.checkPermissions(member, ctx.message.member)) return this.error(ctx.message.channel, 'Permission Denied.');
    ctx.message.delete();

    await this.client.util.moderation.unmute(member.user.id, ctx.message.member, ctx.args.slice(1).join(' '));
    return this.success(ctx.message.channel, `${member.user.username}#${member.user.discriminator} has been unmuted.`);
  }
}
