I'm coming from Nextcord where we have cogs, but here I'm confused. What if I want one command per class? Do I have to register them all manually?
Here's what I'm doing now: I have a Bot class that has:
var slash = discord.UseSlashCommands(); slash.RegisterCommands<DiscordCommand>();
Then in my DiscordCommand
class I have a few Task
s with \[SlashCommand\]
attributes.
Ideally I would want to make a new class for every command, and have that class inherit from a BaseCommand
class that auto registers itself somehow. Is there a way to do that?
Thanks in advance.
There is a relatively easy way of doing this so that you don't need 100 lines of slash.RegisterCommand<t>()
in your code. In this example we will assume you have the word "Command" in all class names that contain a command, and that all of your commands are in the namespace Project.Commands
.
Instead of doing this:
var slash = discord.UseSlashCommands();
slash.RegisterCommands<FirstCommand>();
slash.RegisterCommands<SecondCommand>();
slash.RegisterCommands<ThirdCommand>();
We are going to loop through all the classes of the namespace Project.Commands
and register these through the loop.
var slash = discord.UseSlashCommands();
var types = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(t => t.Namespace.StartsWith("Project.Commands")) //Limit our namespace to Project.Commands
.Where(s => s.Name.Contains("Command")); //Limit the types to those named with the word Command
foreach (var t in types)
{
slash.RegisterCommands(t);
}