Skip to content

Commit

Permalink
feat: extract quote formatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed May 18, 2020
1 parent 9585a5b commit b644dd2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/commands/fun/quote.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Command, CommandoClient, CommandoMessage } from "discord.js-commando";
import { Message } from "discord.js";
import dateFormat from "dateformat";

import { Quote } from "../../types";
import { loadMergedQuotes } from "../../modules/quotes";
import { loadMergedQuotes, formatQuote } from "../../modules/quotes";

class QuoteCommand extends Command {
constructor(client: CommandoClient) {
Expand All @@ -19,9 +18,8 @@ class QuoteCommand extends Command {
run = async (message: CommandoMessage): Promise<Message | Message[]> => {
const quotes = await this.load();
const quote = quotes[Math.floor(Math.random() * quotes.length)];
const date = dateFormat(new Date(quote.date), "fullDate");

return await message.say(`${quote.quote} -- ${quote.author} (${date})`);
return await message.say(formatQuote(quote));
};

load = async (): Promise<Quote[]> => {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/utils/listquotes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command, CommandoClient, CommandoMessage } from "discord.js-commando";
import { Message } from "discord.js";

import { loadMergedQuotes } from "../../modules/quotes";
import { loadMergedQuotes, formatQuote } from "../../modules/quotes";

class ListQuotesCommand extends Command {
constructor(client: CommandoClient) {
Expand All @@ -17,13 +17,13 @@ class ListQuotesCommand extends Command {
run = async (message: CommandoMessage): Promise<Message | Message[]> => {
const quotes = await loadMergedQuotes();

let reply = "";
reply += `You requested all of our quotes, enjoy!\n`;
for (const quote of quotes) {
reply += `> ${quote.quote} -- ${quote.author} (${quote.date})\n\n`;
}

await message.say("Alright! Sneaking the list into your DMs");

await message.author.send(`You requested all of our quotes, enjoy! (its limited to the last 25 sadly)\n`);
const reply = quotes
.slice(quotes.length - 25, quotes.length)
.map((q) => `> ${formatQuote(q)}\n`)
.join("\n");
return await message.author.send(reply);
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/modules/quotes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import dateFormat from "dateformat";

import { Quote } from "../types";
import { loadJson } from "../utils";
Expand All @@ -20,3 +21,9 @@ export const loadMergedQuotes = async (): Promise<Quote[]> => {

return q1.concat(q2);
};

export const formatQuote = (quote: Quote): string => {
const date = dateFormat(new Date(quote.date), "fullDate");

return `${quote.quote} -- ${quote.author} (${date})`;
};

0 comments on commit b644dd2

Please sign in to comment.