2024-07-16 00:48:55 -04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.Chat.Commands.Parameters;
|
2024-07-12 13:36:09 -04:00
|
|
|
using TwitchChatTTS.Chat.Groups;
|
|
|
|
using TwitchChatTTS.Chat.Groups.Permissions;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.Hermes.Socket;
|
2024-07-12 13:36:09 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket.Manager;
|
2024-07-06 23:42:33 -04:00
|
|
|
using TwitchChatTTS.Twitch.Redemptions;
|
2024-06-16 20:19:31 -04:00
|
|
|
using TwitchLib.Client.Models;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Chat.Commands
|
|
|
|
{
|
|
|
|
public class RefreshTTSDataCommand : ChatCommand
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly User _user;
|
2024-07-06 23:42:33 -04:00
|
|
|
private readonly RedemptionManager _redemptionManager;
|
2024-07-12 13:36:09 -04:00
|
|
|
private readonly IGroupPermissionManager _permissionManager;
|
|
|
|
private readonly IChatterGroupManager _chatterGroupManager;
|
|
|
|
private readonly OBSManager _obsManager;
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly HermesApiClient _hermesApi;
|
|
|
|
private readonly ILogger _logger;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
public RefreshTTSDataCommand(
|
|
|
|
User user,
|
|
|
|
RedemptionManager redemptionManager,
|
|
|
|
IGroupPermissionManager permissionManager,
|
|
|
|
IChatterGroupManager chatterGroupManager,
|
|
|
|
OBSManager obsManager,
|
|
|
|
HermesApiClient hermesApi,
|
|
|
|
ILogger logger
|
|
|
|
) : base("refresh", "Refreshes certain TTS related data on the client.")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_user = user;
|
2024-07-06 23:42:33 -04:00
|
|
|
_redemptionManager = redemptionManager;
|
2024-07-12 13:36:09 -04:00
|
|
|
_permissionManager = permissionManager;
|
|
|
|
_chatterGroupManager = chatterGroupManager;
|
|
|
|
_obsManager = obsManager;
|
2024-06-24 18:11:36 -04:00
|
|
|
_hermesApi = hermesApi;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger = logger;
|
2024-07-16 00:48:55 -04:00
|
|
|
|
|
|
|
AddParameter(new SimpleListedParameter([
|
|
|
|
"tts_voice_enabled",
|
|
|
|
"word_filters",
|
|
|
|
"selected_voices",
|
|
|
|
"default_voice",
|
|
|
|
"redemptions",
|
|
|
|
"obs_cache",
|
|
|
|
"permissions"
|
|
|
|
]));
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
public override async Task<bool> CheckDefaultPermissions(ChatMessage message)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
return message.IsModerator || message.IsBroadcaster;
|
|
|
|
}
|
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
public override async Task Execute(IList<string> args, ChatMessage message, HermesSocketClient client)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
var value = args.First().ToLower();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
switch (value)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
case "tts_voice_enabled":
|
2024-06-24 18:11:36 -04:00
|
|
|
var voicesEnabled = await _hermesApi.FetchTTSEnabledVoices();
|
2024-06-16 20:19:31 -04:00
|
|
|
if (voicesEnabled == null || !voicesEnabled.Any())
|
2024-06-24 18:11:36 -04:00
|
|
|
_user.VoicesEnabled = new HashSet<string>(["Brian"]);
|
2024-06-16 20:19:31 -04:00
|
|
|
else
|
2024-06-24 18:11:36 -04:00
|
|
|
_user.VoicesEnabled = new HashSet<string>(voicesEnabled.Select(v => v));
|
|
|
|
_logger.Information($"{_user.VoicesEnabled.Count} TTS voices have been enabled.");
|
2024-06-16 20:19:31 -04:00
|
|
|
break;
|
|
|
|
case "word_filters":
|
2024-06-24 18:11:36 -04:00
|
|
|
var wordFilters = await _hermesApi.FetchTTSWordFilters();
|
|
|
|
_user.RegexFilters = wordFilters.ToList();
|
|
|
|
_logger.Information($"{_user.RegexFilters.Count()} TTS word filters.");
|
2024-06-16 20:19:31 -04:00
|
|
|
break;
|
2024-07-12 13:36:09 -04:00
|
|
|
case "selected_voices":
|
|
|
|
{
|
|
|
|
var voicesSelected = await _hermesApi.FetchTTSChatterSelectedVoices();
|
|
|
|
_user.VoicesSelected = voicesSelected.ToDictionary(s => s.ChatterId, s => s.Voice);
|
|
|
|
_logger.Information($"{_user.VoicesSelected.Count} TTS voices have been selected for specific chatters.");
|
|
|
|
break;
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
case "default_voice":
|
2024-06-24 18:11:36 -04:00
|
|
|
_user.DefaultTTSVoice = await _hermesApi.FetchTTSDefaultVoice();
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Information("TTS Default Voice: " + _user.DefaultTTSVoice);
|
|
|
|
break;
|
|
|
|
case "redemptions":
|
|
|
|
var redemptionActions = await _hermesApi.FetchRedeemableActions();
|
|
|
|
var redemptions = await _hermesApi.FetchRedemptions();
|
|
|
|
_redemptionManager.Initialize(redemptions, redemptionActions.ToDictionary(a => a.Name, a => a));
|
|
|
|
_logger.Information($"Redemption Manager has been refreshed with {redemptionActions.Count()} actions & {redemptions.Count()} redemptions.");
|
2024-06-16 20:19:31 -04:00
|
|
|
break;
|
2024-07-12 13:36:09 -04:00
|
|
|
case "obs_cache":
|
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
_obsManager.ClearCache();
|
|
|
|
await _obsManager.GetGroupList(async groups => await _obsManager.GetGroupSceneItemList(groups));
|
2024-07-12 13:36:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "permissions":
|
|
|
|
{
|
|
|
|
_chatterGroupManager.Clear();
|
|
|
|
_permissionManager.Clear();
|
|
|
|
|
|
|
|
var groups = await _hermesApi.FetchGroups();
|
|
|
|
var groupsById = groups.ToDictionary(g => g.Id, g => g);
|
|
|
|
foreach (var group in groups)
|
|
|
|
_chatterGroupManager.Add(group);
|
|
|
|
_logger.Information($"{groups.Count()} groups have been loaded.");
|
|
|
|
|
|
|
|
var groupChatters = await _hermesApi.FetchGroupChatters();
|
|
|
|
_logger.Debug($"{groupChatters.Count()} group users have been fetched.");
|
|
|
|
|
|
|
|
var permissions = await _hermesApi.FetchGroupPermissions();
|
|
|
|
foreach (var permission in permissions)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Adding group permission [id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}][allow: {permission.Allow?.ToString() ?? "null"}]");
|
|
|
|
if (groupsById.TryGetValue(permission.GroupId, out var group))
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find group by id [id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}]");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var path = $"{group.Name}.{permission.Path}";
|
|
|
|
_permissionManager.Set(path, permission.Allow);
|
|
|
|
_logger.Debug($"Added group permission [id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}]");
|
|
|
|
}
|
|
|
|
_logger.Information($"{permissions.Count()} group permissions have been loaded.");
|
|
|
|
|
|
|
|
foreach (var chatter in groupChatters)
|
|
|
|
if (groupsById.TryGetValue(chatter.GroupId, out var group))
|
|
|
|
_chatterGroupManager.Add(chatter.ChatterId, group.Name);
|
|
|
|
_logger.Information($"Users in each group have been loaded.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
_logger.Warning($"Unknown refresh value given [value: {value}]");
|
|
|
|
break;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|