fix: some small stuff

This commit is contained in:
2024-04-23 17:20:06 +02:00
parent c42e9bb0b6
commit c2c71016cb
8 changed files with 5044 additions and 2773 deletions

BIN
.yarn/install-state.gz Normal file

Binary file not shown.

5
.yarnrc Normal file
View File

@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.22.cjs"

1
.yarnrc.yml Normal file
View File

@@ -0,0 +1 @@
nodeLinker: node-modules

View File

@@ -1,7 +1,8 @@
import { commandModule, CommandType } from '@sern/handler';
import { publish } from '#plugins';
import { AttachmentBuilder } from 'discord.js';
import { AttachmentBuilder, codeBlock } from 'discord.js';
import { createCanvas, loadImage } from '@napi-rs/canvas';
import sharp from 'sharp';
export default commandModule({
name: 'Clasifica una imagen',
@@ -10,12 +11,18 @@ export default commandModule({
execute: async (ctx) => {
await ctx.deferReply()
if (ctx.targetMessage.attachments.size === 0) return ctx.editReply('No hay ninguna imagen para clasificar!');
const image = ctx.targetMessage.attachments.first();
if (!image.contentType.startsWith('image/')) return ctx.editReply('El archivo no es una imagen!');
if (ctx.targetMessage.attachments.size === 0) return ctx.editReply('No hay ninguna imagen para clasificar!');
const image = ctx.targetMessage.attachments.first();
if (!image.contentType.startsWith('image/') && image.contentType !== 'image/gif') return ctx.editReply('El archivo no es una imagen!');
const imageBlob = await fetch(image.url).then(async res => await res.arrayBuffer());
const imageUint8Array = new Uint8Array(imageBlob);
const imageBuffer = await fetch(image.url).then(async res => await res.arrayBuffer());
const compressed = sharp(imageBuffer)
.png({quality: 70})
.jpeg({quality: 70})
.webp({quality: 70})
.tiff({quality: 70});
const metadata = await compressed.metadata();
const imageUint8Array = new Uint8Array(await compressed.toBuffer());
const request = await fetch(`https://api.cloudflare.com/client/v4/accounts/${process.env.CF_AI_ACC}/ai/run/@cf/facebook/detr-resnet-50`, {
method: 'POST',
@@ -24,19 +31,19 @@ export default commandModule({
},
body: imageUint8Array,
}).then(async res => await res.json());
if (request.errors.length > 0) return ctx.editReply(`Hubo un error! ${codeBlock(JSON.stringify(request.errors))}`);
// all canvas stuff, this was fun to make
const canvas = createCanvas(image.width, image.height);
const canvas = createCanvas(metadata.width, metadata.height);
const ctxCanvas = canvas.getContext('2d');
const img = await loadImage(image.url);
ctxCanvas.drawImage(img, 0, 0, image.width, image.height);
ctxCanvas.font = '40px sans-serif';
ctxCanvas.drawImage(img, 0, 0, metadata.width, metadata.height);
ctxCanvas.font = '30px sans-serif';
ctxCanvas.fillStyle = 'red';
ctxCanvas.strokeStyle = 'red';
ctxCanvas.lineWidth = 3;
for (const result of request.result) {
if (result.score < 0.5) continue;
console.log(result)
const box = result.box;
ctxCanvas.strokeRect(box.xmin, box.ymin, box.xmax - box.xmin, box.ymax - box.ymin);
ctxCanvas.fillText(result.label, box.xmin, box.ymin - 5);
@@ -46,4 +53,4 @@ export default commandModule({
await ctx.editReply({ files: [attachment] })
},
});
});

View File

@@ -1,9 +1,7 @@
import { discordEvent } from '@sern/handler';
import axios from 'axios';
import { TextChannel } from 'discord.js';
import db from '../schemas/chatgpt.js';
import { fetchEventSource } from '@ai-zen/node-fetch-event-source';
import database from '../schemas/chatgpt';
import { devMode } from '../index.js';
export default discordEvent({
@@ -92,7 +90,6 @@ export default discordEvent({
const dbData = new db({
messageid: message.id,
threadid: thread.id,
devServer: devMode,
messages: [
{ role: 'system', content: systemMsg },
{ role: 'user', content: message.content },

View File

@@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "tsc-watch -p \"./tsconfig.json\" --onSuccess \"node ./dist/index.js --dev\"",
"prod": "tsc-watch -p \"./tsconfig.json\" --onSuccess \"node ./dist/index.js\"",
"compile": "tsc --build",
"build": "tsc --build",
"web": "node webserver.js",
@@ -37,7 +38,7 @@
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.15.0",
"@microsoft/fetch-event-source": "^2.0.1",
"@napi-rs/canvas": "^0.1.30",
"@napi-rs/canvas": "^0.1.52",
"@sern/handler": "^3.3.4",
"axios": "^1.6.8",
"dayjs": "^1.11.6",
@@ -57,6 +58,7 @@
"pretty-seconds-spanish": "^2.1.1",
"rockpaperscissors-checker": "^1.2.0",
"set-interval-async": "^3.0.2",
"sharp": "^0.33.3",
"spotify-api.js": "^9.2.5",
"stringify-safe": "^1.0.3",
"systeminformation": "^5.21.7"
@@ -66,5 +68,6 @@
"ts-node": "10.9.1",
"tsc-watch": "^5.0.3",
"typescript": "^5.2.2"
}
},
"packageManager": "yarn@4.1.1"
}

View File

@@ -6,7 +6,6 @@ const messageSchema = new mongoose.Schema({
const schema = new mongoose.Schema({
messageid: { type: String, required: true },
threadid: { type: String, required: true },
devServer: { type: Boolean, required: true },
messages: { type: [messageSchema], required: true },
});
const db = mongoose.model('chatgpt', schema, 'chatgpt');

7771
yarn.lock

File diff suppressed because it is too large Load Diff