mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-22 10:19:11 +02:00
shortenText Util Command
This commit is contained in:
@@ -2,6 +2,7 @@ const { Command } = require('discord.js-commando');
|
|||||||
const { createCanvas, loadImage, registerFont } = require('canvas');
|
const { createCanvas, loadImage, registerFont } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const path = require('path');
|
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.ttf'), { family: 'Noto' });
|
||||||
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' });
|
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' });
|
||||||
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Emoji.ttf'), { 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.fillStyle = '#90ba3c';
|
||||||
ctx.font = '10px Noto';
|
ctx.font = '10px Noto';
|
||||||
ctx.fillText(user.username, 63, 26);
|
ctx.fillText(user.username, 63, 26);
|
||||||
let shorten;
|
ctx.fillText(shortenText(ctx, game, 160), 63, 54);
|
||||||
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);
|
|
||||||
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'steam-now-playing.png' }] });
|
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'steam-now-playing.png' }] });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage, registerFont } = require('canvas');
|
const { createCanvas, loadImage, registerFont } = require('canvas');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { shortenText } = require('../../util/Canvas');
|
||||||
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Minecraftia.ttf'), { family: 'Minecraftia' });
|
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Minecraftia.ttf'), { family: 'Minecraftia' });
|
||||||
|
|
||||||
module.exports = class AchievementCommand extends Command {
|
module.exports = class AchievementCommand extends Command {
|
||||||
@@ -34,11 +35,8 @@ module.exports = class AchievementCommand extends Command {
|
|||||||
ctx.font = '17px Minecraftia';
|
ctx.font = '17px Minecraftia';
|
||||||
ctx.fillStyle = '#ffff00';
|
ctx.fillStyle = '#ffff00';
|
||||||
ctx.fillText('Achievement Get!', 60, 40);
|
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.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' }] });
|
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'achievement.png' }] });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -85,6 +85,13 @@ class CanvasUtil {
|
|||||||
ctx.fillStyle = '#000000';
|
ctx.fillStyle = '#000000';
|
||||||
ctx.globalAlpha = 1;
|
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;
|
module.exports = CanvasUtil;
|
||||||
|
|||||||
Reference in New Issue
Block a user