commands: make urban dictionary save to db, and then serve from db if its stored

This commit is contained in:
Ayden Jahola 2024-09-08 01:22:22 +01:00
parent d6fc9d03cd
commit a9e163c104
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742
2 changed files with 67 additions and 30 deletions

View file

@ -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,7 +13,18 @@ module.exports = {
.setRequired(true)
),
async execute(interaction, client) {
const term = interaction.options.getString("term");
const term = interaction.options.getString("term").toLowerCase();
const guild = interaction.guild;
const serverName = guild.name;
const serverIcon = guild.iconURL();
let source = "API";
try {
// Check if the term exists in the database
let definition = await Definition.findOne({ term });
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
)}`;
@ -21,12 +33,12 @@ module.exports = {
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",
"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",
},
};
try {
const response = await axios.request(options);
const data = response.data;
@ -37,15 +49,26 @@ module.exports = {
});
}
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();
// 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";
}
// 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,
});
}

14
models/UrbanDictionary.js Normal file
View file

@ -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;