Skip to content

Commit

Permalink
Merge alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Aug 25, 2024
1 parent c1f15c5 commit 11d5618
Show file tree
Hide file tree
Showing 140 changed files with 1,139 additions and 836 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on:
branches-ignore:
- stable
- alpha
pull_request:
types: [opened, synchronize]
branches:
- stable
- alpha

jobs:
build:
Expand Down
6 changes: 3 additions & 3 deletions Documentation/guides/advanced/sharding.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Sharding is splitting your bot into multiple @"NetCord.Gateway.GatewayClient"s.

## How to shard?

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

With hosting, to start sharding, 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.GatewayClientHostBuilderExtensions.UseDiscordGateway(Microsoft.Extensions.Hosting.IHostBuilder), you need to call @NetCord.Hosting.Gateway.ShardedGatewayClientHostBuilderExtensions.UseDiscordShardedGateway(Microsoft.Extensions.Hosting.IHostBuilder). 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.

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

To start sharding, you need to create an instance of @NetCord.Gateway.ShardedGatewayClient. Its usage is very similar to @NetCord.Gateway.GatewayClient. Example:
[!code-cs[Program.cs](Sharding/Program.cs)]
Expand Down
6 changes: 3 additions & 3 deletions Documentation/guides/events/first-events.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# First Events

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

