Hey everyone! I’m working on a Discord bot project in C# and having trouble with a bulk delete feature. I want users to type something like !clear 5 and have the bot remove the last 5 messages from the channel.
The problem is that this code doesn’t seem to work at all. Previously I had a version that would always delete a fixed number of messages (like 10) no matter what number I specified in the command.
I encountered a similar problem when building my moderation bot last year. The issue is likely with how you’re handling the parameter conversion and error checking. Your current code will crash if someone types !clear abc or just !clear without a number. Try wrapping the conversion in a try-catch block and validate the input first. Also make sure you’re downloading the correct number of messages - you might want to download messageCount + 1 since the command message itself counts as one of the messages in the channel. The Discord API has some quirks with bulk deletion too, messages older than 14 days can’t be bulk deleted so you’ll need to handle that separately with individual deletions.
looks like your parameter parsing might be the issue here. try adding .Parameter("messageCount", ParameterType.Required) instead of just .Parameter("messageCount"). also double check that context.GetArg("messageCount") isnt returning null or empty string before converting to int. maybe add some debug logging to see whats actually being passed?
I’ve dealt with this exact same frustration before. The main culprit is probably that you’re using an older Discord.NET version based on your syntax. The DownloadMessages and DeleteMessages methods have different behavior depending on the library version you’re running. In newer versions, you’d typically use GetMessagesAsync instead of DownloadMessages. Another thing to consider is that your parameter might not be getting parsed correctly - I’d suggest adding some console output to verify what value you’re actually getting from context.GetArg("messageCount") before the conversion. When I had similar issues, it turned out the parameter was coming through as null even though I thought I was passing it correctly. You might also want to add a limit check since Discord has restrictions on bulk operations - anything over 100 messages tends to cause problems.