From e88bc1b75815c40077d441aea8573e7105a766d6 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 24 Apr 2021 11:49:29 -0400 Subject: [PATCH] Analog Clock Command --- Xiao.js | 24 ++++- commands/edit-image/analog-clock.js | 135 ++++++++++++++++++++++++++++ commands/events/time.js | 3 - package.json | 2 +- structures/Client.js | 7 ++ 5 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 commands/edit-image/analog-clock.js diff --git a/Xiao.js b/Xiao.js index ac9aea69..26da2fb9 100644 --- a/Xiao.js +++ b/Xiao.js @@ -160,12 +160,28 @@ client.on('ready', async () => { client.logger.info(`[BLACKLIST] Left ${guildsLeft} guilds owned by blacklisted users.`); // Set up existing timers - await client.timers.fetchAll(); - client.logger.info('[TIMERS] All timers imported.'); + try { + await client.timers.fetchAll(); + client.logger.info('[TIMERS] All timers imported.'); + } catch (err) { + client.logger.error(`[TIMERS] Failed to import timers\n${err.stack}`); + } // Register all canvas fonts - await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts')); - client.logger.info('[FONTS] All fonts loaded.'); + try { + await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts')); + client.logger.info('[FONTS] All fonts loaded.'); + } catch (err) { + client.logger.error(`[FONTS] Failed to load fonts\n${err.stack}`); + } + + // Set up moment timezones + try { + client.setTimezones(); + client.logger.info('[TIMEZONES] Set all custom timezones.'); + } catch (err) { + client.logger.error(`[TIMEZONES] Failed to set timezones\n${err.stack}`); + } // Fetch adult site list try { diff --git a/commands/edit-image/analog-clock.js b/commands/edit-image/analog-clock.js new file mode 100644 index 00000000..f58344fc --- /dev/null +++ b/commands/edit-image/analog-clock.js @@ -0,0 +1,135 @@ +const Command = require('../../structures/Command'); +const { createCanvas } = require('canvas'); +const moment = require('moment-timezone'); +const { firstUpperCase } = require('../../util/Util'); + +module.exports = class AnalogClockCommand extends Command { + constructor(client) { + super(client, { + name: 'analog-clock', + aliases: ['analog-time', 'analog-time-zone', 'clock', 'analog'], + group: 'edit-image', + memberName: 'analog-clock', + description: 'Draws an analog clock for a timezone.', + details: '**Zones:** ', + credit: [ + { + name: 'Wikipedia', + url: 'https://www.wikipedia.org/', + reason: 'Time Zone Data', + reasonURL: 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones' + }, + { + name: 'Neopets', + url: 'http://www.neopets.com/', + reason: 'Neopia Time Zone' + }, + { + name: 'Google', + url: 'https://www.google.com/', + reason: 'Noto Font', + reasonURL: 'https://www.google.com/get/noto/' + } + ], + args: [ + { + key: 'timeZone', + label: 'time zone', + prompt: 'Which time zone do you want to get the time of?', + type: 'string', + parse: timeZone => timeZone.replaceAll(' ', '_').toLowerCase() + } + ] + }); + } + + run(msg, { timeZone }) { + if (!moment.tz.zone(timeZone)) { + return msg.reply('Invalid time zone. Refer to .'); + } + const time = moment().tz(timeZone); + const location = timeZone.split('/'); + const main = firstUpperCase(location[0], /[_ ]/); + const sub = location[1] ? firstUpperCase(location[1], /[_ ]/) : null; + const subMain = location[2] ? firstUpperCase(location[2], /[_ ]/) : null; + const parens = sub ? ` (${subMain ? `${sub}, ` : ''}${main})` : ''; + const canvas = createCanvas(1000, 1000); + const ctx = canvas.getContext('2d'); + let radius = canvas.height / 2; + ctx.translate(radius, radius); + radius = radius * 0.9; + this.drawFace(ctx, radius); + this.drawNumbers(ctx, radius); + this.drawTime(ctx, radius, time, time.format('A')); + return msg.say(`${subMain || sub || main}${parens}`, { + files: [{ attachment: canvas.toBuffer(), name: 'analog-clock.png' }] + }); + } + + drawFace(ctx, radius) { + ctx.beginPath(); + ctx.arc(0, 0, radius, 0, 2 * Math.PI); + ctx.fillStyle = 'white'; + ctx.fill(); + let grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05); + grad.addColorStop(0, '#333'); + grad.addColorStop(0.5, 'white'); + grad.addColorStop(1, '#333'); + ctx.strokeStyle = grad; + ctx.lineWidth = radius * 0.1; + ctx.stroke(); + ctx.beginPath(); + ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI); + ctx.fillStyle = '#333'; + ctx.fill(); + return ctx; + } + + drawNumbers(ctx, radius) { + ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(radius * 0.15); + ctx.textBaseline = 'middle'; + ctx.textAlign = 'center'; + for (let num = 1; num < 13; num++) { + const ang = (num * Math.PI) / 6; + ctx.rotate(ang); + ctx.translate(0, -radius * 0.85); + ctx.rotate(-ang); + ctx.fillText(num.toString(), 0, 0); + ctx.rotate(ang); + ctx.translate(0, radius * 0.85); + ctx.rotate(-ang); + } + return ctx; + } + + drawTime(ctx, radius, time, meridiem) { + let hour = time.hours(); + let minute = time.minutes(); + let second = time.seconds(); + hour = hour % 12; + hour = (hour * (Math.PI / 6)) + ((minute * Math.PI) / (6 * 60)) + ((second * Math.PI) / (360 * 60)); + this.drawHand(ctx, hour, radius * 0.5, radius * 0.07); + minute = (minute * (Math.PI / 30)) + ((second * Math.PI) / (30 * 60)); + this.drawHand(ctx, minute, radius * 0.8, radius * 0.07); + second = (second * (Math.PI / 30)); + this.drawHand(ctx, second, radius * 0.9, radius * 0.02); + ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(radius * 0.15); + ctx.textBaseline = 'middle'; + ctx.textAlign = 'center'; + ctx.fillStyle = 'white'; + ctx.fillText(meridiem, ctx.canvas.width - 10, ctx.canvas.height - 10); + return ctx; + } + + drawHand(ctx, pos, length, width) { + ctx.beginPath(); + ctx.lineWidth = width; + ctx.lineCap = 'round'; + ctx.moveTo(0, 0); + ctx.rotate(pos); + ctx.lineTo(0, -length); + ctx.stroke(); + ctx.rotate(-pos); + return ctx; + } +}; diff --git a/commands/events/time.js b/commands/events/time.js index 92169a4d..bb0e5770 100644 --- a/commands/events/time.js +++ b/commands/events/time.js @@ -1,9 +1,6 @@ const Command = require('../../structures/Command'); const moment = require('moment-timezone'); const { firstUpperCase } = require('../../util/Util'); -moment.tz.link('America/Vancouver|Neopia'); -moment.tz.link('America/Los_Angeles|Discord'); -moment.tz.link('America/New_York|Dragon'); module.exports = class TimeCommand extends Command { constructor(client) { diff --git a/package.json b/package.json index ad4ea23c..e978aa5c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "136.5.0", + "version": "136.6.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true, diff --git a/structures/Client.js b/structures/Client.js index 9c0f0c28..7372d182 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -5,6 +5,7 @@ const Collection = require('@discordjs/collection'); const winston = require('winston'); const fontFinder = require('font-finder'); const nsfw = require('nsfwjs'); +const moment = require('moment-timezone'); const fs = require('fs'); const url = require('url'); const path = require('path'); @@ -65,6 +66,12 @@ module.exports = class XiaoClient extends CommandoClient { return this.fonts; } + setTimezones() { + moment.tz.link('America/Vancouver|Neopia'); + moment.tz.link('America/Los_Angeles|Discord'); + moment.tz.link('America/New_York|Dragon'); + } + async postTopGGStats() { if (!TOP_GG_TOKEN) return null; try {