From f349d285ff8e2153008549823cf17df865d32e24 Mon Sep 17 00:00:00 2001 From: BenSegal855 Date: Tue, 29 Dec 2020 14:11:42 -0500 Subject: [PATCH 1/4] feat: Random XKCD comic --- src/commands/Fun/xkcd.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/commands/Fun/xkcd.ts b/src/commands/Fun/xkcd.ts index af0feb6f..02517bad 100644 --- a/src/commands/Fun/xkcd.ts +++ b/src/commands/Fun/xkcd.ts @@ -21,29 +21,45 @@ interface XkcdComic { } @ApplyOptions({ + aliases: ['x'], cooldown: 15, cooldownLevel: 'author', description: lang => lang.tget('COMMAND_XKCD_DESCRIPTION'), extendedHelp: lang => lang.tget('COMMAND_XKCD_EXTENDED'), requiredPermissions: ['EMBED_LINKS'], - usage: '[comicNumber:integer]' + subcommands: true, + usage: ' [comicNumber:integer]' }) export default class extends SteveCommand { - public async run(msg: KlasaMessage, [comicID]: [number]) { + public async random(msg: KlasaMessage, [comicID]: [number]) { + const comic = comicID + ? await this.getXkcdByNumber(comicID).catch(() => { throw msg.language.tget('COMMAND_XKCD_INVALID'); }) + : await this.getRandomXkcd(); + + return msg.channel.send(this.createComicEmbed(comic)); + } + + public async new(msg: KlasaMessage, [comicID]: [number]) { const comic = comicID ? await this.getXkcdByNumber(comicID).catch(() => { throw msg.language.tget('COMMAND_XKCD_INVALID'); }) : await this.getCurrentXkcd(); - const embed = new MessageEmbed() + return msg.channel.send(this.createComicEmbed(comic)); + } + + /** + * + * @param comic The Xkcd commic used to form the embed + */ + private createComicEmbed(comic: XkcdComic): MessageEmbed { + return new MessageEmbed() .setColor(0x2242c7) .setDescription(comic.transcript || comic.alt) .setImage(comic.img) .setTimestamp() .setTitle(oneLine`${comic.safe_title} (#${comic.num}, ${formatDate(new Date(Number(comic.year), Number(comic.month) - 1, Number(comic.day)), 'YYYY MMMM Do')})`); - - return msg.channel.send(embed); } private async getCurrentXkcd() { @@ -51,6 +67,11 @@ export default class extends SteveCommand { return res.data; } + private async getRandomXkcd() { + const comicID = Math.trunc((Math.random() * ((await this.getCurrentXkcd()).num - 1)) + 1); + return this.getXkcdByNumber(comicID); + } + /** * * @param id The ID of the desired comic From 27e96e3160c329336c10c8b365565db292ccac6f Mon Sep 17 00:00:00 2001 From: BenSegal855 Date: Tue, 29 Dec 2020 15:10:56 -0500 Subject: [PATCH 2/4] tweak: replace dubble braces with single braces --- src/commands/Fun/xkcd.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/commands/Fun/xkcd.ts b/src/commands/Fun/xkcd.ts index 02517bad..a97b9032 100644 --- a/src/commands/Fun/xkcd.ts +++ b/src/commands/Fun/xkcd.ts @@ -53,9 +53,16 @@ export default class extends SteveCommand { * @param comic The Xkcd commic used to form the embed */ private createComicEmbed(comic: XkcdComic): MessageEmbed { + const description = (comic.transcript || comic.alt) + .replace('{{', '{') + .replace('}}', '}') + .replace('[[', '[') + .replace(']]', ']') + .replace('<<', '<') + .replace('>>', '>'); return new MessageEmbed() .setColor(0x2242c7) - .setDescription(comic.transcript || comic.alt) + .setDescription(description) .setImage(comic.img) .setTimestamp() .setTitle(oneLine`${comic.safe_title} (#${comic.num}, From e1481600514cdf5c4ab6eea7e1cf76e0c86bdb0c Mon Sep 17 00:00:00 2001 From: BenSegal855 Date: Tue, 29 Dec 2020 15:18:53 -0500 Subject: [PATCH 3/4] feat: add link --- src/commands/Fun/xkcd.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/Fun/xkcd.ts b/src/commands/Fun/xkcd.ts index a97b9032..8dbde3b9 100644 --- a/src/commands/Fun/xkcd.ts +++ b/src/commands/Fun/xkcd.ts @@ -62,7 +62,7 @@ export default class extends SteveCommand { .replace('>>', '>'); return new MessageEmbed() .setColor(0x2242c7) - .setDescription(description) + .setDescription(`${description}\n\nhttps://xkcd.com/${comic.num}/`) .setImage(comic.img) .setTimestamp() .setTitle(oneLine`${comic.safe_title} (#${comic.num}, From 30b63a351b8db0cee561d9f097a95c4c4dbd815a Mon Sep 17 00:00:00 2001 From: BenSegal855 Date: Tue, 29 Dec 2020 15:23:27 -0500 Subject: [PATCH 4/4] fix: actually replace all the double braces --- src/commands/Fun/xkcd.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/Fun/xkcd.ts b/src/commands/Fun/xkcd.ts index 8dbde3b9..57d6269c 100644 --- a/src/commands/Fun/xkcd.ts +++ b/src/commands/Fun/xkcd.ts @@ -54,12 +54,12 @@ export default class extends SteveCommand { */ private createComicEmbed(comic: XkcdComic): MessageEmbed { const description = (comic.transcript || comic.alt) - .replace('{{', '{') - .replace('}}', '}') - .replace('[[', '[') - .replace(']]', ']') - .replace('<<', '<') - .replace('>>', '>'); + .replace(/{{/g, '{') + .replace(/}}/g, '}') + .replace(/\[\[/g, '[') + .replace(/]]/g, ']') + .replace(/<>/g, '>'); return new MessageEmbed() .setColor(0x2242c7) .setDescription(`${description}\n\nhttps://xkcd.com/${comic.num}/`)