mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-22 08:45:55 +00:00
games: add word association
This commit is contained in:
parent
91c38d0c59
commit
5384e1f684
3 changed files with 122 additions and 2 deletions
|
@ -1,6 +1,6 @@
|
||||||
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const ScrambledWord = require("../../models/ScrambledWord"); // Your database model for scrambled words
|
const ScrambledWord = require("../../models/ScrambledWord");
|
||||||
|
|
||||||
const ACTIVE_GAMES = new Set(); // Track users with ongoing games
|
const ACTIVE_GAMES = new Set(); // Track users with ongoing games
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ const createScrambleEmbed = (scrambledWord, guild, timeLimit) => {
|
||||||
.setTimestamp()
|
.setTimestamp()
|
||||||
.setFooter({
|
.setFooter({
|
||||||
text: `${guild.name} | Answer within ${timeLimit / 1000} seconds`,
|
text: `${guild.name} | Answer within ${timeLimit / 1000} seconds`,
|
||||||
iconURL: guild.iconURL(), // Server avatar
|
iconURL: guild.iconURL(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
111
commands/games/wordassociation.js
Normal file
111
commands/games/wordassociation.js
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
||||||
|
const axios = require("axios");
|
||||||
|
const WordAssociation = require("../../models/WordAssociation");
|
||||||
|
|
||||||
|
const MAX_FIELDS = 25; // Maximum number of fields in an embed
|
||||||
|
const CACHE_EXPIRY = 30 * 24 * 60 * 60 * 1000; // 1 month
|
||||||
|
|
||||||
|
const fetchAssociatedWordsFromAPI = async (word) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(
|
||||||
|
`https://api.datamuse.com/words?rel_trg=${word}`
|
||||||
|
);
|
||||||
|
return response.data.slice(0, MAX_FIELDS); // Limit the number of associated words
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching associated words from API:", error);
|
||||||
|
throw new Error("Error fetching associated words");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getAssociatedWords = async (word) => {
|
||||||
|
try {
|
||||||
|
let wordAssociation = await WordAssociation.findOne({ word });
|
||||||
|
|
||||||
|
if (
|
||||||
|
wordAssociation &&
|
||||||
|
Date.now() - new Date(wordAssociation.last_updated).getTime() <
|
||||||
|
CACHE_EXPIRY
|
||||||
|
) {
|
||||||
|
return wordAssociation.associatedWords;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch from API if not cached or cache expired
|
||||||
|
const associatedWords = await fetchAssociatedWordsFromAPI(word);
|
||||||
|
|
||||||
|
// Save or update cache
|
||||||
|
if (wordAssociation) {
|
||||||
|
wordAssociation.associatedWords = associatedWords.map(
|
||||||
|
(entry) => entry.word
|
||||||
|
);
|
||||||
|
wordAssociation.last_updated = new Date();
|
||||||
|
await wordAssociation.save();
|
||||||
|
} else {
|
||||||
|
await WordAssociation.create({
|
||||||
|
word,
|
||||||
|
associatedWords: associatedWords.map((entry) => entry.word),
|
||||||
|
last_updated: new Date(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return associatedWords.map((entry) => entry.word);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error getting associated words:", error);
|
||||||
|
throw new Error("Error retrieving associated words");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const createWordAssociationEmbed = (word, associatedWords) => {
|
||||||
|
return new EmbedBuilder()
|
||||||
|
.setColor("#0099ff")
|
||||||
|
.setTitle("Word Association")
|
||||||
|
.setDescription(`Words associated with **${word}**:`)
|
||||||
|
.addFields(
|
||||||
|
associatedWords.map((word, index) => ({
|
||||||
|
name: `Option ${index + 1}`,
|
||||||
|
value: word,
|
||||||
|
inline: true,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
.setTimestamp();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("wordassoc")
|
||||||
|
.setDescription("Find words associated with a given word")
|
||||||
|
.addStringOption((option) =>
|
||||||
|
option
|
||||||
|
.setName("word")
|
||||||
|
.setDescription("The word to find associations for")
|
||||||
|
.setRequired(true)
|
||||||
|
),
|
||||||
|
|
||||||
|
async execute(interaction) {
|
||||||
|
const word = interaction.options.getString("word");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const associatedWords = await getAssociatedWords(word);
|
||||||
|
|
||||||
|
if (associatedWords.length === 0) {
|
||||||
|
return interaction.reply({
|
||||||
|
content: `No associated words found for **${word}**.`,
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const wordAssociationEmbed = createWordAssociationEmbed(
|
||||||
|
word,
|
||||||
|
associatedWords
|
||||||
|
);
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [wordAssociationEmbed] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error executing word association command:", error);
|
||||||
|
await interaction.reply({
|
||||||
|
content:
|
||||||
|
"There was an error fetching associated words. Please try again later.",
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
9
models/WordAssociation.js
Normal file
9
models/WordAssociation.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const wordAssociationSchema = new mongoose.Schema({
|
||||||
|
word: { type: String, required: true },
|
||||||
|
associatedWords: [{ type: String }],
|
||||||
|
last_updated: { type: Date, default: Date.now },
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("WordAssociation", wordAssociationSchema);
|
Loading…
Reference in a new issue