Skip to content

Commit

Permalink
Use ApplicationCommandContext where it's possible (#84)
Browse files Browse the repository at this point in the history
* Use ApplicationCommandContext where it's possible

* Shorten the name for IDataProvider variables

* Improve wording

* Use `ISomeService` instead of `IDataProvider`

* Move the multiple services guide to the bottom and improve its wording
  • Loading branch information
KubaZ2 authored Dec 29, 2024
1 parent b5ce10a commit 4276b54
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

builder.Services
.AddDiscordRest()
.AddApplicationCommands<SlashCommandInteraction, HttpSlashCommandContext>();
.AddApplicationCommands<ApplicationCommandInteraction, HttpApplicationCommandContext>();

var app = builder.Build();

Expand Down
6 changes: 3 additions & 3 deletions Documentation/guides/basic-concepts/Voice/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
Intents = GatewayIntents.Guilds | GatewayIntents.GuildVoiceStates,
});

ApplicationCommandService<SlashCommandContext> applicationCommandService = new();
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new();
applicationCommandService.AddModules(typeof(Program).Assembly);

client.InteractionCreate += async interaction =>
{
if (interaction is not SlashCommandInteraction slashCommandInteraction)
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;

var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client));
var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client));

if (result is not IFailResult failResult)
return;
Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/basic-concepts/Voice/VoiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace MyBot;

public class VoiceModule : ApplicationCommandModule<SlashCommandContext>
public class VoiceModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("play", "Plays music", Contexts = [InteractionContextType.Guild])]
public async Task PlayAsync(string track)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace MyBot;

public class DataAutocompleteProvider(IDataProvider dataProvider) : IAutocompleteProvider<AutocompleteInteractionContext>
public class DataAutocompleteProvider(ISomeService someService) : IAutocompleteProvider<AutocompleteInteractionContext>
{
public ValueTask<IEnumerable<ApplicationCommandOptionChoiceProperties>?> GetChoicesAsync(
ApplicationCommandInteractionDataOption option,
AutocompleteInteractionContext context)
{
var input = option.Value!;
var data = dataProvider.GetData();
var data = someService.GetSomeData();

var result = data.Where(d => d.Contains(input))
.Take(25)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace MyBot;

public class DataModule(IDataProvider dataProvider) : CommandModule<CommandContext>
public class DataModule(ISomeService someService) : CommandModule<CommandContext>
{
[Command("data")]
public string Data(int count) => string.Join(' ', dataProvider.GetData().Take(count));
public string Data(int count) => string.Join(' ', someService.GetSomeData().Take(count));
}
11 changes: 0 additions & 11 deletions Documentation/guides/services/DependencyInjection/IDataProvider.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Documentation/guides/services/DependencyInjection/ISomeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MyBot;

public interface ISomeService
{
public IReadOnlyList<string> GetSomeData();
}

public class SomeService : ISomeService
{
public IReadOnlyList<string> GetSomeData() => ["hello", "world"];
}
9 changes: 5 additions & 4 deletions Documentation/guides/services/DependencyInjection/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddSingleton<IDataProvider, DataProvider>()
.AddSingleton<ISomeService, SomeService>()
.AddDiscordGateway(o => o.Intents = GatewayIntents.GuildMessages | GatewayIntents.DirectMessages | GatewayIntents.MessageContent)
.AddCommands<CommandContext>()
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>();
.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext>();

var host = builder.Build();

Expand All @@ -27,8 +27,9 @@
host.AddSlashCommand(
name: "data",
description: "Shows the data!",
(IDataProvider dataProvider, SlashCommandContext context, int count) => string.Join(' ', dataProvider.GetData()
.Take(count)));
(ISomeService someService, ApplicationCommandContext context, int count) => string.Join(' ',
someService.GetSomeData()
.Take(count)));

host.UseGatewayEventHandlers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace MyBot;

public class HelloModule : ApplicationCommandModule<SlashCommandContext>
public class HelloModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("hello", "Say hello!")]
public static string Hello([MustContain<SlashCommandContext>("hello")] string text) => text;
public static string Hello([MustContain<ApplicationCommandContext>("hello")] string text) => text;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

