|
@@ -1,4 +1,3 @@
|
|
|
-import { db, IRandomElementMixin } from "../db";
|
|
|
import { isValidImage } from "../util";
|
|
|
import Jimp from "jimp";
|
|
|
import client from "../client";
|
|
@@ -7,13 +6,18 @@ import * as path from "path";
|
|
|
import request from "request-promise-native";
|
|
|
import { ICommand } from "./command";
|
|
|
import { Message } from "discord.js";
|
|
|
-import { ObjectChain } from "lodash";
|
|
|
+import { getRepository } from "typeorm";
|
|
|
+import { FaceCaptionMessage, FaceCaptionType } from "../entity/FaceCaptionMessage";
|
|
|
+import { KnownChannel } from "../entity/KnownChannel";
|
|
|
|
|
|
const EMOTE_GUILD = "505333548694241281";
|
|
|
|
|
|
const animeCascade = new cv.CascadeClassifier(path.resolve(process.cwd(), "animu.xml"));
|
|
|
const faceCascade = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2);
|
|
|
|
|
|
+const CAPTION_IMG_SIZE = 300;
|
|
|
+const CAPTION_PROBABILITY = 0.33;
|
|
|
+
|
|
|
type ImageProcessor = (faces: cv.Rect[], data: Buffer) => Promise<Jimp>;
|
|
|
|
|
|
function intersects(r1: cv.Rect, r2: cv.Rect) {
|
|
@@ -60,12 +64,25 @@ async function morphFaces(faces: cv.Rect[], data: Buffer) {
|
|
|
|
|
|
const CAPTION_OFFSET = 5;
|
|
|
|
|
|
+async function getRandomCaption(type: FaceCaptionType) {
|
|
|
+ let repo = getRepository(FaceCaptionMessage);
|
|
|
+
|
|
|
+ let caption = await repo.query(`select message
|
|
|
+ from face_caption_message
|
|
|
+ where type = ?
|
|
|
+ order by random()
|
|
|
+ limit 1`, [ type ]) as FaceCaptionMessage[];
|
|
|
+ if(caption.length == 0)
|
|
|
+ return null;
|
|
|
+ return caption[0];
|
|
|
+}
|
|
|
+
|
|
|
async function captionFace(faces: cv.Rect[], data: Buffer) {
|
|
|
let padoru = Math.random() <= getPadoruChance();
|
|
|
let face = faces[Math.floor(Math.random() * faces.length)];
|
|
|
let squaredFace = await face.toSquareAsync();
|
|
|
|
|
|
- let targetSize = db.get("faceEditConfig.captionedImageSize").value();
|
|
|
+ let targetSize = CAPTION_IMG_SIZE;
|
|
|
|
|
|
let img = await Jimp.read(data);
|
|
|
|
|
@@ -83,7 +100,7 @@ async function captionFace(faces: cv.Rect[], data: Buffer) {
|
|
|
|
|
|
let font = await Jimp.loadFont(padoru ? Jimp.FONT_SANS_16_WHITE : Jimp.FONT_SANS_16_BLACK);
|
|
|
|
|
|
- let text = padoru ? "PADORU PADORU" : `${(db.get("faceCaptions.pre") as IRandomElementMixin).randomElement().value()} ${(db.get("faceCaptions.post") as IRandomElementMixin).randomElement().value()}`;
|
|
|
+ let text = padoru ? "PADORU PADORU" : `${await getRandomCaption(FaceCaptionType.PREFIX)} ${await getRandomCaption(FaceCaptionType.PREFIX)}`;
|
|
|
|
|
|
let h = Jimp.measureTextHeight(font, text, targetSize - CAPTION_OFFSET * 2);
|
|
|
|
|
@@ -168,7 +185,7 @@ async function processFaceSwap(message: Message, attachmentUrl: string, processo
|
|
|
if (processor)
|
|
|
jimpImage = await processor(faces, data);
|
|
|
else {
|
|
|
- if (Math.random() <= db.get("faceEditConfig.captionProbability").value())
|
|
|
+ if (Math.random() <= CAPTION_PROBABILITY)
|
|
|
jimpImage = await captionFace(faces, data);
|
|
|
else
|
|
|
jimpImage = await morphFaces(faces, data);
|
|
@@ -206,7 +223,7 @@ function processLastImage(msg: Message, processor: ImageProcessor) {
|
|
|
}
|
|
|
|
|
|
export default {
|
|
|
- onMessage: (actionsDone, msg, contents) => {
|
|
|
+ onMessage: async (actionsDone, msg, contents) => {
|
|
|
if (actionsDone) return false;
|
|
|
|
|
|
if (msg.mentions.users.size > 0 && msg.mentions.users.first().id == client.user.id)
|
|
@@ -215,11 +232,17 @@ export default {
|
|
|
let imageAttachment = msg.attachments.find(v => isValidImage(v.filename));
|
|
|
|
|
|
if (imageAttachment) {
|
|
|
- let probValue = (db.get("faceEditChannels") as ObjectChain<any>).get(msg.channel.id);
|
|
|
- if (probValue.isUndefined().value() || probValue.isNull().value()) return false;
|
|
|
|
|
|
- if (Math.random() > probValue.value()) return false;
|
|
|
+ let repo = getRepository(KnownChannel);
|
|
|
+
|
|
|
+ let knownChannel = await repo.findOne({
|
|
|
+ where: { channelId: msg.channel.id },
|
|
|
+ select: [ "faceMorphProbability" ]
|
|
|
+ });
|
|
|
|
|
|
+ if(!knownChannel || Math.random() > knownChannel.faceMorphProbability)
|
|
|
+ return false;
|
|
|
+
|
|
|
processFaceSwap(msg, imageAttachment.url).catch(err =>
|
|
|
console.log(`Failed to run faceapp because ${err}`)
|
|
|
);
|