~ruther/NosSmooth.Local

ref: e17f06759be04b9bf33005d654a2305eca11d7ac NosSmooth.Local/src/Extensions/NosSmooth.ChatCommands/ChatCommandInterceptor.cs -rw-r--r-- 3.8 KiB
e17f0675 — Rutherther chore: update to .net 7 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  ChatCommandInterceptor.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection.Emit;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalClient;
using NosSmooth.Packets.Enums;
using NosSmooth.Packets.Enums.Chat;
using NosSmooth.Packets.Server.Chat;
using Remora.Commands.Services;

namespace NosSmooth.ChatCommands;

/// <summary>
/// Handles commands in the chat.
/// </summary>
public class ChatCommandInterceptor : IPacketInterceptor
{
    private readonly CommandService _commandService;
    private readonly IServiceProvider _serviceProvider;
    private readonly FeedbackService _feedbackService;
    private readonly ILogger<ChatCommandInterceptor> _logger;
    private readonly ChatCommandsOptions _options;

    /// <summary>
    /// Initializes a new instance of the <see cref="ChatCommandInterceptor"/> class.
    /// </summary>
    /// <param name="options">The options.</param>
    /// <param name="commandService">The command service.</param>
    /// <param name="serviceProvider">The services.</param>
    /// <param name="feedbackService">The feedback service.</param>
    /// <param name="logger">The logger.</param>
    public ChatCommandInterceptor
    (
        IOptions<ChatCommandsOptions> options,
        CommandService commandService,
        IServiceProvider serviceProvider,
        FeedbackService feedbackService,
        ILogger<ChatCommandInterceptor> logger
    )
    {
        _commandService = commandService;
        _serviceProvider = serviceProvider;
        _feedbackService = feedbackService;
        _logger = logger;
        _options = options.Value;
    }

    /// <inheritdoc />
    public bool InterceptSend(ref string packet)
    {
        ReadOnlySpan<char> span = packet;
        if (span.StartsWith("say ") && span.Slice(4).StartsWith(_options.Prefix))
        {
            var command = span.Slice(4 + _options.Prefix.Length).ToString();
            Task.Run(async () => await ExecuteCommand(command));
            return false;
        }

        return true;
    }

    /// <inheritdoc />
    public bool InterceptReceive(ref string packet)
    {
        return true;
    }

    private async Task ExecuteCommand(string command)
    {
        try
        {
            var preparedResult = await _commandService.TryPrepareCommandAsync(command, _serviceProvider);
            if (!preparedResult.IsSuccess)
            {
                _logger.LogError($"Could not prepare \"{command}\"");
                _logger.LogResultError(preparedResult);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not prepare the given command. {preparedResult.ToFullString()}");
                return;
            }

            var executeResult = await _commandService.TryExecuteAsync(preparedResult.Entity, _serviceProvider);
            if (!executeResult.IsSuccess)
            {
                _logger.LogError($"Could not execute \"{command}\"");
                _logger.LogResultError(executeResult);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not execute the given command. {executeResult.ToFullString()}");
                return;
            }

            if (!executeResult.Entity.IsSuccess)
            {
                _logger.LogError($"There was an error while handling \"{command}\"");
                _logger.LogResultError(executeResult.Entity);
                await _feedbackService.SendErrorMessageAsync
                    ($"Could not execute the given command. {executeResult.Entity.ToFullString()}");
            }
        }
        catch (Exception e)
        {
            _logger.LogError(e, "Could not execute a command");
        }
    }
}
Do not follow this link