finished automod
diff --git a/src/reflex/scanners.ts b/src/reflex/scanners.ts
index c8d59f0..c55e463 100644
--- a/src/reflex/scanners.ts
+++ b/src/reflex/scanners.ts
@@ -9,19 +9,18 @@
interface NSFWSchema {
nsfw: boolean;
+ errored?: boolean;
}
interface MalwareSchema {
safe: boolean;
+ errored?: boolean;
}
export async function testNSFW(link: string): Promise<NSFWSchema> {
const [p, hash] = await saveAttachment(link);
- console.log("Checking an image")
let alreadyHaveCheck = await client.database.scanCache.read(hash)
if(alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
- console.log("Was not in db")
const data = new FormData();
- console.log(link);
data.append("file", createReadStream(p));
const result = await fetch("https://unscan.p.rapidapi.com/", {
method: "POST",
@@ -31,13 +30,14 @@
},
body: data
})
- .then((response) => response.json() as Promise<NSFWSchema>)
+ .then((response) => response.status === 200 ? response.json() as Promise<NSFWSchema> : { nsfw: false, errored: true })
.catch((err) => {
console.error(err);
- return { nsfw: false };
+ return { nsfw: false, errored: true };
});
- console.log(result);
- client.database.scanCache.write(hash, result.nsfw);
+ if(!result.errored) {
+ client.database.scanCache.write(hash, result.nsfw);
+ }
return { nsfw: result.nsfw };
}
@@ -48,7 +48,6 @@
const data = new URLSearchParams();
let f = createReadStream(p);
data.append("file", f.read(fs.statSync(p).size));
- console.log(link);
const result = await fetch("https://unscan.p.rapidapi.com/malware", {
method: "POST",
headers: {
@@ -57,18 +56,18 @@
},
body: data
})
- .then((response) => response.json() as Promise<MalwareSchema>)
+ .then((response) => response.status === 200 ? response.json() as Promise<MalwareSchema> : { safe: true, errored: true })
.catch((err) => {
console.error(err);
- return { safe: true };
+ return { safe: true, errored: true };
});
- console.log(result);
- client.database.scanCache.write(hash, result.safe);
+ if (!result.errored) {
+ client.database.scanCache.write(hash, result.safe);
+ }
return { safe: result.safe };
}
export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
- console.log(link);
let alreadyHaveCheck = await client.database.scanCache.read(link)
if(alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] };
const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", {
@@ -84,7 +83,6 @@
console.error(err);
return { safe: true, tags: [] };
});
- console.log(scanned);
client.database.scanCache.write(link, scanned.safe ?? true, []);
return {
safe: scanned.safe ?? true,
@@ -93,10 +91,11 @@
}
export async function saveAttachment(link: string): Promise<[string, string]> {
- const image = (await fetch(link)).arrayBuffer().toString();
+ const image = await (await fetch(link)).arrayBuffer()
const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
- writeFileSync(fileName, image, "base64");
- return [fileName, createHash('sha512').update(image, 'base64').digest('base64')];
+ const enc = new TextDecoder("utf-8");
+ writeFileSync(fileName, new DataView(image), "base64");
+ return [fileName, createHash('sha512').update(enc.decode(image), 'base64').digest('base64')];
}
const linkTypes = {