Analog Clock Command

This commit is contained in:
Dragon Fire
2021-04-24 11:49:29 -04:00
parent 64c2c9096d
commit e88bc1b758
5 changed files with 163 additions and 8 deletions
+20 -4
View File
@@ -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 {
+135
View File
@@ -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:** <https://en.wikipedia.org/wiki/List_of_tz_database_time_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 <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>.');
}
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;
}
};
-3
View File
@@ -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) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "136.5.0",
"version": "136.6.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"private": true,
+7
View File
@@ -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 {