Skip to content

Commit

Permalink
Use application builder in guides
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Sep 13, 2024
1 parent 06c9f0a commit 45c6537
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Documentation/guides/advanced/HttpInteractions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

var builder = WebApplication.CreateBuilder(args);

builder.Host
.UseDiscordRest()
.UseApplicationCommands<SlashCommandInteraction, HttpSlashCommandContext>();
builder.Services
.AddDiscordRest()
.AddApplicationCommands<SlashCommandInteraction, HttpSlashCommandContext>();

var app = builder.Build();

Expand Down
6 changes: 4 additions & 2 deletions Documentation/guides/advanced/ShardingHosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using NetCord.Hosting.Gateway;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordShardedGateway();
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordShardedGateway();

var host = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/advanced/http-interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Follow the [installation guide](installing-native-dependencies.md) to install th

## Usage

To handle HTTP interactions, you need to use @NetCord.Hosting.Rest.RestClientHostBuilderExtensions.UseDiscordRest(Microsoft.Extensions.Hosting.IHostBuilder) to add the @NetCord.Rest.RestClient and then call @NetCord.Hosting.AspNetCore.EndpointRouteBuilderExtensions.UseHttpInteractions(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder,System.String) to map the interactions route.
To handle HTTP interactions, you need to use @NetCord.Hosting.Rest.RestClientServiceCollectionExtensions.AddDiscordRest(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the @NetCord.Rest.RestClient and then call @NetCord.Hosting.AspNetCore.EndpointRouteBuilderExtensions.UseHttpInteractions(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder,System.String) to map the interactions route.

[!code-cs[Program.cs](HttpInteractions/Program.cs?highlight=10,17-18)]

Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/advanced/sharding.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Sharding is splitting your bot into multiple @"NetCord.Gateway.GatewayClient"s.

## [Generic Host](#tab/generic-host)

To start sharding with the generic host, instead of calling @NetCord.Hosting.Gateway.GatewayClientHostBuilderExtensions.UseDiscordGateway(Microsoft.Extensions.Hosting.IHostBuilder), you need to call @NetCord.Hosting.Gateway.ShardedGatewayClientHostBuilderExtensions.UseDiscordShardedGateway(Microsoft.Extensions.Hosting.IHostBuilder). Example:
To start sharding with the generic host, instead of calling @NetCord.Hosting.Gateway.GatewayClientServiceCollectionExtensions.AddDiscordGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection), you need to call @NetCord.Hosting.Gateway.ShardedGatewayClientServiceCollectionExtensions.AddDiscordShardedGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection). Example:
[!code-cs[Program.cs](ShardingHosting/Program.cs)]

Also note that you need to use @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead of @NetCord.Hosting.Gateway.IGatewayEventHandler or @NetCord.Hosting.Gateway.IGatewayEventHandler`1 for event handlers. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers.
Expand Down
8 changes: 5 additions & 3 deletions Documentation/guides/events/FirstEventsHosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using NetCord.Gateway;
using NetCord.Hosting.Gateway;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway(options =>
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway(options =>
{
options.Configuration = new()
{
Expand All @@ -15,7 +17,7 @@
| GatewayIntents.GuildMessageReactions,
};
})
.ConfigureServices(services => services.AddGatewayEventHandlers(typeof(Program).Assembly));
.AddGatewayEventHandlers(typeof(Program).Assembly);

var host = builder.Build()
.UseGatewayEventHandlers();
Expand Down
6 changes: 4 additions & 2 deletions Documentation/guides/events/IntentsHosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using NetCord.Gateway;
using NetCord.Hosting.Gateway;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway(options =>
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway(options =>
{
options.Configuration = new()
{
Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/events/first-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
The preferred way to receive events with the generic host is by implementing @NetCord.Hosting.Gateway.IGatewayEventHandler or @NetCord.Hosting.Gateway.IGatewayEventHandler`1.

First, use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add all event handlers in an assembly. You also need to call @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the handlers to the client.
[!code-cs[Program.cs](FirstEventsHosting/Program.cs?highlight=18,21)]
[!code-cs[Program.cs](FirstEventsHosting/Program.cs?highlight=20,23)]

> [!NOTE]
> All gateway event handlers require @NetCord.Hosting.Gateway.GatewayEventAttribute that specifies the event to bind to. The event argument must match the type of the handler, otherwise an exception will be thrown.
Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/events/intents.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Intents in NetCord are handled by @NetCord.Gateway.GatewayIntents.
You specify them like this:

