Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ node_modules | |
.env | ||
config.json | ||
invocations.json | ||
command_limits.json | ||
command_limits.json | ||
books.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |