hermes-client/Hermes/Socket/Requests/GetEmotesAck.cs

48 lines
1.5 KiB
C#

using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Emotes;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class GetEmotesAck : IRequestAck
{
public string Name => "get_emotes";
private readonly IEmoteDatabase _emotes;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public GetEmotesAck(IEmoteDatabase emotes, JsonSerializerOptions options, ILogger logger)
{
_emotes = emotes;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var data = JsonSerializer.Deserialize<IEnumerable<EmoteInfo>>(json, _options);
if (data == null)
{
_logger.Warning("Emotes is null.");
return;
}
var count = 0;
var duplicateNames = 0;
foreach (var emote in data)
{
if (_emotes.Get(emote.Name) == null)
{
_emotes.Add(emote.Name, emote.Id);
count++;
}
else
duplicateNames++;
}
_logger.Information($"Fetched emotes of various sources [count: {count}]");
if (duplicateNames > 0)
_logger.Warning($"Found {duplicateNames} emotes with duplicate names.");
}
}
}