import { promises as fs } from "fs";
import db from "../sql.mjs";

export default async bot => {

  return [{
    name: "delete",
    call: /^\!(del|rm) .*/i,
    active: true,
    level: 100,
    f: async e => {
      const ret = (await Promise.all(e.args.map(async id => {
        id = +id;
        if(id <= 0)
          return false;

        const f0ck = await db`
          select dest
          from "items"
          where id = ${+id}
          limit 1
        `;
        if(f0ck.length === 0)
          return false;
        
        await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
        await fs.unlink(`./public/t/${id}`).catch(_=>{});

        await db`
          delete from "items"
          where id = ${+id}
        `;

        return id;
      }))).filter(d => d);
      
      if(ret.length > 0)
        e.reply(`deleted ${ret.length}/${e.args.length} (${ret.join(",")}) f0cks`);
      else
        e.reply(`oof`);
    }
  }]
};
