hermes-server/Requests/CreateTTSVoice.cs

48 lines
1.5 KiB
C#

using HermesSocketServer.Models;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreateTTSVoice : IRequest
{
public string Name => "create_tts_voice";
public string[] RequiredKeys => ["voice"];
private IStore<string, Voice> _voices;
private ILogger _logger;
private Random _random;
public CreateTTSVoice(VoiceStore voices, ILogger logger)
{
_voices = voices;
_logger = logger;
_random = new Random();
}
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
{
data["voice"] = data["voice"].ToString()!;
string id = RandomString(25);
var result = _voices.Set(id, new Voice()
{
Id = id,
Name = data["voice"].ToString()
});
if (result) {
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]");
return RequestResult.Successful(id);
}
return RequestResult.Failed("Something went wrong when updating the cache.");
}
private string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[_random.Next(s.Length)]).ToArray());
}
}
}