JavaScript开发Discord Bot:.completed命令遇command未定义错误求助
ReferenceError: command is not defined and Other Issues in Your Discord Bot .completed Command Hey there! Let's walk through fixing your .completed command—you've got a handful of small issues causing the ReferenceError and other potential bugs. Let's break them down one by one.
1. The command and prefix Undefined Error
Your code checks if (command === ${prefix}completed) but neither command nor prefix are declared in this module. Since this is a command module exported with name: "completed", your main bot file should already be routing the .completed command to this function, so this check is unnecessary. If you still want to keep a prefix guard, define the prefix first and adjust the logic:
const prefix = "."; // Optional: Add a command check if needed const command = message.content.slice(prefix.length).split(' ')[0]; if (command !== "completed") return;
2. Outdated RichEmbed Syntax
In Discord.js v12 and above, RichEmbed was renamed to MessageEmbed. Update that line to avoid compatibility issues:
var botEmbed = new discord.MessageEmbed()
3. Invalid Mention Access Methods
message.mentions.users doesn't have .second(), .third(), etc. Use the array() method to get a list of mentioned users, then access them by index. Add fallbacks in case not enough users are mentioned:
const mentionedUsers = message.mentions.users.array(); // Use in fields like this: .addField("Booster 1", mentionedUsers[0]?.tag || "No user mentioned") .addField("Booster 2", mentionedUsers[1]?.tag || "No user mentioned")
4. displayAvatarURL is a Method
You forgot to call the method—add parentheses to retrieve the actual avatar URL:
var userIcon = message.member.user.displayAvatarURL();
5. Incorrect addField Usage
Each addField requires two arguments: the field name and value. Your current code only passes one, which will throw errors. Fix each field like this:
.addField("Pot of Gold", `${goldPot}k`) .addField("Total Booster Cut", `${totBooCut.toFixed(2)}k`) // Use toFixed for clean decimal values
6. Broken Input Validation Logic
The check if (!words.length > 7) is logically incorrect. Adjust it to ensure users provide the right number of arguments (assuming your expected format is .completed @booster1 @booster2 @booster3 @booster4 <gold> <location> @advertiser):
const words = message.content.slice(prefix.length).split(' '); // Check for too few arguments if (words.length < 8) { return message.channel.send(`You're missing arguments, ${message.author}! Use: \`${prefix}completed @booster1 @booster2 @booster3 @booster4 <gold amount> <location> @advertiser\``); } // Check for too many arguments if (words.length > 8) { return message.channel.send(`Too many arguments, ${message.author}! Stick to the format above.`); }
Full Fixed Code
Here's the complete corrected code incorporating all these fixes:
const discord = require("discord.js"); const prefix = "."; module.exports.run = async (bot, message, args) => { const mentionedUsers = message.mentions.users.array(); const words = message.content.slice(prefix.length).split(' '); // Input validation if (words.length < 8) { return message.channel.send(`You're missing arguments, ${message.author}! Use the format: \`${prefix}completed @booster1 @booster2 @booster3 @booster4 <gold amount> <location> @advertiser\``); } if (words.length > 8) { return message.channel.send(`You provided too many arguments, ${message.author}! Stick to the format above.`); } // Extract and validate values const goldPot = words[5]; const goldLocation = words[6]; const goldPotInt = parseInt(goldPot); if (isNaN(goldPotInt)) { return message.channel.send(`Please enter a valid number for the gold pot, ${message.author}!`); } // Calculate cuts const boosterCut = goldPotInt * 0.178; const totBooCut = boosterCut * 4; const advertCut = goldPotInt * 0.173; const guildCut = goldPotInt * 0.115; // Build embed const userIcon = message.member.user.displayAvatarURL(); const botEmbed = new discord.MessageEmbed() .setDescription("M+ Completed") .setColor("#8e12db") .setThumbnail(userIcon) .addField("Booster 1", mentionedUsers[0]?.tag || "Not specified") .addField("Booster 2", mentionedUsers[1]?.tag || "Not specified") .addField("Booster 3", mentionedUsers[2]?.tag || "Not specified") .addField("Booster 4", mentionedUsers[3]?.tag || "Not specified") .addField("Pot of Gold", `${goldPot}k`) .addField("Total Booster Cut", `${totBooCut.toFixed(2)}k`) .addField("Individual Booster Cut", `${boosterCut.toFixed(2)}k`) .addField("Advertiser Cut", `${advertCut.toFixed(2)}k`) .addField("Guild Cut", `${guildCut.toFixed(2)}k`) .addField("Location of Gold", goldLocation) .addField("Advertiser", mentionedUsers[4]?.tag || "Not specified"); // Send embed (adjust for your Discord.js version) return message.channel.send({ embeds: [botEmbed] }); // For v13+; use message.channel.send(botEmbed) for v12 }; module.exports.help = { name: "completed" };
Quick Version Note
- If you're on Discord.js v12, remove the
embedsarray wrapper and sendmessage.channel.send(botEmbed)directly.
内容的提问来源于stack exchange,提问作者Askelaboeska

