Skip to content

Commit

Permalink
[OneBot] Fixed Echo when not supported operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 29, 2023
1 parent 61e1359 commit 9c31d2e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions Lagrange.OneBot/Core/Operation/OperationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Core.Network.Service;
using Microsoft.Extensions.Logging;

namespace Lagrange.OneBot.Core.Operation;

public sealed class OperationService
{
private readonly BotContext _bot;
private readonly ILogger _logger;
private readonly LagrangeWebSvcCollection _service;
private readonly Dictionary<string, IOperation> _operations;

public OperationService(BotContext bot, LagrangeWebSvcCollection service)
public OperationService(BotContext bot, Logger<LagrangeApp> logger, LagrangeWebSvcCollection service)
{
_bot = bot;
_logger = logger;
_service = service;
_operations = new Dictionary<string, IOperation>();

Expand All @@ -31,11 +34,9 @@ public OperationService(BotContext bot, LagrangeWebSvcCollection service)

private async Task HandleOperation(MsgRecvEventArgs eventArgs)
{
var action = JsonSerializer.Deserialize<OneBotAction>(eventArgs.Data);

try
if (JsonSerializer.Deserialize<OneBotAction>(eventArgs.Data) is { } action)
{
if (action != null)
try
{
bool supported = _operations.TryGetValue(action.Action, out var handler);

Expand All @@ -48,17 +49,17 @@ private async Task HandleOperation(MsgRecvEventArgs eventArgs)
}
else
{
await _service.SendJsonAsync(new OneBotResult(null, 404, "failed"), eventArgs.Identifier);
await _service.SendJsonAsync(new OneBotResult(null, 404, "failed") { Echo = action.Echo }, eventArgs.Identifier);
}
}
else
catch
{
throw new Exception("action deserialized failed");
await _service.SendJsonAsync(new OneBotResult(null, 200, "failed") { Echo = action.Echo }, eventArgs.Identifier);
}
}
catch
else
{
await _service.SendJsonAsync(new OneBotResult(null, 200, "failed"), eventArgs.Identifier);
_logger.LogWarning($"Json Serialization failed for such action");
}
}
}

0 comments on commit 9c31d2e

Please sign in to comment.