Sfoglia il codice sorgente

Fix caption postfix and last image capture

ghorsington 4 anni fa
parent
commit
0e74db58f9
2 ha cambiato i file con 17 aggiunte e 16 eliminazioni
  1. 2 2
      bot/package.json
  2. 15 14
      bot/src/commands/facemorph.ts

+ 2 - 2
bot/package.json

@@ -41,6 +41,8 @@
       "discord.js": "^12.2.0",
       "discord.js": "^12.2.0",
       "dotenv": "^8.2.0",
       "dotenv": "^8.2.0",
       "emoji-regex": "^9.0.0",
       "emoji-regex": "^9.0.0",
+      "form-data": "^3.0.0",
+      "got": "^11.2.0",
       "html2bbcode": "^1.2.6",
       "html2bbcode": "^1.2.6",
       "interval-promise": "^1.4.0",
       "interval-promise": "^1.4.0",
       "jimp": "^0.12.1",
       "jimp": "^0.12.1",
@@ -49,7 +51,6 @@
       "koa-router": "^8.0.8",
       "koa-router": "^8.0.8",
       "lowdb": "^1.0.0",
       "lowdb": "^1.0.0",
       "module-alias": "^2.2.2",
       "module-alias": "^2.2.2",
-      "needle": "^2.5.0",
       "node-schedule": "^1.3.2",
       "node-schedule": "^1.3.2",
       "nodemailer": "^6.4.8",
       "nodemailer": "^6.4.8",
       "pg": "^8.2.1",
       "pg": "^8.2.1",
@@ -70,7 +71,6 @@
       "yaml": "^1.10.0"
       "yaml": "^1.10.0"
    },
    },
    "devDependencies": {
    "devDependencies": {
-      "@types/needle": "^2.0.4",
       "@types/node": "^14.0.5",
       "@types/node": "^14.0.5",
       "@types/nodemailer": "^6.4.0",
       "@types/nodemailer": "^6.4.0",
       "@typescript-eslint/eslint-plugin": "^3.0.2",
       "@typescript-eslint/eslint-plugin": "^3.0.2",

+ 15 - 14
bot/src/commands/facemorph.ts

@@ -8,8 +8,8 @@ import { FaceCaptionMessage, FaceCaptionType } from "@shared/db/entity/FaceCapti
 import { KnownChannel } from "@shared/db/entity/KnownChannel";
 import { KnownChannel } from "@shared/db/entity/KnownChannel";
 import { CommandSet, Action, ActionType, Command } from "src/model/command";
 import { CommandSet, Action, ActionType, Command } from "src/model/command";
 import { logger } from "src/logging";
 import { logger } from "src/logging";
-import { Response } from "request";
-import needle from "needle";
+import got from "got";
+import FormData from "form-data";
 
 
 const EMOTE_GUILD = "505333548694241281";
 const EMOTE_GUILD = "505333548694241281";
 
 
@@ -144,7 +144,7 @@ export class Facemorph {
         }
         }
         else {
         else {
             const prefixMessage = (await this.getRandomCaption(FaceCaptionType.PREFIX))?.message ?? "Feed them";
             const prefixMessage = (await this.getRandomCaption(FaceCaptionType.PREFIX))?.message ?? "Feed them";
-            const postfixMessage = (await this.getRandomCaption(FaceCaptionType.POSTFIX)) ?? "carrots";
+            const postfixMessage = (await this.getRandomCaption(FaceCaptionType.POSTFIX))?.message ?? "carrots";
             text = `${prefixMessage} ${postfixMessage}`;
             text = `${prefixMessage} ${postfixMessage}`;
         }
         }
         const h = Jimp.measureTextHeight(font, text, targetSize - CAPTION_OFFSET * 2);
         const h = Jimp.measureTextHeight(font, text, targetSize - CAPTION_OFFSET * 2);
@@ -172,20 +172,21 @@ export class Facemorph {
     async processFaceSwap(message: Message, attachmentUrl: string, processor?: ImageProcessor, failMessage?: string, successMessage?: string): Promise<void> {
     async processFaceSwap(message: Message, attachmentUrl: string, processor?: ImageProcessor, failMessage?: string, successMessage?: string): Promise<void> {
         const data = await request(attachmentUrl, { encoding: null }) as Buffer;
         const data = await request(attachmentUrl, { encoding: null }) as Buffer;
 
 
-        const result = await needle("post", `http://${process.env.FACEDETECT_URL}/process`, {
-            img_data: {
-                buffer: data,
-                filename: "image.png",
-                content_type: "application/octet-stream"
-            }
-        }, { multipart: true });
-
+        const form = new FormData();
+        form.append("img_data", data, {
+            filename: "image.png",
+            contentType: "application/octet-stream"
+        });
+        const result = await got.post<FaceDetectionResponse>(`http://${process.env.FACEDETECT_URL}/process`, {
+            responseType: "json",
+            body: form
+        });
+        
         if(result.statusCode != 200) {
         if(result.statusCode != 200) {
             logger.error("Face detection failed! Got response %s", result.statusCode);
             logger.error("Face detection failed! Got response %s", result.statusCode);
             return;
             return;
         }
         }
-
-        const faceRects = result.body as FaceDetectionResponse;
+        const faceRects = result.body;
 
 
         if (isError(faceRects)) {
         if (isError(faceRects)) {
             logger.error("Face detection failed! Got response %s", result.statusCode);
             logger.error("Face detection failed! Got response %s", result.statusCode);
@@ -252,7 +253,7 @@ export class Facemorph {
     processLastImage(msg: Message, processor: ImageProcessor): void {
     processLastImage(msg: Message, processor: ImageProcessor): void {
         type AttachedMessage = {msg: Message, att: MessageAttachment};
         type AttachedMessage = {msg: Message, att: MessageAttachment};
         const lastImagedMessage = msg.channel.messages.cache.mapValues(m => ({msg: m, att: m.attachments.find(v => isValidImage(v.name))}))
         const lastImagedMessage = msg.channel.messages.cache.mapValues(m => ({msg: m, att: m.attachments.find(v => isValidImage(v.name))}))
-            .filter(v => v.att != undefined).last() as AttachedMessage;
+            .filter(v => !v.msg.author.bot && v.att != undefined).last() as AttachedMessage;
 
 
         if (!lastImagedMessage) {
         if (!lastImagedMessage) {
             msg.channel.send(`${msg.author.toString()} Sorry, I couldn't find any recent messages with images.`);
             msg.channel.send(`${msg.author.toString()} Sorry, I couldn't find any recent messages with images.`);