diff --git a/commands/general/urbanDictionary.js b/commands/general/urbanDictionary.js index c5ff471..ab9455c 100644 --- a/commands/general/urbanDictionary.js +++ b/commands/general/urbanDictionary.js @@ -1,5 +1,6 @@ const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const axios = require("axios"); +const Definition = require("../../models/UrbanDictionary"); module.exports = { data: new SlashCommandBuilder() @@ -12,40 +13,62 @@ module.exports = { .setRequired(true) ), async execute(interaction, client) { - const term = interaction.options.getString("term"); - const url = `https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=${encodeURIComponent( - term - )}`; - - const options = { - method: "GET", - url: url, - headers: { - "X-RapidAPI-Key": "272f95b62amsh3dddd28f7289395p1bd2a9jsna5ee0dd5d9ea", // public API key please dont shout at me, https://rapidapi.com/community/api/urban-dictionary/playground/53aa4f68e4b07e1f4ebeb2b0 - "X-RapidAPI-Host": "mashape-community-urban-dictionary.p.rapidapi.com", - }, - }; + const term = interaction.options.getString("term").toLowerCase(); + const guild = interaction.guild; + const serverName = guild.name; + const serverIcon = guild.iconURL(); + let source = "API"; try { - const response = await axios.request(options); - const data = response.data; + // Check if the term exists in the database + let definition = await Definition.findOne({ term }); - if (data.list.length === 0) { - return await interaction.reply({ - content: "🚫 No definitions found.", - ephemeral: true, + if (!definition) { + // If definition is not found, fetch from the API + const url = `https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=${encodeURIComponent( + term + )}`; + + const options = { + method: "GET", + url: url, + headers: { + "X-RapidAPI-Key": process.env.RAPIDAPI_KEY, // everything started shouting at me so lets just save the key in the .env file + "X-RapidAPI-Host": + "mashape-community-urban-dictionary.p.rapidapi.com", + }, + }; + + const response = await axios.request(options); + const data = response.data; + + if (data.list.length === 0) { + return await interaction.reply({ + content: "🚫 No definitions found.", + ephemeral: true, + }); + } + + // Save the new definition to the database + definition = new Definition({ + term, + definition: data.list[0].definition, + example: data.list[0].example || "No example provided", + author: data.list[0].author || "Unknown", + thumbs_up: data.list[0].thumbs_up || 0, + thumbs_down: data.list[0].thumbs_down || 0, }); + + await definition.save(); + } else { + // If found in the database, set source to "Database" + source = "Database"; } - const definition = data.list[0]; - const author = definition.author || "Unknown"; // Default if author info is missing - const guild = interaction.guild; - const serverName = guild.name; - const serverIcon = guild.iconURL(); - + // Create and send the embed message const embed = new EmbedBuilder() .setColor("#3498db") - .setTitle(`📚 Definition of: **${definition.word}**`) + .setTitle(`📚 Definition of: **${definition.term}**`) .setDescription(definition.definition) .setThumbnail( "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Urban_Dictionary_Logo.svg/1200px-Urban_Dictionary_Logo.svg.png" @@ -63,22 +86,22 @@ module.exports = { }, { name: "✍️ Submitted by", - value: author, + value: definition.author || "Unknown", inline: false, } ) .setFooter({ - text: `Powered by Urban Dictionary | ${serverName}`, + text: `${serverName} | Source: ${source}`, iconURL: serverIcon, }) .setTimestamp(); await interaction.reply({ embeds: [embed] }); } catch (error) { - console.error("Error fetching Urban Dictionary term:", error); + console.error("Error processing Urban Dictionary term:", error); await interaction.reply({ content: - "⚠️ There was an error while fetching the term. Please try again later.", + "⚠️ There was an error while processing the term. Please try again later.", ephemeral: true, }); } diff --git a/models/UrbanDictionary.js b/models/UrbanDictionary.js new file mode 100644 index 0000000..55d0676 --- /dev/null +++ b/models/UrbanDictionary.js @@ -0,0 +1,14 @@ +const mongoose = require("mongoose"); + +const definitionSchema = new mongoose.Schema({ + term: { type: String, required: true, unique: true, trim: true }, + definition: { type: String, required: true }, + example: { type: String, default: "No example provided" }, + author: { type: String, default: "Unknown" }, + thumbs_up: { type: Number, default: 0 }, + thumbs_down: { type: Number, default: 0 }, +}); + +const Definition = mongoose.model("Definition", definitionSchema); + +module.exports = Definition;