From 7ff1de753952f9cfa3f2bb699e2c14cdb46923af Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 8 Jun 2020 16:07:03 -0400 Subject: [PATCH] Format Upvotes like real reddit in subreddit commands --- commands/edit-image/tweet.js | 20 ++++++++------------ commands/random-img/awwnime.js | 4 ++-- commands/random-img/hentai.js | 4 ++-- commands/random-img/meme.js | 4 ++-- commands/random-img/porn.js | 4 ++-- commands/random-res/subreddit.js | 4 ++-- package.json | 2 +- util/Util.js | 4 ++++ 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/commands/edit-image/tweet.js b/commands/edit-image/tweet.js index 11e0ccc4..21a36d24 100644 --- a/commands/edit-image/tweet.js +++ b/commands/edit-image/tweet.js @@ -3,7 +3,7 @@ const { createCanvas, loadImage, registerFont } = require('canvas'); const moment = require('moment'); const request = require('node-superfetch'); const path = require('path'); -const { base64 } = require('../../util/Util'); +const { base64, formatNumberK } = require('../../util/Util'); const { wrapText } = require('../../util/Canvas'); const { TWITTER_KEY, TWITTER_SECRET } = process.env; registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Regular.ttf'), { family: 'Noto' }); @@ -108,21 +108,21 @@ module.exports = class TweetCommand extends Command { ctx.fillText('Twitter for Xiao', 31 + timeLen + 6, base2StartY + 16); ctx.fillStyle = '#8899a6'; ctx.font = '16px Noto'; - ctx.fillText(this.formatNumber(replies), 87, base2StartY + 139); - ctx.fillText(this.formatNumber(likes), 509, base2StartY + 139); - ctx.fillText(this.formatNumber(retweets), 300, base2StartY + 139); + ctx.fillText(formatNumberK(replies), 87, base2StartY + 139); + ctx.fillText(formatNumberK(likes), 509, base2StartY + 139); + ctx.fillText(formatNumberK(retweets), 300, base2StartY + 139); ctx.fillStyle = 'white'; ctx.font = 'normal bold 18px Noto'; - ctx.fillText(this.formatNumber(retweets), 31, base2StartY + 77); - const retweetsLen = ctx.measureText(this.formatNumber(retweets)).width; + ctx.fillText(formatNumberK(retweets), 31, base2StartY + 77); + const retweetsLen = ctx.measureText(formatNumberK(retweets)).width; ctx.fillStyle = '#8899a6'; ctx.font = '18px Noto'; ctx.fillText('Retweets', 31 + retweetsLen + 5, base2StartY + 77); const retweetsWordLen = ctx.measureText('Retweets').width; ctx.fillStyle = 'white'; ctx.font = 'normal bold 18px Noto'; - ctx.fillText(this.formatNumber(likes), 31 + retweetsLen + 5 + retweetsWordLen + 10, base2StartY + 77); - const likesLen = ctx.measureText(this.formatNumber(likes)).width; + ctx.fillText(formatNumberK(likes), 31 + retweetsLen + 5 + retweetsWordLen + 10, base2StartY + 77); + const likesLen = ctx.measureText(formatNumberK(likes)).width; ctx.fillStyle = '#8899a6'; ctx.font = '18px Noto'; ctx.fillText('Likes', 31 + retweetsLen + 5 + retweetsWordLen + 10 + likesLen + 5, base2StartY + 77); @@ -172,8 +172,4 @@ module.exports = class TweetCommand extends Command { }; } } - - formatNumber(number) { - return number > 999 ? `${(number / 1000).toLocaleString(undefined, { maximumFractionDigits: 1 })}K` : number; - } }; diff --git a/commands/random-img/awwnime.js b/commands/random-img/awwnime.js index f02c4820..17f64f48 100644 --- a/commands/random-img/awwnime.js +++ b/commands/random-img/awwnime.js @@ -1,6 +1,6 @@ const SubredditCommand = require('../../structures/commands/Subreddit'); const { MessageEmbed } = require('discord.js'); -const { formatNumber } = require('../../util/Util'); +const { formatNumberK } = require('../../util/Util'); module.exports = class AwwnimeCommand extends SubredditCommand { constructor(client) { @@ -25,6 +25,6 @@ module.exports = class AwwnimeCommand extends SubredditCommand { .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) .setTimestamp(post.created_utc * 1000) - .setFooter(`⬆ ${formatNumber(post.ups)}`); + .setFooter(`⬆ ${formatNumberK(post.ups)}`); } }; diff --git a/commands/random-img/hentai.js b/commands/random-img/hentai.js index 84c221ba..cd0c82e6 100644 --- a/commands/random-img/hentai.js +++ b/commands/random-img/hentai.js @@ -1,6 +1,6 @@ const SubredditCommand = require('../../structures/commands/Subreddit'); const { MessageEmbed } = require('discord.js'); -const { list, formatNumber } = require('../../util/Util'); +const { list, formatNumberK } = require('../../util/Util'); const subreddits = require('../../assets/json/hentai'); module.exports = class HentaiCommand extends SubredditCommand { @@ -43,6 +43,6 @@ module.exports = class HentaiCommand extends SubredditCommand { .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) .setTimestamp(post.created_utc * 1000) - .setFooter(`⬆ ${formatNumber(post.ups)}`); + .setFooter(`⬆ ${formatNumberK(post.ups)}`); } }; diff --git a/commands/random-img/meme.js b/commands/random-img/meme.js index d249965d..5ca90f27 100644 --- a/commands/random-img/meme.js +++ b/commands/random-img/meme.js @@ -1,6 +1,6 @@ const SubredditCommand = require('../../structures/commands/Subreddit'); const { MessageEmbed } = require('discord.js'); -const { list, formatNumber } = require('../../util/Util'); +const { list, formatNumberK } = require('../../util/Util'); const subreddits = require('../../assets/json/meme'); module.exports = class MemeCommand extends SubredditCommand { @@ -35,6 +35,6 @@ module.exports = class MemeCommand extends SubredditCommand { .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) .setTimestamp(post.created_utc * 1000) - .setFooter(`⬆ ${formatNumber(post.ups)}`); + .setFooter(`⬆ ${formatNumberK(post.ups)}`); } }; diff --git a/commands/random-img/porn.js b/commands/random-img/porn.js index e51874b0..b26d13bf 100644 --- a/commands/random-img/porn.js +++ b/commands/random-img/porn.js @@ -1,6 +1,6 @@ const SubredditCommand = require('../../structures/commands/Subreddit'); const { MessageEmbed } = require('discord.js'); -const { list, formatNumber } = require('../../util/Util'); +const { list, formatNumberK } = require('../../util/Util'); const subreddits = require('../../assets/json/porn'); module.exports = class PornCommand extends SubredditCommand { @@ -44,6 +44,6 @@ module.exports = class PornCommand extends SubredditCommand { .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) .setTimestamp(post.created_utc * 1000) - .setFooter(`⬆ ${formatNumber(post.ups)}`); + .setFooter(`⬆ ${formatNumberK(post.ups)}`); } }; diff --git a/commands/random-res/subreddit.js b/commands/random-res/subreddit.js index 44f0a05e..0007d909 100644 --- a/commands/random-res/subreddit.js +++ b/commands/random-res/subreddit.js @@ -1,6 +1,6 @@ const SubredditCommandBase = require('../../structures/commands/Subreddit'); const { MessageEmbed } = require('discord.js'); -const { shorten, formatNumber } = require('../../util/Util'); +const { shorten, formatNumberK } = require('../../util/Util'); module.exports = class SubredditCommand extends SubredditCommandBase { constructor(client) { @@ -32,7 +32,7 @@ module.exports = class SubredditCommand extends SubredditCommandBase { .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) .setTimestamp(post.created_utc * 1000) - .setFooter(`⬆ ${formatNumber(post.score)}`); + .setFooter(`⬆ ${formatNumberK(post.score)}`); if (post.thumbnail && post.thumbnail !== 'self' && post.post_hint !== 'image') { embed.setThumbnail(post.thumbnail); } diff --git a/package.json b/package.json index 5a509e2c..1d4119ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.8.0", + "version": "116.8.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index 01e9e98c..b6dc6325 100644 --- a/util/Util.js +++ b/util/Util.js @@ -72,6 +72,10 @@ module.exports = class Util { }); } + static formatNumberK(number) { + return number > 999 ? `${(number / 1000).toLocaleString(undefined, { maximumFractionDigits: 1 })}K` : number; + } + static base64(text, mode = 'encode') { if (mode === 'encode') return Buffer.from(text).toString('base64'); if (mode === 'decode') return Buffer.from(text, 'base64').toString('utf8') || null;