mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-25 18:25:55 +00:00
commands: make urban dictionary save to db, and then serve from db if its stored
This commit is contained in:
parent
d6fc9d03cd
commit
a9e163c104
2 changed files with 67 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
||||||
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
|
const Definition = require("../../models/UrbanDictionary");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
|
@ -12,40 +13,62 @@ module.exports = {
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
),
|
),
|
||||||
async execute(interaction, client) {
|
async execute(interaction, client) {
|
||||||
const term = interaction.options.getString("term");
|
const term = interaction.options.getString("term").toLowerCase();
|
||||||
const url = `https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=${encodeURIComponent(
|
const guild = interaction.guild;
|
||||||
term
|
const serverName = guild.name;
|
||||||
)}`;
|
const serverIcon = guild.iconURL();
|
||||||
|
let source = "API";
|
||||||
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",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.request(options);
|
// Check if the term exists in the database
|
||||||
const data = response.data;
|
let definition = await Definition.findOne({ term });
|
||||||
|
|
||||||
if (data.list.length === 0) {
|
if (!definition) {
|
||||||
return await interaction.reply({
|
// If definition is not found, fetch from the API
|
||||||
content: "🚫 No definitions found.",
|
const url = `https://mashape-community-urban-dictionary.p.rapidapi.com/define?term=${encodeURIComponent(
|
||||||
ephemeral: true,
|
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];
|
// Create and send the embed message
|
||||||
const author = definition.author || "Unknown"; // Default if author info is missing
|
|
||||||
const guild = interaction.guild;
|
|
||||||
const serverName = guild.name;
|
|
||||||
const serverIcon = guild.iconURL();
|
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor("#3498db")
|
.setColor("#3498db")
|
||||||
.setTitle(`📚 Definition of: **${definition.word}**`)
|
.setTitle(`📚 Definition of: **${definition.term}**`)
|
||||||
.setDescription(definition.definition)
|
.setDescription(definition.definition)
|
||||||
.setThumbnail(
|
.setThumbnail(
|
||||||
"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Urban_Dictionary_Logo.svg/1200px-Urban_Dictionary_Logo.svg.png"
|
"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",
|
name: "✍️ Submitted by",
|
||||||
value: author,
|
value: definition.author || "Unknown",
|
||||||
inline: false,
|
inline: false,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.setFooter({
|
.setFooter({
|
||||||
text: `Powered by Urban Dictionary | ${serverName}`,
|
text: `${serverName} | Source: ${source}`,
|
||||||
iconURL: serverIcon,
|
iconURL: serverIcon,
|
||||||
})
|
})
|
||||||
.setTimestamp();
|
.setTimestamp();
|
||||||
|
|
||||||
await interaction.reply({ embeds: [embed] });
|
await interaction.reply({ embeds: [embed] });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching Urban Dictionary term:", error);
|
console.error("Error processing Urban Dictionary term:", error);
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content:
|
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,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
14
models/UrbanDictionary.js
Normal file
14
models/UrbanDictionary.js
Normal 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;
|
Loading…
Reference in a new issue