builder.Services
.AddDiscordGateway()
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>()
.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext>()
.AddCommands<CommandContext>();

var host = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

builder.Services
.AddDiscordGateway()
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>()
.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext>()
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
.AddCommands<CommandContext>();

Expand All @@ -25,7 +25,7 @@
host.AddSlashCommand(
name: "hi",
description: "Hi!",
[RequireAnimatedAvatar<SlashCommandContext>] () => "Hi! You can use this command because your avatar is animated!");
[RequireAnimatedAvatar<ApplicationCommandContext>] () => "Hi! You can use this command because your avatar is animated!");

host.AddModules(typeof(Program).Assembly)
.UseGatewayEventHandlers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MyBot;

public class AnimalModule : ApplicationCommandModule<SlashCommandContext>
public class AnimalModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("animal", "Sends the animal you selected")]
public static string Animal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace MyBot;

[SlashCommand("permissions", "Manages permissions")]
public class PermissionsModule : ApplicationCommandModule<SlashCommandContext>
public class PermissionsModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SubSlashCommand("user", "Manages user permissions")]
public class UserPermissionsModule : ApplicationCommandModule<SlashCommandContext>
public class UserPermissionsModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SubSlashCommand("show", "Shows user permissions")]
public static string Show(
Expand All @@ -18,7 +18,7 @@ public static string Show(
}

[SubSlashCommand("role", "Manages role permissions")]
public class RolePermissionsModule : ApplicationCommandModule<SlashCommandContext>
public class RolePermissionsModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SubSlashCommand("show", "Shows role permissions")]
public static string Show(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Intents = default,
});

ApplicationCommandService<SlashCommandContext> applicationCommandService = new(ApplicationCommandServiceConfiguration<SlashCommandContext>.Default with
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new(ApplicationCommandServiceConfiguration<ApplicationCommandContext>.Default with
{
LocalizationsProvider = new JsonLocalizationsProvider(),
});
Expand All @@ -18,10 +18,10 @@

client.InteractionCreate += async interaction =>
{
if (interaction is not SlashCommandInteraction slashCommandInteraction)
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;

var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client));
var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client));

if (result is not IFailResult failResult)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>(options =>
.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext>(options =>
{
options.LocalizationsProvider = new JsonLocalizationsProvider();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MyBot;

public class ExampleModule : ApplicationCommandModule<SlashCommandContext>
public class ExampleModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("username", "Returns user's username")]
public string Username(User? user = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
Intents = default,
});

ApplicationCommandService<SlashCommandContext> applicationCommandService = new();
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new();
applicationCommandService.AddModules(typeof(Program).Assembly);

client.InteractionCreate += async interaction =>
{
if (interaction is not SlashCommandInteraction slashCommandInteraction)
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;

var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client));
var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client));

if (result is not IFailResult failResult)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MyBot;

public class ExampleModule : ApplicationCommandModule<SlashCommandContext>
public class ExampleModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SlashCommand("mention-everyone", "Mentions @everyone",
DefaultGuildUserPermissions = Permissions.MentionEveryone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
Intents = default,
});

ApplicationCommandService<SlashCommandContext> applicationCommandService = new();
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new();
applicationCommandService.AddModules(typeof(Program).Assembly);

client.InteractionCreate += async interaction =>
{
if (interaction is not SlashCommandInteraction slashCommandInteraction)
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;

var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client));
var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client));

if (result is not IFailResult failResult)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace MyBot;

[SlashCommand("guild", "Guild command")]
public class GuildCommandsModule : ApplicationCommandModule<SlashCommandContext>
public class GuildCommandsModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SubSlashCommand("channels", "Get guild channel count")]
public string Channels() => $"Channels: {Context.Guild!.Channels.Count}";

