You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Fixing the RichEmbed Setup

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 addField method is perfect for listing your two options clearly
  • Always use a valid color format (hex code, decimal number, or Discord's color constants)
Adding awaitMessages for User Choices

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 filter function prevents other users from interfering with the command
  • The awaitMessages options 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
Quick Checks for Common Issues

Since you mentioned being new to JavaScript, here are a few easy pitfalls to avoid:

  • If you use await anywhere in your message handler, make sure the handler function is marked async (like async message => { ... })
  • For Discord.js v14+, RichEmbed/MessageEmbed are replaced with EmbedBuilder from discord.js
  • Ensure your bot has the SendMessages and EmbedLinks permissions in the channel
  • If you're using Discord.js v14+, you'll need to enable the MessageContent intent in your Discord Developer Portal and in your bot code

内容的提问来源于stack exchange,提问作者Ignoratus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:32:37