Cage command animal details
Créé le : 18 janvier 2025
Créé le : 18 janvier 2025
Shifting display style
I'm changing the "cage" command to show animal emojis, enhancing focus. Adding a new command for specific animal details maintains a cleaner interface.
Crafting the command
I’m figuring out how to create a flexible command for animal details, supporting both slash and text-based inputs. This shows the necessity of clear, adaptable coding practices.
Enhancing command flexibility
I'm refining the "cage" command to list animals by name or index, while also detailing how to use it with slash commands. The aim is to simplify and improve the user experience.
Crafting the command
I’m developing the "cageCommand" to handle specific animal details or a general animal list. Integrating emojis and stats, I’m ensuring smooth interaction with ephemeral messages.
Thought about animal details feature for 5 seconds
Below is an example of how you can restructure your cage command to provide two different views:
Feel free to adapt the naming of commands, arguments, etc. to match your existing bot structure (including slash commands if you wish). This example is for both text-based usage (like !cage deer
) and potential slash commands (/cage deer
).
import User from '../../../models/Hunt.js'; import { EmbedBuilder } from 'discord.js'; // A helper function for sending or editing replies async function handleMessage(context, data) { const isInteraction = !!context.isCommand; // Distinguishes slash command from a normal message if (isInteraction) { // If not already deferred, defer it. if (!context.deferred) { await context.deferReply(); } return context.editReply(data); } else { // For normal text-based usage return context.channel.send(data); } } /** * Show general cage info: display only the emojis of the user's animals. */ async function showCageOverview(context, user) { if (!user.hunt.animals || user.hunt.animals.length === 0) { return handleMessage(context, { content: `Your cage is currently empty! Try using the **/hunt** command (or your hunt command) first.` }); } // Just display the emojis of each animal const animalEmojis = user.hunt.animals .map((animal) => animal.emoji) .join(' '); const embed = new EmbedBuilder() .setTitle(`${user.username}'𝕤 𝔸𝕟𝕚𝕞𝕒𝕝 ℂ𝕒𝕘𝕖 🕷️`) .setDescription(`**Your Animals**\n\n${animalEmojis}`) .setFooter({ text: `Tip: use "cage <animalName>" to see more details about a specific animal.` }); return handleMessage(context, { embeds: [embed] }); } /** * Show details for one specific animal (by name or partial name, if you want). */ async function showAnimalDetail(context, user, animalName) { if (!user.hunt.animals || user.hunt.animals.length === 0) { return handleMessage(context, { content: `Your cage is empty. Nothing to show!` }); } // Find the animal by name (case-insensitive). // If you store a unique 'id' for each animal, you could match that instead. const foundAnimal = user.hunt.animals.find((animal) => animal.name.toLowerCase() === animalName.toLowerCase() ); if (!foundAnimal) { return handleMessage(context, { content: `Couldn't find an animal with the name: **${animalName}**.` }); } // Build an embed for that specific animal // Include stats or additional fields for an enhanced experience const embed = new EmbedBuilder() .setTitle(`Details of ${foundAnimal.emoji} ${foundAnimal.name}`) .setDescription( [ `**Level**: ${foundAnimal.level}`, `**EXP**: ${foundAnimal.exp}`, `**HP**: ${foundAnimal.hp}`, `**Attack**: ${foundAnimal.attack}` ].join('\n') ) .setFooter({ text: `Use "cage" with no arguments to see all your animals.` }); return handleMessage(context, { embeds: [embed] }); } /** * Main command function: * - If no argument -> show cage overview (emojis only). * - If an argument (like 'deer') -> show that animal's details. */ export async function cageCommand(context) { try { // For slash commands, you might extract arguments differently. // For text commands, we can assume arguments are in context.args. const args = context.args || []; const userId = context.user?.id || context.author?.id; let user = await User.findOne({ discordId: userId }); if (!user) { return handleMessage(context, { content: `You have no hunting profile yet. Go hunt some animals first!` }); } // Check if an argument is provided if (args.length > 0) { // Combine args if you allow multi-word names, or just take args[0] if single-word const animalName = args.join(' '); return showAnimalDetail(context, user, animalName); } else { // No argument: show the overview return showCageOverview(context, user); } } catch (error) { console.error(error); return handleMessage(context, { content: `**Error**: ${error.message}` }); } } export default { name: "cage", description: "View your animal collection in the cage or details about one animal.", aliases: ["animals", "animalcage"], args: "<animalName> (optional)", example: [ "cage", "cage deer", "animals dragon" ], related: ["hunt", "profile"], cooldown: 10000, // 10 seconds cooldown category: "🐯 Hunt", execute: async (args, context) => { // We pass in 'args' so that the command can parse them context.args = args; try { await cageCommand(context); } catch (e) { console.error(e); } } };
If the user just types cage
(no arguments):
If the user types cage <animalName>
, for example cage deer
:
cage [animalName]
if you want to use interactions. For example, a slash command with an autocomplete field listing the user’s animals.This pattern will keep the main cage list concise (just emojis) and let the user dig deeper only when they want to see a particular animal’s stats. You can further customize the visuals, add more stats, or integrate advanced features like interactive buttons to cycle through each animal.