diff --git a/commands/avatar-edit/steam-now-playing.js b/commands/avatar-edit/steam-now-playing.js index 8fbf5b64..42929555 100644 --- a/commands/avatar-edit/steam-now-playing.js +++ b/commands/avatar-edit/steam-now-playing.js @@ -2,6 +2,7 @@ const { Command } = require('discord.js-commando'); const { createCanvas, loadImage, registerFont } = require('canvas'); const snekfetch = require('snekfetch'); const path = require('path'); +const { shortenText } = require('../../util/Canvas'); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto.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' }); @@ -48,10 +49,7 @@ module.exports = class SteamNowPlayingCommand extends Command { ctx.fillStyle = '#90ba3c'; ctx.font = '10px Noto'; ctx.fillText(user.username, 63, 26); - let shorten; - if (ctx.measureText(game).width > 160) shorten = true; - while (ctx.measureText(game).width > 160) game = game.substr(0, game.length - 1); - ctx.fillText(shorten ? `${game}...` : game, 63, 54); + ctx.fillText(shortenText(ctx, game, 160), 63, 54); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'steam-now-playing.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/image-edit/achievement.js b/commands/image-edit/achievement.js index 56274efb..68801e54 100644 --- a/commands/image-edit/achievement.js +++ b/commands/image-edit/achievement.js @@ -1,6 +1,7 @@ const { Command } = require('discord.js-commando'); const { createCanvas, loadImage, registerFont } = require('canvas'); const path = require('path'); +const { shortenText } = require('../../util/Canvas'); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Minecraftia.ttf'), { family: 'Minecraftia' }); module.exports = class AchievementCommand extends Command { @@ -34,11 +35,8 @@ module.exports = class AchievementCommand extends Command { ctx.font = '17px Minecraftia'; ctx.fillStyle = '#ffff00'; ctx.fillText('Achievement Get!', 60, 40); - let shorten; - if (ctx.measureText(text).width > 230) shorten = true; - while (ctx.measureText(text).width > 230) text = text.substr(0, text.length - 1); ctx.fillStyle = '#ffffff'; - ctx.fillText(shorten ? `${text}...` : text, 60, 60); + ctx.fillText(shortenText(ctx, text, 230), 60, 60); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'achievement.png' }] }); } }; diff --git a/util/Canvas.js b/util/Canvas.js index 48cd8091..d2c40431 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -85,6 +85,13 @@ class CanvasUtil { ctx.fillStyle = '#000000'; ctx.globalAlpha = 1; } + + static shortenText(ctx, text, maxWidth) { + let shorten; + if (ctx.measureText(text).width > maxWidth) shorten = true; + while (ctx.measureText(text).width > maxWidth) text = text.substr(0, text.length - 1); + return shorten ? `${text}...` : text; + } } module.exports = CanvasUtil;