ability to delete own f0cks

This commit is contained in:
Flummi 2022-05-12 16:18:18 +02:00
parent 7e9599adce
commit a97ed32c9f

View File

@ -1,5 +1,6 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import db from "../sql.mjs"; import db from "../sql.mjs";
import { getLevel } from "../../inc/admin.mjs";
export default async bot => { export default async bot => {
@ -7,21 +8,41 @@ export default async bot => {
name: "delete", name: "delete",
call: /^\!(del|rm) .*/i, call: /^\!(del|rm) .*/i,
active: true, active: true,
level: 100,
f: async e => { f: async e => {
const ret = (await Promise.all(e.args.map(async id => { let deleted = [];
for(let id of e.args) {
id = +id; id = +id;
if(id <= 0) if(id <= 0)
return false; continue;
const f0ck = await db` const f0ck = await db`
select dest select dest, username, userchannel, usernetwork
from "items" from "items"
where id = ${+id} where id = ${id}
limit 1 limit 1
`; `;
if(f0ck.length === 0) const level = getLevel(e.user).level;
return false;
if(f0ck.length === 0) {
e.reply(`f0ck ${id}: f0ck not found`);
continue;
}
if(
(f0ck[0].username !== (e.user.nick || e.user.username) ||
f0ck[0].userchannel !== e.channel ||
f0ck[0].usernetwork !== e.network) &&
level < 100
) {
e.reply(`f0ck ${id}: insufficient permissions`);
continue;
}
if(~~(new Date() / 1e3) >= (f0ck[0].stamp + 600) && level < 100) {
e.reply(`f0ck ${id}: too late lol`);
continue;
}
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{}); await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./public/t/${id}`).catch(_=>{}); await fs.unlink(`./public/t/${id}`).catch(_=>{});
@ -31,13 +52,10 @@ export default async bot => {
where id = ${+id} where id = ${+id}
`; `;
return id; deleted.push(id);
}))).filter(d => d); }
if(ret.length > 0) e.reply(`deleted ${deleted.length}/${e.args.length} f0cks (${deleted.join(",")})`);
e.reply(`deleted ${ret.length}/${e.args.length} (${ret.join(",")}) f0cks`);
else
e.reply(`oof`);
} }
}] }]
}; };