Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ApplicationCommandInteractionHandler(IServiceProvider services,
_handleAsync = &HandleInteractionAsync;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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<TInteraction, TContext> : IApplicationCommandResultHandler<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext
Comment thread
csmir marked this conversation as resolved.
Outdated
{
public virtual ValueTask HandleResultAsync(IExecutionResult result, TInteraction interaction, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services)
Comment thread
csmir marked this conversation as resolved.
Outdated
Comment thread
csmir marked this conversation as resolved.
Outdated
{
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)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Logging;

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

Expand All @@ -15,20 +14,7 @@ public class ApplicationCommandServiceOptions<TInteraction, TContext> where TInt

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

public Func<IExecutionResult, TInteraction, TContext, GatewayClient?, ILogger, IServiceProvider, ValueTask> HandleResultAsync { get; set; } = (result, interaction, context, 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<TInteraction, TContext> ResultHandler { get; set; } = new ApplicationCommandResultHandler<TInteraction, TContext>();
}

public class ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext> : ApplicationCommandServiceOptions<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext where TAutocompleteContext : IAutocompleteInteractionContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

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

namespace NetCord.Hosting.Services.ApplicationCommands;

public interface IApplicationCommandResultHandler<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext
{
public ValueTask HandleResultAsync(IExecutionResult result, TInteraction interaction, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public CommandHandler(IServiceProvider services,

_getPrefixLengthAsync = GetGetPrefixLengthAsyncDelegate(optionsValue);
_createContext = optionsValue.CreateContext ?? ContextHelper.CreateContextDelegate<Message, GatewayClient, TContext>();
_handleResultAsync = optionsValue.HandleResultAsync;
_handleResultAsync = optionsValue.ResultHandler.HandleResultAsync;
Comment thread
csmir marked this conversation as resolved.
Outdated
_client = client;
}

Expand Down
29 changes: 29 additions & 0 deletions Hosting/NetCord.Hosting.Services/Commands/CommandResultHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Services;
using NetCord.Services.Commands;

namespace NetCord.Hosting.Services.Commands;

public class CommandResultHandler<TContext> : ICommandResultHandler<TContext> where TContext : ICommandContext
{
public virtual ValueTask HandleResultAsync(IExecutionResult result, Message message, TContext context, GatewayClient client, ILogger logger, IServiceProvider services)
{
if (result is not IFailResult failResult)
return default;

string resultMessage = failResult.Message;

if (failResult is IExceptionResult exceptionResult)
logger.LogError(exceptionResult.Exception, "Execution of a command with content '{Content}' failed with an exception", message.Content);
else
logger.LogDebug("Execution of a command with content '{Content}' failed with '{Message}'", message.Content, resultMessage);

return new(message.ReplyAsync(new()
{
Content = resultMessage,
FailIfNotExists = false,
}));
}
}
24 changes: 2 additions & 22 deletions Hosting/NetCord.Hosting.Services/Commands/CommandServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Services;
using NetCord.Gateway;
using NetCord.Services.Commands;

namespace NetCord.Hosting.Services.Commands;
Expand All @@ -20,22 +17,5 @@ public class CommandServiceOptions<TContext> where TContext : ICommandContext

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

public Func<IExecutionResult, Message, TContext, GatewayClient, ILogger, IServiceProvider, ValueTask> HandleResultAsync { get; set; } = (result, message, context, client, logger, services) =>
{
if (result is not IFailResult failResult)
return default;

string resultMessage = failResult.Message;

if (failResult is IExceptionResult exceptionResult)
logger.LogError(exceptionResult.Exception, "Execution of a command with content '{Content}' failed with an exception", message.Content);
else
logger.LogDebug("Execution of a command with content '{Content}' failed with '{Message}'", message.Content, resultMessage);

return new(message.ReplyAsync(new()
{
Content = resultMessage,
FailIfNotExists = false,
}));
};
public ICommandResultHandler<TContext> ResultHandler { get; set; } = new CommandResultHandler<TContext>();
}
12 changes: 12 additions & 0 deletions Hosting/NetCord.Hosting.Services/Commands/ICommandResultHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Services;
using NetCord.Services.Commands;

namespace NetCord.Hosting.Services.Commands;

public interface ICommandResultHandler<TContext> where TContext : ICommandContext
{
public ValueTask HandleResultAsync(IExecutionResult result, Message message, TContext context, GatewayClient client, ILogger logger, IServiceProvider services);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ComponentInteractionHandler(IServiceProvider services,
_handleAsync = &HandleInteractionAsync;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Rest;
using NetCord.Services;
using NetCord.Services.ComponentInteractions;

namespace NetCord.Hosting.Services.ComponentInteractions;

public class ComponentInteractionResultHandler<TInteraction, TContext> : IComponentInteractionResultHandler<TInteraction, TContext> where TInteraction : Interaction where TContext : IComponentInteractionContext
{
public virtual ValueTask HandleResultAsync(IExecutionResult result, TInteraction interaction, TContext context, GatewayClient? client, ILogger logger, IServiceProvider 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 interaction of custom ID '{Id}' failed with an exception", interaction.Id);
else
logger.LogDebug("Execution of an interaction of custom ID '{Id}' failed with '{Message}'", interaction.Id, message);

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.ComponentInteractions;

namespace NetCord.Hosting.Services.ComponentInteractions;
Expand All @@ -15,18 +11,5 @@ public class ComponentInteractionServiceOptions<TInteraction, TContext> where TI

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

public Func<IExecutionResult, TInteraction, TContext, GatewayClient?, ILogger, IServiceProvider, ValueTask> HandleResultAsync { get; set; } = (result, interaction, context, 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 interaction of custom ID '{Id}' failed with an exception", interaction.Id);
else
logger.LogDebug("Execution of an interaction of custom ID '{Id}' failed with '{Message}'", interaction.Id, message);

return new(interaction.SendResponseAsync(InteractionCallback.Message(message)));
};
public IComponentInteractionResultHandler<TInteraction, TContext> ResultHandler { get; set; } = new ComponentInteractionResultHandler<TInteraction, TContext>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

using NetCord.Gateway;
using NetCord.Services;
using NetCord.Services.ComponentInteractions;

namespace NetCord.Hosting.Services.ComponentInteractions;

public interface IComponentInteractionResultHandler<TInteraction, TContext> where TInteraction : Interaction where TContext : IComponentInteractionContext
{
public ValueTask HandleResultAsync(IExecutionResult result, TInteraction interaction, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services);
}
18 changes: 18 additions & 0 deletions Tests/NetCord.Test.Hosting/CustomSlashCommandResultHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.Logging;

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

namespace NetCord.Test.Hosting;

internal class CustomSlashCommandResultHandler : ApplicationCommandResultHandler<SlashCommandInteraction, SlashCommandContext>
{
public override ValueTask HandleResultAsync(IExecutionResult result, SlashCommandInteraction interaction, SlashCommandContext context, GatewayClient? client, ILogger logger, IServiceProvider services)
{
logger.LogInformation("Handling result of slash command");

return base.HandleResultAsync(result, interaction, context, client, logger, services);
}
}
5 changes: 4 additions & 1 deletion Tests/NetCord.Test.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
{
Intents = GatewayIntents.All,
})
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext, AutocompleteInteractionContext>()
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext, AutocompleteInteractionContext>(options =>
{
options.ResultHandler = new CustomSlashCommandResultHandler();
})
.AddApplicationCommands<UserCommandInteraction, UserCommandContext>()
.AddApplicationCommands<MessageCommandInteraction, MessageCommandContext>()
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
Expand Down