You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Haha-Yes/commands/images/idubbbzpaint.js

79 lines
2.9 KiB
JavaScript

6 years ago
const { Command } = require('discord.js-commando');
const Discord = require('discord.js');
const { createCanvas, loadImage, getContext } = require('canvas')
const superagent = require('superagent')
const SelfReloadJSON = require('self-reload-json');
5 years ago
const blacklist = require('../../json/blacklist.json');
6 years ago
6 years ago
6 years ago
module.exports = class idubbbzpaintCommand extends Command {
6 years ago
constructor(client) {
super(client, {
6 years ago
name: 'idubbbzpaint',
6 years ago
aliases: ['idubbzpaint', 'edupspaint'],
group: 'images',
6 years ago
memberName: 'painting',
description: `Put the image you send or you in idubbbz painting`,
6 years ago
args: [
{
6 years ago
key: 'text',
6 years ago
prompt: 'What do you the paper to say?',
type: 'string',
default: 'Perfection'
}
]
6 years ago
});
}
6 years ago
async run(message, { text }) {
let blacklistJson = new SelfReloadJSON('./json/blacklist.json');
if(blacklistJson[message.author.id])
return blacklist(blacklistJson[message.author.id] , message)
6 years ago
let Attachment = (message.attachments).array();
let image = null
6 years ago
if (!Attachment[0])
image = message.author.displayAvatarURL
else if(Attachment[0] && Attachment[0].url.endsWith('gif'))
return message.say('Gif dosent work, sorry')
6 years ago
else
image = Attachment[0].url
6 years ago
const canvas = createCanvas(1024, 544)
6 years ago
const applyText = (canvas, text) => {
const ctx = canvas.getContext('2d');
// Declare a base size of the font
let fontSize = 100;
do {
// Assign the font to the context and decrement it so it can be measured again
6 years ago
ctx.font = `${fontSize -= 10}px ubuntu`;
6 years ago
} while (ctx.measureText(text).width > 800 - 300);
// Return the result to use in the actual canvas
return ctx.font;
};
6 years ago
const ctx = canvas.getContext('2d')
const background = await loadImage(image);
ctx.drawImage(background, 140, 40, 400, 340);
const { body: buffer } = await superagent.get('https://image.noelshack.com/fichiers/2018/41/7/1539533685-untitled.png').catch(error => {
return message.say('An error as occured, please try again')
})
6 years ago
const bg = await loadImage(buffer);
ctx.drawImage(bg, 0, 0, canvas.width, canvas.height);
6 years ago
ctx.font = applyText(canvas, text)
6 years ago
ctx.fillStyle = '#ffffff';
6 years ago
ctx.fillText(text, canvas.width / 3, canvas.height / 1.1);
6 years ago
6 years ago
const attachment = new Discord.Attachment(canvas.toBuffer(), 'edupspaint.png');
6 years ago
message.say(attachment).catch(error => {
message.say('an error as occured. Check the bot/channel permissions')
})
6 years ago
}
};