2024-09-14 21:51:31 +01:00
|
|
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
2024-09-08 00:34:08 +01:00
|
|
|
const Word = require("../../models/wordModel");
|
|
|
|
const axios = require("axios");
|
|
|
|
|
2024-09-14 21:51:31 +01:00
|
|
|
const WORDNIK_API_KEY = process.env.WORDNIK_API_KEY;
|
|
|
|
|
|
|
|
// Function to clean up XML-like tags from the text
|
|
|
|
function cleanText(text) {
|
|
|
|
// Ensure text is a string and remove XML-like tags
|
|
|
|
return (text || "").replace(/<[^>]*>/g, "");
|
|
|
|
}
|
|
|
|
|
2024-09-08 00:34:08 +01:00
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName("dictionary")
|
|
|
|
.setDescription("Look up a word in the dictionary.")
|
|
|
|
.addStringOption((option) =>
|
|
|
|
option
|
|
|
|
.setName("word")
|
|
|
|
.setDescription("The word to look up")
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
.addBooleanOption((option) =>
|
|
|
|
option
|
|
|
|
.setName("ephemeral")
|
|
|
|
.setDescription("Whether the response should be ephemeral")
|
|
|
|
.setRequired(false)
|
|
|
|
),
|
|
|
|
async execute(interaction) {
|
|
|
|
const word = interaction.options.getString("word").toLowerCase();
|
|
|
|
const isEphemeral = interaction.options.getBoolean("ephemeral") || false;
|
2024-09-14 21:51:31 +01:00
|
|
|
const wordnikUrl = `https://www.wordnik.com/words/${word}`;
|
|
|
|
|
|
|
|
// Create the base embed
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor("#0099ff")
|
|
|
|
.setTitle(`Dictionary: ${word.charAt(0).toUpperCase() + word.slice(1)}`)
|
|
|
|
.setURL(wordnikUrl) // Set URL to the Wordnik page for the word
|
|
|
|
.setFooter({
|
|
|
|
text: "Powered by Wordnik | Source: Loading...",
|
|
|
|
iconURL: "https://wordnik.com/favicon.ico",
|
|
|
|
});
|
2024-09-08 00:34:08 +01:00
|
|
|
|
|
|
|
// Try to find the word in the database
|
|
|
|
let result = await Word.findOne({ word });
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
// If the word is found in the database
|
2024-09-14 21:51:31 +01:00
|
|
|
embed
|
|
|
|
.setDescription(
|
|
|
|
`**Definition:** ${cleanText(
|
|
|
|
result.definition || "No definition found"
|
|
|
|
)}\n` +
|
|
|
|
`**Part of Speech:** ${result.partOfSpeech || "Unknown"}\n` +
|
|
|
|
`**Attribution:** ${result.attributionText || "No attribution"}\n` +
|
|
|
|
`**Source Dictionary:** ${
|
|
|
|
result.sourceDictionary || "Unknown source"
|
|
|
|
}\n` +
|
|
|
|
`**Synonyms:** ${result.synonyms.join(", ") || "None found"}\n` +
|
|
|
|
`**Antonyms:** ${result.antonyms.join(", ") || "None found"}\n` +
|
|
|
|
`**Example:** ${result.exampleSentence || "No examples found"}`
|
|
|
|
)
|
|
|
|
.setURL(result.wordnikUrl || wordnikUrl)
|
|
|
|
.setFooter({
|
|
|
|
text: `Powered by Wordnik | Source: Database`,
|
|
|
|
iconURL: "https://wordnik.com/favicon.ico",
|
|
|
|
});
|
2024-09-08 00:34:08 +01:00
|
|
|
} else {
|
2024-09-14 21:51:31 +01:00
|
|
|
// Fetch the word information from Wordnik API
|
2024-09-08 00:34:08 +01:00
|
|
|
try {
|
2024-09-14 21:51:31 +01:00
|
|
|
// Fetch definitions
|
|
|
|
const definitionResponse = await axios.get(
|
|
|
|
`https://api.wordnik.com/v4/word.json/${word}/definitions`,
|
|
|
|
{
|
|
|
|
params: {
|
|
|
|
api_key: WORDNIK_API_KEY,
|
|
|
|
limit: 1,
|
|
|
|
includeRelated: true,
|
|
|
|
sourceDictionaries: "all",
|
|
|
|
useCanonical: true,
|
|
|
|
includeTags: false,
|
|
|
|
},
|
|
|
|
}
|
2024-09-08 00:34:08 +01:00
|
|
|
);
|
|
|
|
|
2024-09-14 21:51:31 +01:00
|
|
|
const definitionData = definitionResponse.data[0];
|
|
|
|
if (definitionData) {
|
|
|
|
const definition = cleanText(
|
|
|
|
definitionData.text || "No definition found"
|
|
|
|
);
|
|
|
|
const partOfSpeech = definitionData.partOfSpeech || "Unknown";
|
|
|
|
const attributionText =
|
|
|
|
definitionData.attributionText || "No attribution";
|
|
|
|
const sourceDictionary =
|
|
|
|
definitionData.sourceDictionary || "Unknown source";
|
|
|
|
const wordnikUrl = definitionData.wordnikUrl || wordnikUrl;
|
|
|
|
|
|
|
|
// Example sentence extraction (make sure `exampleUses` is correctly handled)
|
|
|
|
const exampleSentence =
|
|
|
|
(definitionData.exampleUses &&
|
|
|
|
definitionData.exampleUses[0] &&
|
|
|
|
definitionData.exampleUses[0].text) ||
|
|
|
|
"No examples found";
|
2024-09-08 00:34:08 +01:00
|
|
|
|
|
|
|
// Save the new word and definition in the database
|
2024-09-14 21:51:31 +01:00
|
|
|
await Word.create({
|
|
|
|
word,
|
|
|
|
definition,
|
|
|
|
partOfSpeech,
|
|
|
|
attributionText,
|
|
|
|
sourceDictionary,
|
|
|
|
exampleSentence,
|
|
|
|
wordnikUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
embed
|
|
|
|
.setDescription(
|
|
|
|
`**Definition:** ${definition}\n` +
|
|
|
|
`**Part of Speech:** ${partOfSpeech}\n` +
|
|
|
|
`**Attribution:** ${attributionText}\n` +
|
|
|
|
`**Source Dictionary:** ${sourceDictionary}\n` +
|
|
|
|
`**Example:** ${exampleSentence}`
|
|
|
|
)
|
|
|
|
.setURL(wordnikUrl); // Add a URL to the embed if available
|
2024-09-08 00:34:08 +01:00
|
|
|
|
2024-09-14 21:51:31 +01:00
|
|
|
embed.setFooter({
|
|
|
|
text: `Powered by Wordnik | Source: API`,
|
|
|
|
iconURL: "https://wordnik.com/favicon.ico",
|
2024-09-08 00:34:08 +01:00
|
|
|
});
|
|
|
|
} else {
|
2024-09-14 21:51:31 +01:00
|
|
|
embed.setDescription(
|
|
|
|
`Sorry, I couldn't find a definition for **${word}**.`
|
|
|
|
);
|
2024-09-08 00:34:08 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-09-14 21:51:31 +01:00
|
|
|
console.error("Error fetching Wordnik data:", error.message);
|
|
|
|
embed.setDescription(
|
|
|
|
`An error occurred while fetching the definition for **${word}**. ${error.message}`
|
|
|
|
);
|
2024-09-08 00:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-14 21:51:31 +01:00
|
|
|
|
|
|
|
await interaction.reply({
|
|
|
|
embeds: [embed],
|
|
|
|
ephemeral: isEphemeral,
|
|
|
|
});
|
2024-09-08 00:34:08 +01:00
|
|
|
},
|
|
|
|
};
|