With hosting, the preferred way to receive events is by implementing @NetCord.Hosting.Gateway.IGatewayEventHandler or @NetCord.Hosting.Gateway.IGatewayEventHandler`1.
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)]
Expand All @@ -28,7 +28,7 @@ Other events work similar to these. You can play with them if you want!
> [!NOTE]
> When using @NetCord.Gateway.ShardedGatewayClient, you need to implement @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers instead.
## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

### MessageCreate Event
To listen to the event, add the following lines before `client.StartAsync()`!
Expand Down
4 changes: 2 additions & 2 deletions Documentation/guides/events/intents.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Privileged intents are intents that you need to enable in [Discord Developer Por
Intents in NetCord are handled by @NetCord.Gateway.GatewayIntents.
You specify them like this:

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

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)
[!code-cs[Program.cs](Intents/Program.cs?highlight=3#L4-L7)]

***
Expand Down
6 changes: 3 additions & 3 deletions Documentation/guides/getting-started/coding.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Before we start, you need a token of your bot... so you need to go to the [Disco
> [!IMPORTANT]
> You should never give your token to anybody.
## [Hosting](#tab/hosting)
## [Generic Host](#tab/generic-host)

With hosting, 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.GatewayClientHostBuilderExtensions.UseDiscordGateway(Microsoft.Extensions.Hosting.IHostBuilder) 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 All @@ -21,7 +21,7 @@ Also note that the token needs to be stored in the configuration. You can for ex
Now, when you run the code, your bot should be online!
![](../../images/coding_BotOnline.png)

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

Add the following lines to file `Program.cs`.
[!code-cs[Program.cs](Coding/Program.cs#L1-L4)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ public class MessageCommandModule : ApplicationCommandModule<MessageCommandConte
[MessageCommand("Timestamp")]
public string Timestamp() => Context.Target.CreatedAt.ToString();
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ public class UserCommandModule : ApplicationCommandModule<UserCommandContext>
[UserCommand("ID")]
public string Id() => Context.Target.Id.ToString();
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
>
> **must** be lowercase.
## [Hosting](#tab/hosting)
## [Generic Host](#tab/generic-host)

With hosting, adding application commands 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.
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)]

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

First, add the following lines to using the section.
[!code-cs[Program.cs](Introduction/Program.cs#L4-L5)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ To localize application commands, you need to use @NetCord.Services.ApplicationC

The samples below show how to specify the @NetCord.Services.ApplicationCommands.JsonLocalizationsProvider.

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

## [Without Hosting](#tab/without-hosting)
## [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
@@ -1,11 +1,11 @@
# Introduction

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

With hosting, adding component interactions 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.
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)]

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

First, add the following lines to the using section.
[!code-cs[Program.cs](Introduction/Program.cs#L4-L5)]
Expand Down
6 changes: 3 additions & 3 deletions Documentation/guides/services/text-commands/introduction.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Introduction

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

With hosting, adding commands 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.
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)]

### Specifying a prefix

You can specify a prefix in the configuration. You can for example use `appsettings.json` file. It should look like this:
[!code-json[appsettings.json](IntroductionHosting/appsettings.json)]

## [Without Hosting](#tab/without-hosting)
## [Bare Bones](#tab/bare-bones)

First, add the following lines to the using section.
[!code-cs[Program.cs](Introduction/Program.cs#L3-L4)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageIcon>SmallSquare.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>$(VersionPrefix)</Version>
<VersionSuffix>alpha.73</VersionSuffix>
<VersionSuffix>alpha.82</VersionSuffix>
<Description>The modern and fully customizable C# Discord library.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using NetCord.Gateway;
using NetCord.Hosting.Gateway;
using NetCord.Services;
using NetCord.Services.ApplicationCommands;

namespace NetCord.Hosting.Services.ApplicationCommands;
Expand All @@ -19,7 +18,7 @@ internal unsafe partial class ApplicationCommandInteractionHandler<TInteraction,
private readonly IServiceScopeFactory? _scopeFactory;
private readonly delegate*<ApplicationCommandInteractionHandler<TInteraction, TContext>, Interaction, GatewayClient?, ValueTask> _handleAsync;
private readonly Func<TInteraction, GatewayClient?, IServiceProvider, TContext> _createContext;
private readonly Func<IExecutionResult, TInteraction, GatewayClient?, ILogger, IServiceProvider, ValueTask> _handleResultAsync;
private readonly IApplicationCommandResultHandler<TContext> _resultHandler;
private readonly GatewayClient? _client;

public ApplicationCommandInteractionHandler(IServiceProvider services,
Expand All @@ -44,7 +43,7 @@ public ApplicationCommandInteractionHandler(IServiceProvider services,
_handleAsync = &HandleInteractionAsync;

_createContext = optionsValue.CreateContext ?? ContextHelper.CreateContextDelegate<TInteraction, GatewayClient?, TContext>();
_handleResultAsync = optionsValue.HandleResultAsync;
_resultHandler = optionsValue.ResultHandler;
_client = client;
}

Expand Down Expand Up @@ -89,7 +88,7 @@ private async ValueTask HandleInteractionAsyncCore(TInteraction interaction, Gat

try
{
await _handleResultAsync(result, interaction, client, _logger, services).ConfigureAwait(false);
await _resultHandler.HandleResultAsync(result, context, client, _logger, services).ConfigureAwait(false);
}
catch (Exception exceptionHandlerException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Rest;
using NetCord.Services;
using NetCord.Services.ApplicationCommands;

namespace NetCord.Hosting.Services.ApplicationCommands;

public class ApplicationCommandResultHandler<TContext>(MessageFlags? messageFlags = null) : IApplicationCommandResultHandler<TContext> where TContext : IApplicationCommandContext
{
public ValueTask HandleResultAsync(IExecutionResult result, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services)
{
if (result is not IFailResult failResult)
return default;

var resultMessage = failResult.Message;

var interaction = context.Interaction;

if (failResult is IExceptionResult exceptionResult)
logger.LogError(exceptionResult.Exception, "Execution of an application command of name '{Name}' failed with an exception", interaction.Data.Name);
else
logger.LogDebug("Execution of an application command of name '{Name}' failed with '{Message}'", interaction.Data.Name, resultMessage);

InteractionMessageProperties message = new()
{
Content = resultMessage,
Flags = messageFlags,
};

return new(interaction.SendResponseAsync(InteractionCallback.Message(message)));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Rest;
using NetCord.Services;
using NetCord.Gateway;
using NetCord.Services.ApplicationCommands;

namespace NetCord.Hosting.Services.ApplicationCommands;
Expand All @@ -15,38 +11,12 @@ public class ApplicationCommandServiceOptions<TInteraction, TContext> where TInt

public Func<TInteraction, GatewayClient?, IServiceProvider, TContext>? CreateContext { get; set; }

public Func<IExecutionResult, TInteraction, GatewayClient?, ILogger, IServiceProvider, ValueTask> HandleResultAsync { get; set; } = (result, interaction, client, logger, services) =>
{
if (result is not IFailResult failResult)
return default;
var message = failResult.Message;
if (failResult is IExceptionResult exceptionResult)
logger.LogError(exceptionResult.Exception, "Execution of an application command of name '{Name}' failed with an exception", interaction.Data.Name);
else
logger.LogDebug("Execution of an application command of name '{Name}' failed with '{Message}'", interaction.Data.Name, message);
return new(interaction.SendResponseAsync(InteractionCallback.Message(message)));
};
public IApplicationCommandResultHandler<TContext> ResultHandler { get; set; } = new ApplicationCommandResultHandler<TContext>();
}

public class ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext> : ApplicationCommandServiceOptions<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext where TAutocompleteContext : IAutocompleteInteractionContext
{
public Func<AutocompleteInteraction, GatewayClient?, IServiceProvider, TAutocompleteContext>? CreateAutocompleteContext { get; set; }

public Func<IExecutionResult, AutocompleteInteraction, GatewayClient?, ILogger, IServiceProvider, ValueTask> HandleAutocompleteResultAsync { get; set; } = (result, interaction, client, logger, services) =>
{
if (result is not IFailResult failResult)
return default;
var commandName = interaction.Data.Name;
if (failResult is IExceptionResult exceptionResult)
logger.LogError(exceptionResult.Exception, "Execution of an autocomplete for application command of name '{Name}' failed with an exception", commandName);
else
logger.LogDebug("Execution of an autocomplete for application command of name '{Name}' failed with '{Message}'", commandName, failResult.Message);
return default;
};
public IAutocompleteInteractionResultHandler<TAutocompleteContext> AutocompleteResultHandler { get; set; } = new AutocompleteInteractionResultHandler<TAutocompleteContext>();
}
Loading

0 comments on commit 11d5618

Please sign in to comment.