2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.Hermes.Socket;
|
2024-08-04 19:46:10 -04:00
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
2024-07-19 12:56:41 -04:00
|
|
|
using static TwitchChatTTS.Chat.Commands.TTSCommands;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.Chat.Commands
|
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
public class VoiceCommand : IChatCommand
|
2024-03-15 08:27:35 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private readonly ILogger _logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public VoiceCommand(User user, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_user = user;
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public string Name => "voice";
|
|
|
|
|
|
|
|
public void Build(ICommandBuilder builder)
|
2024-03-15 08:27:35 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
builder.CreateCommandTree(Name, b =>
|
|
|
|
{
|
|
|
|
b.CreateVoiceNameParameter("voiceName", true)
|
2024-08-04 19:46:10 -04:00
|
|
|
.CreateCommand(new TTSVoiceSelector(_user, _logger))
|
2024-08-06 15:29:29 -04:00
|
|
|
.CreateMentionParameter("chatter", enabled: true, optional: true)
|
2024-08-04 19:46:10 -04:00
|
|
|
.AddPermission("tts.command.voice.admin")
|
|
|
|
.CreateCommand(new TTSVoiceSelectorAdmin(_user, _logger));
|
2024-07-19 12:56:41 -04:00
|
|
|
});
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
private sealed class TTSVoiceSelector : IChatPartialCommand
|
2024-03-15 08:27:35 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private readonly ILogger _logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public bool AcceptCustomPermission { get => true; }
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public TTSVoiceSelector(User user, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
_user = user;
|
|
|
|
_logger = logger;
|
2024-07-16 00:48:55 -04:00
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task Execute(IDictionary<string, string> values, ChannelChatMessage message, HermesSocketClient hermes)
|
2024-08-04 19:46:10 -04:00
|
|
|
{
|
|
|
|
if (_user == null || _user.VoicesSelected == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
long chatterId = long.Parse(message.ChatterUserId);
|
|
|
|
var voiceName = values["voiceName"];
|
|
|
|
var voiceNameLower = voiceName.ToLower();
|
|
|
|
var voice = _user.VoicesAvailable.First(v => v.Value.ToLower() == voiceNameLower);
|
|
|
|
|
|
|
|
if (_user.VoicesSelected.ContainsKey(chatterId))
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
await hermes.UpdateTTSUser(chatterId, voice.Key);
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
await hermes.CreateTTSUser(chatterId, voice.Key);
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class TTSVoiceSelectorAdmin : IChatPartialCommand
|
|
|
|
{
|
|
|
|
private readonly User _user;
|
|
|
|
private readonly ILogger _logger;
|
2024-07-19 12:56:41 -04:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public bool AcceptCustomPermission { get => true; }
|
|
|
|
|
|
|
|
public TTSVoiceSelectorAdmin(User user, ILogger logger)
|
2024-07-16 00:48:55 -04:00
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_user = user;
|
|
|
|
_logger = logger;
|
2024-07-16 00:48:55 -04:00
|
|
|
}
|
2024-07-19 12:56:41 -04:00
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task Execute(IDictionary<string, string> values, ChannelChatMessage message, HermesSocketClient hermes)
|
2024-07-16 00:48:55 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
if (_user == null || _user.VoicesSelected == null)
|
|
|
|
return;
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
var mention = message.Message.Fragments.FirstOrDefault(f => f.Mention != null && f.Text == values["chatter"])?.Mention;
|
2024-08-04 19:46:10 -04:00
|
|
|
if (mention == null)
|
|
|
|
{
|
|
|
|
_logger.Warning("Failed to find the chatter to apply voice command to.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long chatterId = long.Parse(mention.UserId);
|
2024-07-19 12:56:41 -04:00
|
|
|
var voiceName = values["voiceName"];
|
|
|
|
var voiceNameLower = voiceName.ToLower();
|
|
|
|
var voice = _user.VoicesAvailable.First(v => v.Value.ToLower() == voiceNameLower);
|
|
|
|
|
|
|
|
if (_user.VoicesSelected.ContainsKey(chatterId))
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
await hermes.UpdateTTSUser(chatterId, voice.Key);
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]");
|
2024-07-19 12:56:41 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-06 15:29:29 -04:00
|
|
|
await hermes.CreateTTSUser(chatterId, voice.Key);
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]");
|
2024-07-19 12:56:41 -04:00
|
|
|
}
|
2024-07-06 23:42:33 -04:00
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|