-
-
Notifications
You must be signed in to change notification settings - Fork 40
Handle command results by class instead of Func #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KubaZ2
merged 10 commits into
NetCordDev:alpha
from
csmir:enhancement/resulthandlingbyclass
Aug 9, 2024
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8515eec
Implement IMessageProperties
csmir 801ac08
Remove unnecessary usings
csmir c0584b3
Swap TTS with components, oopsie
csmir 660df95
Mark IMessageProperties as partial (for SG?)
csmir 85a080c
Merge branch 'alpha' of https://github.com/csmir/NetCord into alpha
csmir 7a31fcc
Init
csmir df133d5
Clean usings
csmir 2c7206d
^ (again)
csmir abc1a54
Take suggestions
csmir 0c791f4
Clarity & accessibility reduction, take suggestion
csmir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| public virtual ValueTask HandleResultAsync(IExecutionResult result, TInteraction interaction, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services) | ||
|
csmir marked this conversation as resolved.
Outdated
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))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Hosting/NetCord.Hosting.Services/ApplicationCommands/IApplicationCommandResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Hosting/NetCord.Hosting.Services/Commands/CommandResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| })); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Hosting/NetCord.Hosting.Services/Commands/ICommandResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Hosting/NetCord.Hosting.Services/ComponentInteractions/IComponentInteractionResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
18
Tests/NetCord.Test.Hosting/CustomSlashCommandResultHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.