使用discord.js-commando开发的Purge命令为何无法处理两位数消息量?
Hey there! Let's break down why your purge command only deletes 9 messages when you ask for 99, and fix it up properly.
What's Causing the Problem?
The core issue is how you're handling the args parameter in your run method. In discord.js-commando, if you don't explicitly define an args configuration for your command, the args value passed to run is a single string of all text after the command name—not an array.
So when you run !purge 99, args becomes "99" instead of ["99"]. When you access args[0], you're grabbing the first character of that string: "9". That's why only 9 messages get deleted!
How to Fix It
Let's rewrite the command with proper args parsing, cleaner flow, and better permission handling:
const Commando = require("discord.js-commando"); class PurgeCommand extends Commando.Command { constructor(client) { super(client, { name: "purge", group: "moderation", memberName: "purge", description: "Deletes a specified amount of messages.", examples: ["purge 50"], // Define args so Commando parses them correctly args: [ { key: "amount", prompt: "How many messages would you like to delete?", type: "integer", min: 1, max: 99 // Discord's bulkDelete limit is 100, cap at 99 to avoid issues } ], // Enforce permission checks directly in command config userPermissions: ["MANAGE_MESSAGES"], clientPermissions: ["MANAGE_MESSAGES"] }); } async run(message, { amount }) { try { // Delete the user's command message first await message.delete(); // Bulk delete messages: +1 to include the command message we just deleted // The second "true" skips messages older than 14 days (Discord's restriction) const deletedMessages = await message.channel.bulkDelete(amount + 1, true); // Send a temporary confirmation message const confirmation = await message.channel.send( `Successfully purged **${deletedMessages.size - 1}** messages.` ); // Auto-delete confirmation after 3 seconds to keep the channel clean setTimeout(() => confirmation.delete(), 3000); } catch (error) { console.error(error); message.reply( "There was an error trying to purge messages! Make sure I have permission to manage messages and that the messages aren't older than 14 days." ); } } } module.exports = PurgeCommand;
Key Improvements Explained:
- Proper Args Parsing: We added an
argsconfiguration that tells Commando to parse a single integer parameter. This gives us a cleanamountinteger directly in therunmethod—no more messy string indexing or parsing. - Built-In Permission Checks: Using
userPermissionsandclientPermissionsin the command config is the standard Commando way to handle permissions. It automatically blocks users/bots without the right access, so we don't need manualifchecks. - Simplified Async Flow: Removed unnecessary nested async functions that complicated the code. We use
try/catchto handle errors gracefully, like if messages can't be deleted. - Accurate Deletion Count: We add 1 to the purge amount to include the user's command message, then subtract 1 from the final count to exclude that message from the confirmation.
- Cleaner Channel: The confirmation message auto-deletes after 3 seconds to avoid cluttering the channel.
Why Your Original Code Had Other Issues:
- Your permission check only verified the bot's permissions, not the user's—so any user could run the command even without moderation rights.
- Nested async functions created unnecessary complexity and could lead to unexpected context issues with the message object.
- You didn't handle cases where messages can't be deleted (like older than 14 days), which would cause silent failures.
Give this modified command a try, and it should work exactly as expected when you input 99 or any other valid number between 1 and 99!
内容的提问来源于stack exchange,提问作者Pruina Tempestatis

