From 4a5d5a8eb5e8d562679f522b0be2dff497371aff Mon Sep 17 00:00:00 2001 From: Bram Adams <3282661+bramses@users.noreply.github.com> Date: Sat, 26 Aug 2023 17:35:08 -0400 Subject: [PATCH] book lookup --- .gitignore | 3 +- books.js | 15 +++++ commands/quoordinates/quos.js | 108 +++++++++++++++++++--------------- 3 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 books.js diff --git a/.gitignore b/.gitignore index c55a9c6..1a1f086 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules .env config.json invocations.json -command_limits.json \ No newline at end of file +command_limits.json +books.json \ No newline at end of file diff --git a/books.js b/books.js new file mode 100644 index 0000000..d3bfdf5 --- /dev/null +++ b/books.js @@ -0,0 +1,15 @@ +import fs from 'fs'; + +// lookup title in books.json and return url if found + + +const books = JSON.parse(fs.readFileSync('./books.json', 'utf8')); + +export const lookupBook = (title) => { + for (const book of books.books) { + if (book.title.toLowerCase().includes(title.toLowerCase())) { + return book.link; + } + } + return null; +} diff --git a/commands/quoordinates/quos.js b/commands/quoordinates/quos.js index 0452a6d..2687d2e 100644 --- a/commands/quoordinates/quos.js +++ b/commands/quoordinates/quos.js @@ -1,58 +1,68 @@ -import config from '../../config.json' assert { "type": "json" }; -import { SlashCommandBuilder, ChannelType } from 'discord.js'; -import { invocationWorkflow, preWorkflow } from '../../invocation.js'; +import config from "../../config.json" assert { "type": "json" }; +import { SlashCommandBuilder, ChannelType } from "discord.js"; +import { invocationWorkflow, preWorkflow } from "../../invocation.js"; +import { lookupBook } from "../../books.js"; const { quoordinates_server } = config; // post to quoordinates server with the user's input as the body.query async function main(query) { - const response = await fetch(quoordinates_server, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ query }), - }); - const json = await response.json(); - console.log(json); - return json; - } + const response = await fetch(quoordinates_server, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ query }), + }); + const json = await response.json(); + console.log(json); + return json; +} export const data = new SlashCommandBuilder() - .setName('quos') - .setDescription('Generates a random quoordinate.') - .addStringOption(option => - option.setName('input') - .setDescription('The text to generate a quoordinate from.') - .setRequired(true) - ); + .setName("quos") + .setDescription("Generates a random quoordinate.") + .addStringOption((option) => + option + .setName("input") + .setDescription("The text to generate a quoordinate from.") + .setRequired(true) + ); export async function execute(interaction) { - await interaction.deferReply(); - - if (!await preWorkflow(interaction)) { - await interaction.editReply('You have reached your monthly limit for this command: ' + interaction.commandName); - return; - } - - - const userInput = interaction.options.getString('input'); - const quoordinate = await main(userInput); - - - const quotes = quoordinate.map(q => `> ${q.text}\n\n-- ${q.title}\n\n`).filter(q => q.length < 2000); - - const thread = await interaction.channel.threads.create({ - name: userInput, - autoArchiveDuration: 60, - startMessage: interaction.channel.lastMessage, - type: ChannelType.GUILD_PUBLIC_THREAD, - reason: 'Sending quotes as separate messages in one thread', - }); - - for (const quote of quotes) { - await thread.send(quote); - } - await interaction.editReply(`Results: ${quoordinate.length} quotes`); - await invocationWorkflow(interaction); -} \ No newline at end of file + await interaction.deferReply(); + + if (!(await preWorkflow(interaction))) { + await interaction.editReply( + "You have reached your monthly limit for this command: " + + interaction.commandName + ); + return; + } + + const userInput = interaction.options.getString("input"); + const quoordinate = await main(userInput); + + const quotes = quoordinate + .map( + (q) => + `> ${q.text}\n\n-- ${ + lookupBook(q.title) ? `[${q.title}](${lookupBook(q.title)})` : q.title + }\n\n` + ) + .filter((q) => q.length < 2000); + + const thread = await interaction.channel.threads.create({ + name: userInput, + autoArchiveDuration: 60, + startMessage: interaction.channel.lastMessage, + type: ChannelType.GUILD_PUBLIC_THREAD, + reason: "Sending quotes as separate messages in one thread", + }); + + for (const quote of quotes) { + await thread.send(quote); + } + await interaction.editReply(`Results: ${quoordinate.length} quotes`); + await invocationWorkflow(interaction); +}