46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using HermesSocketServer.Models;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace HermesSocketServer.Requests
|
|
{
|
|
public class CreateTTSUser : IRequest
|
|
{
|
|
public string Name => "create_tts_user";
|
|
public string[] RequiredKeys => ["chatter", "voice"];
|
|
private readonly ServerConfiguration _configuration;
|
|
private ILogger _logger;
|
|
|
|
public CreateTTSUser(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 disabled on this channel."));
|
|
|
|
bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
|
{
|
|
UserId = channel.Id,
|
|
ChatterId = chatterId,
|
|
VoiceId = data["voice"].ToString()!
|
|
});
|
|
|
|
if (result)
|
|
{
|
|
_logger.Information($"Selected a tts voice [voice: {voiceId}] for user [chatter: {chatterId}] in channel [channel: {channel.Id}]");
|
|
return Task.FromResult(RequestResult.Successful(null));
|
|
}
|
|
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
|
}
|
|
}
|
|
} |