[SubSlashCommand("name", "Guild name")]
public class GuildNameModule : ApplicationCommandModule<SlashCommandContext>
public class GuildNameModule : ApplicationCommandModule<ApplicationCommandContext>
{
[SubSlashCommand("get", "Get guild name")]
public string GetName() => $"Name: {Context.Guild!.Name}";

[RequireUserPermissions<SlashCommandContext>(Permissions.ManageGuild)]
[RequireBotPermissions<SlashCommandContext>(Permissions.ManageGuild)]
[RequireUserPermissions<ApplicationCommandContext>(Permissions.ManageGuild)]
[RequireBotPermissions<ApplicationCommandContext>(Permissions.ManageGuild)]
[SubSlashCommand("set", "Set guild name")]
public async Task<string> SetNameAsync(string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
Intents = GatewayIntents.Guilds,
});

ApplicationCommandService<SlashCommandContext> applicationCommandService = new();
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new();
applicationCommandService.AddModules(typeof(Program).Assembly);

client.InteractionCreate += async interaction =>
{
if (interaction is not SlashCommandInteraction slashCommandInteraction)
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;

var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client));
var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client));

if (result is not IFailResult failResult)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ First, add the following lines to the using section.
Now, it's time to create @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 instance and add application commands to it. You can do it by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddSlashCommand*, @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddUserCommand* or @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddMessageCommand* to add an application command using the minimal APIs way and/or by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddModules(System.Reflection.Assembly) to add application command modules from an assembly.
[!code-cs[Program.cs](Introduction/Program.cs#L12-L21)]

We can add a command handler now. If you used other context than @NetCord.Services.ApplicationCommands.SlashCommandContext, you should change the interaction type of the handler to the appropriate one.
We can add a command handler now. If you used a context other than @NetCord.Services.ApplicationCommands.ApplicationCommandContext, you may need to change the interaction type of the handler to the appropriate one.
[!code-cs[Program.cs](Introduction/Program.cs#L23-L45)]

Now, we should send the commands to Discord, to make them usable. Add the following line under the handler:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Multiple Services

> [!NOTE]
> Hosting handles multiple instances of @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 automatically, you don't need to do anything.
> When using hosting, multiple instances of @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 are handled automatically, so no additional configuration is needed.
In some scenarios, you might want to use multiple instances of @NetCord.Services.ApplicationCommands.ApplicationCommandService`1. For instance, you may want to use different contexts for different application commands. In such cases, you can utilize the @NetCord.Services.ApplicationCommands.ApplicationCommandServiceManager, which allows you to send commands from different instances of @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 to Discord simultaneously.

To use multiple instances of @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 you need to use @NetCord.Services.ApplicationCommands.ApplicationCommandServiceManager, example:
[!code-cs[Program.cs](MultipleServices/Program.cs)]
2 changes: 1 addition & 1 deletion Documentation/guides/services/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Dependency injection with minimal APIs can seem complicated at first, but it is
What you need to know is that parameters preceding the context parameter are treated as services and parameters following the context parameter are treated as command/interaction parameters. When the context parameter is not present, all parameters are treated as command/interaction parameters.

You can see an example slash command below, but the same rules apply to all services:
[!code-cs[Program.cs](DependencyInjection/Program.cs#L27-L31)]
[!code-cs[Program.cs](DependencyInjection/Program.cs#L27-L32)]

## Modules

Expand Down
4 changes: 2 additions & 2 deletions Documentation/guides/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
items:
- name: Introduction
href: services/application-commands/introduction.md
- name: Multiple Services
href: services/application-commands/multiple-services.md
- name: Parameters
href: services/application-commands/parameters.md
- name: Subcommands
Expand All @@ -40,6 +38,8 @@
href: services/application-commands/permissions.md
- name: Localizations
href: services/application-commands/localizations.md
- name: Multiple Services
href: services/application-commands/multiple-services.md
- name: Component Interactions
items:
- name: Introduction
Expand Down

0 comments on commit 4276b54

Please sign in to comment.