From 9c8847bd2a6875ceb8dafbc34959ad2e6878ed08 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 27 Mar 2021 16:45:53 -0400 Subject: [PATCH] Snapchat Command --- commands/edit-image/snapchat.js | 78 +++++++++++++++++++++++++++++++++ commands/edit-image/snapcode.js | 1 - package.json | 3 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 commands/edit-image/snapchat.js diff --git a/commands/edit-image/snapchat.js b/commands/edit-image/snapchat.js new file mode 100644 index 00000000..52e617de --- /dev/null +++ b/commands/edit-image/snapchat.js @@ -0,0 +1,78 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const visualCenter = require('visual-center'); + +module.exports = class SnapchatCommand extends Command { + constructor(client) { + super(client, { + name: 'snapchat', + aliases: ['snap'], + group: 'edit-image', + memberName: 'snapchat', + description: 'Creates a fake Snap from Snapchat with the image and text you provide.', + throttling: { + usages: 2, + duration: 10 + }, + clientPermissions: ['ATTACH_FILES'], + credit: [ + { + name: 'Google', + url: 'https://www.google.com/', + reason: 'Noto Font', + reasonURL: 'https://www.google.com/get/noto/' + }, + { + name: 'Snapchat', + url: 'https://www.snapchat.com/', + reason: 'Design' + } + ], + args: [ + { + key: 'text', + prompt: 'What text should the snap say?', + type: 'string', + max: 32 + }, + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image-or-avatar', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 }) + } + ] + }); + } + + async run(msg, { text, image }) { + const { body } = await request.get(image); + const base = await loadImage(body); + const canvas = createCanvas(base.width, base.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(base, 0, 0); + ctx.textAlign = 'center'; + ctx.textBaseline = 'top'; + ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(base.height / 24); + const barHeight = (base.height / 24) * 2; + const { visualTop } = await this.visualCenter(body); + const moveCenter = visualTop > 0.5 ? 1 - visualTop - 0.5 : visualTop < 0.5 ? 1 + (visualTop - 0.5) : 1; + const barPosition = base.height - (base.height * moveCenter); + ctx.globalAlpha = 0.3; + ctx.fillStyle = 'black'; + ctx.fillRect(0, barPosition - barHeight, base.width, barHeight); + ctx.globalAlpha = 1; + ctx.fillText(text, base.width / 2, (barPosition - barHeight) * 0.4); + return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'snapchat.png' }] }); + } + + visualCenter(image) { + return new Promise((res, rej) => { + visualCenter(image.toString('base64'), (err, data) => { + if (err) return rej(err); + return res(data); + }); + }); + } +}; diff --git a/commands/edit-image/snapcode.js b/commands/edit-image/snapcode.js index ca466ed0..a989dc90 100644 --- a/commands/edit-image/snapcode.js +++ b/commands/edit-image/snapcode.js @@ -5,7 +5,6 @@ module.exports = class SnapcodeCommand extends Command { constructor(client) { super(client, { name: 'snapcode', - aliases: ['snapchat'], group: 'edit-image', memberName: 'snapcode', description: 'Responds with the Snapcode of a Snapchat user.', diff --git a/package.json b/package.json index 42df36d0..2577bb9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "134.2.0", + "version": "134.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -79,6 +79,7 @@ "tictactoe-minimax-ai": "^1.2.1", "user-agents": "^1.0.602", "valid-url": "^1.0.9", + "visual-center": "^0.1.1", "wavefile": "^11.0.0", "winston": "^3.3.3", "ytdl-core": "^4.5.0"