2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.Hermes.Socket;
|
2024-06-16 20:19:31 -04:00
|
|
|
using TwitchLib.Client.Models;
|
2024-07-19 12:56:41 -04:00
|
|
|
using static TwitchChatTTS.Chat.Commands.TTSCommands;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.Chat.Commands
|
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
public class TTSCommand : IChatCommand
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private readonly ILogger _logger;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
|
|
|
|
public TTSCommand(User user, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_user = user;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger = logger;
|
2024-07-19 12:56:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name => "tts";
|
|
|
|
|
|
|
|
public void Build(ICommandBuilder builder)
|
|
|
|
{
|
|
|
|
builder.CreateCommandTree(Name, b =>
|
|
|
|
{
|
|
|
|
b.CreateStaticInputParameter("add", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", false)
|
|
|
|
.CreateCommand(new AddTTSVoiceCommand(_user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("del", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
|
|
|
.CreateCommand(new DeleteTTSVoiceCommand(_user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("delete", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
|
|
|
.CreateCommand(new DeleteTTSVoiceCommand(_user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("remove", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
|
|
|
.CreateCommand(new DeleteTTSVoiceCommand(_user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("enable", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", false)
|
|
|
|
.CreateCommand(new SetTTSVoiceStateCommand(true, _user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("on", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", false)
|
|
|
|
.CreateCommand(new SetTTSVoiceStateCommand(true, _user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("disable", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
|
|
|
.CreateCommand(new SetTTSVoiceStateCommand(false, _user, _logger));
|
|
|
|
})
|
|
|
|
.CreateStaticInputParameter("off", b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
|
|
|
.CreateCommand(new SetTTSVoiceStateCommand(false, _user, _logger));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class AddTTSVoiceCommand : IChatPartialCommand
|
|
|
|
{
|
|
|
|
private readonly User _user;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
public bool AcceptCustomPermission { get => false; }
|
|
|
|
|
|
|
|
|
|
|
|
public AddTTSVoiceCommand(User user, ILogger logger)
|
|
|
|
{
|
|
|
|
_user = user;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckDefaultPermissions(ChatMessage message)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public async Task Execute(IDictionary<string, string> values, ChatMessage message, HermesSocketClient client)
|
|
|
|
{
|
|
|
|
if (_user == null || _user.VoicesAvailable == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var voiceName = values["voiceName"];
|
|
|
|
var voiceNameLower = voiceName.ToLower();
|
|
|
|
var exists = _user.VoicesAvailable.Any(v => v.Value.ToLower() == voiceNameLower);
|
|
|
|
if (exists)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Voice already exists [voice: {voiceName}][id: {message.UserId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.CreateTTSVoice(voiceName);
|
|
|
|
_logger.Information($"Added a new TTS voice by {message.Username} [voice: {voiceName}][id: {message.UserId}]");
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
private sealed class DeleteTTSVoiceCommand : IChatPartialCommand
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
public bool AcceptCustomPermission { get => false; }
|
|
|
|
|
|
|
|
public DeleteTTSVoiceCommand(User user, ILogger logger)
|
|
|
|
{
|
|
|
|
_user = user;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckDefaultPermissions(ChatMessage message)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute(IDictionary<string, string> values, ChatMessage message, HermesSocketClient client)
|
|
|
|
{
|
|
|
|
if (_user == null || _user.VoicesAvailable == null)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Voices available are not loaded [chatter: {message.Username}][chatter id: {message.UserId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var voiceName = values["voiceName"];
|
|
|
|
var voiceNameLower = voiceName.ToLower();
|
|
|
|
var exists = _user.VoicesAvailable.Any(v => v.Value.ToLower() == voiceNameLower);
|
|
|
|
if (!exists)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Voice does not exist [voice: {voiceName}][chatter: {message.Username}][chatter id: {message.UserId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var voiceId = _user.VoicesAvailable.FirstOrDefault(v => v.Value.ToLower() == voiceName).Key;
|
|
|
|
await client.DeleteTTSVoice(voiceId);
|
|
|
|
_logger.Information($"Deleted a TTS voice [voice: {voiceName}][chatter: {message.Username}][chatter id: {message.UserId}]");
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
private sealed class SetTTSVoiceStateCommand : IChatPartialCommand
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
private bool _state;
|
|
|
|
private readonly User _user;
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
public bool AcceptCustomPermission { get => true; }
|
|
|
|
|
|
|
|
public SetTTSVoiceStateCommand(bool state, User user, ILogger logger)
|
|
|
|
{
|
|
|
|
_state = state;
|
|
|
|
_user = user;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckDefaultPermissions(ChatMessage message)
|
|
|
|
{
|
|
|
|
return message.IsModerator || message.IsBroadcaster;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute(IDictionary<string, string> values, ChatMessage message, HermesSocketClient client)
|
|
|
|
{
|
|
|
|
if (_user == null || _user.VoicesAvailable == null)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
var voiceName = values["voiceName"];
|
|
|
|
var voiceNameLower = voiceName.ToLower();
|
|
|
|
var voiceId = _user.VoicesAvailable.FirstOrDefault(v => v.Value.ToLower() == voiceNameLower).Key;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
await client.UpdateTTSVoiceState(voiceId, _state);
|
|
|
|
_logger.Information($"Changed state for TTS voice [voice: {voiceName}][state: {_state}][invoker: {message.Username}][id: {message.UserId}]");
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|