diff --git a/README.md b/README.md index a0d3c6de..924166af 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Xiao is a Discord bot coded in JavaScript with The bot is no longer available for invite. You can self-host the bot, or use her on the [home server](https://discord.gg/sbMe32W). -## Commands (304) +## Commands (306) ### Utility: * **eval:** Executes JavaScript code. @@ -244,6 +244,7 @@ on the [home server](https://discord.gg/sbMe32W). * **illegal:** Makes President Trump make your text illegal. * **invert:** Draws an image or a user's avatar but inverted. * **minecraft-skin:** Sends the Minecraft skin for a user. +* **nike-ad:** Sends a "Believe in Something" Nike Ad meme with the text of your choice. * **needs-more-jpeg:** Draws an image or a user's avatar as a low quality JPEG. * **osu-signature:** Creates a card based on an osu! user's stats. * **pixelize:** Draws an image or a user's avatar pixelized. @@ -332,6 +333,7 @@ on the [home server](https://discord.gg/sbMe32W). ### Number Manipulation: +* **character-count:** Responds with the character count of text. * **currency:** Converts money from one currency to another. * **final-grade-calculator:** Determines the grade you need to make on your final to get your desired course grade. * **gravity:** Determines weight on another planet. diff --git a/assets/images/nike-ad.png b/assets/images/nike-ad.png new file mode 100644 index 00000000..29311b5b Binary files /dev/null and b/assets/images/nike-ad.png differ diff --git a/commands/image-edit/nike-ad.js b/commands/image-edit/nike-ad.js new file mode 100644 index 00000000..d74e91c6 --- /dev/null +++ b/commands/image-edit/nike-ad.js @@ -0,0 +1,71 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage, registerFont } = require('canvas'); +const request = require('node-superfetch'); +const path = require('path'); +const { wrapText, greyscale, drawImageWithTint } = require('../../util/Canvas'); +registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Regular.ttf'), { family: 'Noto' }); +registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' }); +registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Emoji.ttf'), { family: 'Noto' }); + +module.exports = class NikeAdCommand extends Command { + constructor(client) { + super(client, { + name: 'nike-ad', + aliases: ['believe-in-something', 'believe-in'], + group: 'image-edit', + memberName: 'nike-ad', + description: 'Sends a "Believe in Something" Nike Ad meme with the text of your choice.', + throttling: { + usages: 1, + duration: 10 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'something', + prompt: 'What should the something to believe in be?', + type: 'string', + max: 20, + parse: something => something.toLowerCase() + }, + { + key: 'sacrifice', + prompt: 'What should believing result in (e.g. sacrificing everything)?', + type: 'string', + max: 50, + parse: sacrifice => sacrifice.toLowerCase() + }, + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 2048 }) + } + ] + }); + } + + async run(msg, { image, something, sacrifice }) { + try { + const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'nike-ad.png')); + const { body } = await request.get(image); + const data = await loadImage(body); + const canvas = createCanvas(data.width, data.height); + const ctx = canvas.getContext('2d'); + drawImageWithTint(ctx, data, 'black', 0, 0, data.width, data.height); + greyscale(ctx, 0, 0, data.width, data.height); + ctx.drawImage(base, data.width / 2 - base.width / 2, base.height - data.height); + ctx.font = `${Math.round(data.height / 25)}px Noto`; + ctx.fillStyle = 'white'; + ctx.textAlign = 'center'; + ctx.textBaseline = 'middle'; + const text = wrapText(ctx, `Believe in ${something}. Even if it means ${sacrifice}.`, data.width); + ctx.fillText(text.join('\n'), data.width / 2, data.height / 2); + const attachment = canvas.toBuffer(); + if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); + return msg.say({ files: [{ attachment, name: 'nike-ad.png' }] }); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/number-edit/character-count.js b/commands/number-edit/character-count.js new file mode 100644 index 00000000..45e94519 --- /dev/null +++ b/commands/number-edit/character-count.js @@ -0,0 +1,24 @@ +const Command = require('../../structures/Command'); + +module.exports = class CharacterCountCommand extends Command { + constructor(client) { + super(client, { + name: 'character-count', + aliases: ['characters', 'chars', 'length'], + group: 'number-edit', + memberName: 'character-count', + description: 'Responds with the character count of text.', + args: [ + { + key: 'text', + prompt: 'What text would you like to get the character count of?', + type: 'string' + } + ] + }); + } + + run(msg, { text }) { + return msg.reply(text.length); + } +}; diff --git a/package.json b/package.json index 4169251e..49444016 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "91.0.3", + "version": "91.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Canvas.js b/util/Canvas.js index ce33ea3b..3a1ead53 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -95,4 +95,32 @@ module.exports = class CanvasUtil { } return shorten ? `${text}...` : text; } + + static wrapText(ctx, text, maxWidth) { + if (ctx.measureText(text).width < maxWidth) return [text]; + const words = text.split(' '); + const lines = []; + let line = ''; + while (words.length > 0) { + let split = false; + while (ctx.measureText(words[0]).width >= maxWidth) { + const temp = words[0]; + words[0] = temp.slice(0, -1); + if (split) { + words[1] = `${temp.slice(-1)}${words[1]}`; + } else { + split = true; + words.splice(1, 0, temp.slice(-1)); + } + } + if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) { + line += `${words.shift()} `; + } else { + lines.push(line.trim()); + line = ''; + } + if (words.length === 0) lines.push(line.trim()); + } + return lines; + } };