Skip to content

Commit

Permalink
book lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Aug 26, 2023
1 parent cc7c45c commit 4a5d5a8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@ node_modules
.env
config.json
invocations.json
command_limits.json
command_limits.json
books.json
15 changes: 15 additions & 0 deletions 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;
}
108 changes: 59 additions & 49 deletions 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);
}
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);
}

0 comments on commit 4a5d5a8

Please sign in to comment.