Discord Bot开发:RichEmbeds与awaitMessages功能故障求助
Hey there! No worries at all—we all start somewhere with JavaScript and Discord bots. Let's break down how to fix your !records command, focusing on getting that RichEmbed working and setting up the awaitMessages functionality for user choices.
First, let's finish building your embed to clearly display the two optional options. I'll fill in the missing parts and add notes for version compatibility:
// Make sure your message handler is marked as async if you plan to use await later client.on('message', async message => { if (message.content.startsWith(config.prefix + 'records')) { // Important note: If you're using Discord.js v12+, RichEmbed is deprecated—use MessageEmbed instead const EmbedConstructor = Discord.RichEmbed || Discord.MessageEmbed; const embed = new EmbedConstructor() .setTitle('D E I C I D E Records') .setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png') .setColor('#e74c3c') // Use a hex code or Discord's built-in color constants like Discord.Colors.RED .setDescription('Pick an option by typing the corresponding number:') .addField('1. View All Records', 'See every recorded entry in the database') .addField('2. Search Records', 'Look up a specific entry by keyword or ID'); // Send the embed to the channel and store the sent message (we'll use this later if needed) const sentEmbed = await message.channel.send(embed); } });
Key things to note here:
- I added a fallback for the embed constructor so it works across older and newer Discord.js versions
- The
addFieldmethod is perfect for listing your two options clearly - Always use a valid color format (hex code, decimal number, or Discord's color constants)
Now let's add the logic to wait for the user's selection after sending the embed. This will ensure only the command runner can respond, and we'll handle timeouts if they don't reply:
// Add this right after sending the embed (inside the !records block) const filter = response => { // Only accept messages from the user who ran the command, and valid option numbers return response.author.id === message.author.id && ['1', '2'].includes(response.content.trim()); }; // Wait for 1 valid response, with a 30-second timeout message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] }) .then(collected => { const userChoice = collected.first().content.trim(); // Handle each option if (userChoice === '1') { message.channel.send('Loading all records...'); // Add your code here to fetch and display all records (e.g., from a database) } else if (userChoice === '2') { message.channel.send('Please enter your search term:'); // You can add another awaitMessages block here to capture the search term } }) .catch(error => { // If the user doesn't reply in time, send a timeout message message.channel.send('Sorry, you took too long! Run `!records` again to try.'); });
Critical tips for this part:
- The
filterfunction prevents other users from interfering with the command - The
awaitMessagesoptions set a clear limit on how many responses we accept and how long we wait - Always wrap this in a
.catch()block to handle timeouts or errors gracefully
Since you mentioned being new to JavaScript, here are a few easy pitfalls to avoid:
- If you use
awaitanywhere in your message handler, make sure the handler function is markedasync(likeasync message => { ... }) - For Discord.js v14+, RichEmbed/MessageEmbed are replaced with
EmbedBuilderfromdiscord.js - Ensure your bot has the
SendMessagesandEmbedLinkspermissions in the channel - If you're using Discord.js v14+, you'll need to enable the
MessageContentintent in your Discord Developer Portal and in your bot code
内容的提问来源于stack exchange,提问作者Ignoratus

