Skip to content

Commit

Permalink
Merge branch 'alpha' into feature/landing-page-details
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 authored Sep 8, 2024
2 parents 6754683 + 305d5c9 commit 515a392
Show file tree
Hide file tree
Showing 301 changed files with 4,194 additions and 2,133 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: 0 additions & 6 deletions Documentation/guides/advanced/Voice/VoiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public async Task PlayAsync(string track)
// Connect
await voiceClient.StartAsync();

// Wait for ready
await voiceClient.ReadyAsync;

// Enter speaking state, to be able to send voice
await voiceClient.EnterSpeakingStateAsync(SpeakingFlags.Microphone);

Expand Down Expand Up @@ -124,9 +121,6 @@ public async Task EchoAsync()
// Connect
await voiceClient.StartAsync();

// Wait for ready
await voiceClient.ReadyAsync;

// Enter speaking state, to be able to send voice
await voiceClient.EnterSpeakingStateAsync(SpeakingFlags.Microphone);

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
4 changes: 2 additions & 2 deletions Documentation/guides/advanced/voice.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Follow the [installation guide](installing-native-dependencies.md) to install th
> In the following examples streams and @NetCord.Gateway.Voice.VoiceClient instances are not disposed because they should be stored somewhere and disposed later.
### Sending Voice
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L12-L102)]
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L12-L99)]

### Receiving Voice
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L104-L146)]
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L101-L140)]
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
8 changes: 4 additions & 4 deletions Documentation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 25 additions & 11 deletions Documentation/templates-src/NetCord/src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,33 @@ function inThisArticle(): TemplateResult {

function findActiveItem(items: (NavItem | NavItemContainer)[]): NavItem {
const url = new URL(window.location.href);
let activeItem: NavItem;
let maxPrefix = 0;
for (const item of items.map((i) => ("items" in i ? i.items : i)).flat()) {
const href = item.href;
if (!isExternalHref(href) && commonUrl(url, href))
return item;
if (isExternalHref(item.href)) {
continue;
}
const prefix = commonUrlPrefix(url, item.href);
if (prefix == maxPrefix) {
activeItem = undefined;
}
else if (prefix > maxPrefix) {
maxPrefix = prefix;
activeItem = item;
}
}
}

function commonUrl(url: URL, base: URL): boolean {
const urlPath = normalizePath(url.pathname);
const basePath = normalizePath(base.pathname);
return urlPath === basePath;
}

function normalizePath(url: string): string {
return url.replace(/\/(?:index.html)?$/, "/");
function commonUrlPrefix(url: URL, base: URL): number {
const urlSegments = url.pathname.split("/");
const baseSegments = base.pathname.split("/");
let i = 0;
while (
i < urlSegments.length &&
i < baseSegments.length &&
urlSegments[i] === baseSegments[i]
) {
i++;
}
return i;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ private static async Task HandleRequestAsync(HttpContext context, HttpInteractio
return null;

var response = context.Response;
var interaction = HttpInteractionFactory.Create(timestampAndBody.Span[timestampByteCount..], async (interaction, interactionCallback, properties) =>
var interaction = HttpInteractionFactory.Create(timestampAndBody.Span[timestampByteCount..], async (interaction, interactionCallback, properties, cancellationToken) =>
{
using var content = interactionCallback.Serialize();
response.ContentType = content.Headers.ContentType!.ToString();
await content.CopyToAsync(response.Body).ConfigureAwait(false);
await content.CopyToAsync(response.Body, cancellationToken).ConfigureAwait(false);
await response.CompleteAsync().ConfigureAwait(false);
}, client);

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.72</VersionSuffix>
<VersionSuffix>alpha.87</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)));
}
}
Loading

0 comments on commit 515a392

Please sign in to comment.