Files
hermes-server/Requests/UpdateTTSUser.cs

48 lines
1.8 KiB
C#

using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class UpdateTTSUser : IRequest
{
public string Name => "update_tts_user";
public string[] RequiredKeys => ["chatter", "voice"];
private readonly ServerConfiguration _configuration;
private ILogger _logger;
public UpdateTTSUser(ServerConfiguration configuration, ILogger logger)
{
_configuration = configuration;
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
if (!long.TryParse(data["chatter"].ToString(), out long chatterId))
return Task.FromResult(RequestResult.Failed("Invalid Twitch user id."));
data["user"] = channel.Id;
var voiceId = data["voice"].ToString()!;
var check = channel.VoiceStates.Get(voiceId)?.Enabled ?? false;
if (!check && chatterId != _configuration.Tts.OwnerId)
return Task.FromResult(RequestResult.Failed("Voice is either non-existent or disabled on this channel."));
var voice = new ChatterVoice()
{
UserId = channel.Id,
ChatterId = chatterId,
VoiceId = voiceId
};
var result = channel.Chatters.Modify(chatterId.ToString(), voice);
if (result)
{
_logger.Information($"Updated chatter's selected tts voice on channel [chatter id: {chatterId}][voice id: {voiceId}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(voice));
}
return Task.FromResult(RequestResult.Failed("Soemthing went wrong when updating the cache."));
}
}
}