C# Discord Bot: Creating Separate Classes for Commands

Hey everyone! I’m new to C# and I’m trying to build a Discord bot. I’ve got the basic structure working, but I’m stuck on organizing my commands.

Right now, all my commands are in one class called PublicModule. I want to split them up into different classes for better organization. For example, I’d like to have a separate class for the say command.

Here’s what I’ve tried:

namespace MyBot.Modules.Public
{
    class SayCommands : ModuleBase
    {
        [Command("say")]
        public async Task EchoMessage([Remainder] string message)
        {
            await ReplyAsync(message);
        }
    }
}

But when I run the bot and try to use the command, I get an “Unknown command” error. What am I doing wrong? How can I properly set up multiple command classes?

I’ve heard I should “put the methods in the class,” but I’m not sure what that means or how to do it. Any help or explanations would be awesome! Thanks!

yo, had similar probs with my bot. make sure ur class is public, like this:

public class SayCommands : ModuleBase
{
// ur commands here
}

also check if ur registering modules right in startup. if ur still stuck, try debuggin or ask in discord.net discord server. they’re super helpful there!

I’ve faced a similar issue when organizing my Discord bot commands. The problem likely stems from how you’re registering the modules with your bot’s command handler. Here’s what worked for me:

Make sure you’re using the ModuleBase<SocketCommandContext> instead of just ModuleBase. Also, ensure you’re registering all your command modules in your bot’s startup method. It should look something like this:

await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);

This line tells the bot to look for all classes that inherit from ModuleBase in your assembly. If you’ve done this and it’s still not working, double-check that your SayCommands class is public. Private classes won’t be discovered by the module registration process.

Lastly, verify that your bot has the necessary permissions in the Discord server to read messages and send responses. Hope this helps!

As someone who’s built a few Discord bots in C#, I can tell you that organizing commands into separate classes is definitely the way to go. It keeps things clean and manageable as your bot grows.

Here’s a tip that worked for me: make sure your command classes are public. The Discord.NET library needs to be able to see them to register the commands properly. Also, don’t forget to add the [Group] attribute to your class if you want to group related commands together.

Another thing to watch out for is namespace issues. Make sure your command classes are in a namespace that’s being scanned by your bot’s startup code. I once spent hours debugging only to realize my new command class was in a namespace I’d forgotten to include!

Lastly, if you’re still having trouble, try adding some debug logging to your command handler. It can help pinpoint where things are going wrong. Good luck with your bot!