From a829bc8de1309e3aec5940f974af0f35b3512385 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Fri, 13 Oct 2017 23:33:57 +0000 Subject: [PATCH] Silhouette --- commands/avatar-edit/silhouette.js | 47 +++++++++++++++++++++++++++++ commands/games/whos-that-pokemon.js | 28 ++++++++++++++--- util/Util.js | 11 +++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 commands/avatar-edit/silhouette.js diff --git a/commands/avatar-edit/silhouette.js b/commands/avatar-edit/silhouette.js new file mode 100644 index 00000000..de1bd408 --- /dev/null +++ b/commands/avatar-edit/silhouette.js @@ -0,0 +1,47 @@ +const { Command } = require('discord.js-commando'); +const { createCanvas, loadImage } = require('canvas'); +const snekfetch = require('snekfetch'); +const { silhouette } = require('../../util/Util'); + +module.exports = class SilhouetteCommand extends Command { + constructor(client) { + super(client, { + name: 'silhouette', + group: 'avatar-edit', + memberName: 'silhouette', + description: 'Draws a silhouette of a user\'s avatar.', + throttling: { + usages: 1, + duration: 15 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'user', + prompt: 'Which user would you like to edit the avatar of?', + type: 'user', + default: '' + } + ] + }); + } + + async run(msg, { user }) { + if (!user) user = msg.author; + const avatarURL = user.displayAvatarURL({ + format: 'png', + size: 512 + }); + try { + const { body } = await snekfetch.get(avatarURL); + const avatar = await loadImage(body); + const canvas = createCanvas(avatar.width, avatar.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(avatar, 0, 0); + silhouette(ctx, 0, 0, avatar.width, avatar.height); + return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'silhouette.png' }] }); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/games/whos-that-pokemon.js b/commands/games/whos-that-pokemon.js index d59f5894..403f15ce 100644 --- a/commands/games/whos-that-pokemon.js +++ b/commands/games/whos-that-pokemon.js @@ -1,6 +1,7 @@ const { Command } = require('discord.js-commando'); +const { createCanvas, loadImage } = require('canvas'); const snekfetch = require('snekfetch'); -const { filterPkmn, pad } = require('../../util/Util'); +const { filterPkmn, pad, silhouette } = require('../../util/Util'); module.exports = class WhosThatPokemonCommand extends Command { constructor(client) { @@ -10,18 +11,35 @@ module.exports = class WhosThatPokemonCommand extends Command { group: 'games', memberName: 'whos-that-pokemon', description: 'Guess who that Pokémon is.', - clientPermissions: ['ATTACH_FILES'] + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'hide', + prompt: 'Do you want to silhouette the Pokémon\'s image?', + type: 'boolean', + default: false + } + ] }); } - async run(msg) { + async run(msg, { hide }) { const pokemon = Math.floor(Math.random() * 721) + 1; try { const { body } = await snekfetch.get(`https://pokeapi.co/api/v2/pokemon-species/${pokemon}/`); const names = body.names.map(name => name.name.toLowerCase()); const displayName = filterPkmn(body.names).name; - const image = `https://www.serebii.net/sunmoon/pokemon/${pad(body.id.toString(), '000')}.png`; - await msg.say('**You have 15 seconds, who\'s that Pokémon?**', { files: [image] }); + const id = pad(body.id.toString(), '000'); + const image = await snekfetch.get(`https://www.serebii.net/sunmoon/pokemon/${id}.png`); + let attachment = image.body; + if (hide) { + const base = await loadImage(image.body); + const canvas = createCanvas(base.width, base.height); + const ctx = canvas.getContext('2d'); + silhouette(ctx, 0, 0, base.width, base.height); + attachment = canvas.toBuffer(); + } + await msg.say('**You have 15 seconds, who\'s that Pokémon?**', { files: [{ attachment, name: `${id}.png` }] }); const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { max: 1, time: 15000 diff --git a/util/Util.js b/util/Util.js index 87f0fd2e..83eecfcd 100644 --- a/util/Util.js +++ b/util/Util.js @@ -83,6 +83,17 @@ class Util { ctx.putImageData(data, x, y); return ctx; } + + static silhouette(ctx, x, y, width, height) { + const data = ctx.getImageData(x, y, width, height); + for (let i = 0; i < data.data.length; i += 4) { + data.data[i] = 0; + data.data[i + 1] = 0; + data.data[i + 2] = 0; + } + ctx.putImageData(data, x, y); + return ctx; + } } module.exports = Util;