Files
hermes-server/Requests/UpdateTTSUser.cs

51 lines
2.0 KiB
C#

using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using HermesSocketServer.Services;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class UpdateTTSUser : IRequest
{
public string Name => "update_tts_user";
public string[] RequiredKeys => ["chatter", "voice"];
private Database _database;
private readonly ServerConfiguration _configuration;
private ILogger _logger;
public UpdateTTSUser(Database database, ServerConfiguration configuration, ILogger logger)
{
_database = database;
_configuration = configuration;
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
if (!long.TryParse(data["chatter"].ToString(), out long chatterId))
return RequestResult.Failed("Chatter should be an integer, representing the Twitch User Id of the chatter.");
var voiceId = data["voice"].ToString()!;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
return 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 RequestResult.Successful(voice);
}
return RequestResult.Failed("Soemthing went wrong when updating the cache.");
}
}
}