Custom Module Bases
You can create custom module bases to add methods and properties to your commands, application commands and interactions. You can also apply precondition attributes on them.
Example
using NetCord;
using NetCord.Services.Commands;
namespace MyBot;
public abstract class CustomCommandModule : CommandModule<CommandContext>
{
public Color GetUserColor(GuildUser user)
{
return (user.GetRoles(Context.Guild!)
.OrderByDescending(r => r.Position)
.FirstOrDefault(r => r.Color != default)?.Color).GetValueOrDefault();
}
}
Example Usage
using NetCord;
using NetCord.Services;
using NetCord.Services.Commands;
namespace MyBot;
public class ExampleModule : CustomCommandModule
{
[RequireContext<CommandContext>(RequiredContext.Guild)]
[Command("color")]
public string Color(GuildUser? user = null)
{
user ??= (GuildUser)Context.User;
var color = GetUserColor(user);
return $"#{color.RawValue:X6}";
}
}
Custom Contexts
You can create custom contexts to extend built-in contexts.
Example
using NetCord;
using NetCord.Gateway;
using NetCord.Services.Commands;
namespace MyBot;
public class CustomCommandContext(Message message, GatewayClient client) : CommandContext(message, client)
{
public GuildUser BotGuildUser => Guild!.Users[Client.Id];
}
Example Usage
using NetCord.Services;
using NetCord.Services.Commands;
namespace MyBot;
public class ExampleModule : CommandModule<CustomCommandContext>
{
[RequireContext<CustomCommandContext>(RequiredContext.Guild)]
[Command("botname")]
public string BotName()
{
var user = Context.BotGuildUser;
return user.Nickname ?? user.Username;
}
}
Note
Your service needs to have CustomCommandContext
as a generic parameter to be able to load commands, application commands and interactions with the context.