## [Generic Host](#tab/generic-host)
[!code-cs[Program.cs](IntentsHosting/Program.cs?highlight=6#L6-L13)]
[!code-cs[Program.cs](IntentsHosting/Program.cs?highlight=6#L8-L15)]

## [Bare Bones](#tab/bare-bones)
[!code-cs[Program.cs](Intents/Program.cs?highlight=3#L4-L7)]
Expand Down
6 changes: 4 additions & 2 deletions Documentation/guides/getting-started/CodingHosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using NetCord.Hosting.Gateway;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway();
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway();

var host = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/getting-started/coding.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Before we start, you need a token of your bot... so you need to go to the [Disco
## [Generic Host](#tab/generic-host)

With the generic host, you can just use @NetCord.Hosting.Gateway.GatewayClientHostBuilderExtensions.UseDiscordGateway(Microsoft.Extensions.Hosting.IHostBuilder) to add your bot to the host. Quite easy, right?
With the generic host, you can just use @NetCord.Hosting.Gateway.GatewayClientServiceCollectionExtensions.AddDiscordGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add your bot to the host. Quite easy, right?
[!code-cs[Program.cs](CodingHosting/Program.cs)]

Also note that the token needs to be stored in the configuration. You can for example use `appsettings.json` file. It should look like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
using NetCord.Hosting.Services.ApplicationCommands;
using NetCord.Services.ApplicationCommands;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway()
.UseApplicationCommands<SlashCommandInteraction, SlashCommandContext>()
.UseApplicationCommands<UserCommandInteraction, UserCommandContext>()
.UseApplicationCommands<MessageCommandInteraction, MessageCommandContext>();
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway()
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>()
.AddApplicationCommands<UserCommandInteraction, UserCommandContext>()
.AddApplicationCommands<MessageCommandInteraction, MessageCommandContext>();

var host = builder.Build()
.AddSlashCommand<SlashCommandContext>("ping", "Ping!", () => "Pong!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
using NetCord.Hosting.Services.ApplicationCommands;
using NetCord.Services.ApplicationCommands;

var builder = Host.CreateDefaultBuilder(args)
.UseApplicationCommands<SlashCommandInteraction, SlashCommandContext>(options =>
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>(options =>
{
options.Configuration = ApplicationCommandServiceConfiguration<SlashCommandContext>.Default with
{
LocalizationsProvider = new JsonLocalizationsProvider(),
};
})
.UseDiscordGateway();
.AddDiscordGateway();

var host = builder.Build()
.AddModules(typeof(Program).Assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
## [Generic Host](#tab/generic-host)

Adding application commands with the generic host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostBuilderExtensions.UseApplicationCommands``2(Microsoft.Extensions.Hosting.IHostBuilder) to add an application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=11-13,16-20)]
Adding application commands with the generic host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddApplicationCommands``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add an application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13-15,18-22)]

## [Bare Bones](#tab/bare-bones)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ To localize application commands, you need to use @NetCord.Services.ApplicationC
The samples below show how to specify the @NetCord.Services.ApplicationCommands.JsonLocalizationsProvider.

## [Generic Host](#tab/generic-host)
[!code-cs[Program.cs](LocalizationsHosting/Program.cs?highlight=6#L9-L16)]
[!code-cs[Program.cs](LocalizationsHosting/Program.cs?highlight=6#L11-L18)]

## [Bare Bones](#tab/bare-bones)
[!code-cs[Program.cs](Localizations/Program.cs?highlight=3#L12-L15)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
using NetCord.Hosting.Services.ComponentInteractions;
using NetCord.Services.ComponentInteractions;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway()
.UseComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
.UseComponentInteractions<StringMenuInteraction, StringMenuInteractionContext>()
.UseComponentInteractions<UserMenuInteraction, UserMenuInteractionContext>()
.UseComponentInteractions<RoleMenuInteraction, RoleMenuInteractionContext>()
.UseComponentInteractions<MentionableMenuInteraction, MentionableMenuInteractionContext>()
.UseComponentInteractions<ChannelMenuInteraction, ChannelMenuInteractionContext>()
.UseComponentInteractions<ModalInteraction, ModalInteractionContext>();
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway()
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
.AddComponentInteractions<StringMenuInteraction, StringMenuInteractionContext>()
.AddComponentInteractions<UserMenuInteraction, UserMenuInteractionContext>()
.AddComponentInteractions<RoleMenuInteraction, RoleMenuInteractionContext>()
.AddComponentInteractions<MentionableMenuInteraction, MentionableMenuInteractionContext>()
.AddComponentInteractions<ChannelMenuInteraction, ChannelMenuInteractionContext>()
.AddComponentInteractions<ModalInteraction, ModalInteractionContext>();

var host = builder.Build()
.AddComponentInteraction<ButtonInteractionContext>("ping", () => "Pong!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## [Generic Host](#tab/generic-host)

Adding component interactions with the generic host is very easy. Use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceHostBuilderExtensions.UseComponentInteractions``2(Microsoft.Extensions.Hosting.IHostBuilder) to add a component interaction service to your host builder. Then, use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceHostExtensions.AddComponentInteraction* to add a component interaction using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=11-17,20-28)]
Adding component interactions with the generic host is very easy. Use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceServiceCollectionExtensions.AddComponentInteractions``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add a component interaction service to your host builder. Then, use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceHostExtensions.AddComponentInteraction* to add a component interaction using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13-19,22-30)]

## [Bare Bones](#tab/bare-bones)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using NetCord.Hosting.Services.Commands;
using NetCord.Services.Commands;

var builder = Host.CreateDefaultBuilder(args)
.UseDiscordGateway()
.UseCommands<CommandContext>();
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddDiscordGateway()
.AddCommands<CommandContext>();

var host = builder.Build()
.AddCommand<CommandContext>(["ping"], () => "Pong!")
Expand Down
4 changes: 2 additions & 2 deletions Documentation/guides/services/text-commands/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## [Generic Host](#tab/generic-host)

Adding commands with the generic host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceHostBuilderExtensions.UseCommands``1(Microsoft.Extensions.Hosting.IHostBuilder) to add a command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=10,13-15)]
Adding commands with the generic host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceServiceCollectionExtensions.AddCommands``1(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add a command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the ASP.NET Core minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers.
[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=12,15-17)]

### Specifying a prefix

Expand Down

0 comments on commit 45c6537

Please sign in to comment.