From ed68cc47e685611c5df14f691ec04f7b7c5129b6 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 6 Aug 2024 22:50:45 +0000 Subject: [PATCH] Apply TTS skips from message deletion & bans when merging with other chats. Fixed group permissions. --- Chat/Commands/CommandManager.cs | 2 +- Chat/Commands/TTSCommand.cs | 56 ++++++++++++++----- .../Permissions/GroupPermissionManager.cs | 10 ++-- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/Chat/Commands/CommandManager.cs b/Chat/Commands/CommandManager.cs index 545cf1a..e21e3f6 100644 --- a/Chat/Commands/CommandManager.cs +++ b/Chat/Commands/CommandManager.cs @@ -69,7 +69,7 @@ namespace TwitchChatTTS.Chat.Commands // Check if command can be executed by this chatter. var command = selectorResult.Command; long chatterId = long.Parse(message.ChatterUserId); - //if (chatterId != _user.OwnerId) + if (chatterId != _user.OwnerId) { bool executable = command.AcceptCustomPermission ? CanExecute(chatterId, groups, $"tts.commands.{com}", selectorResult.Permissions) : false; if (!executable) diff --git a/Chat/Commands/TTSCommand.cs b/Chat/Commands/TTSCommand.cs index 07b452b..f7c2c64 100644 --- a/Chat/Commands/TTSCommand.cs +++ b/Chat/Commands/TTSCommand.cs @@ -214,8 +214,27 @@ namespace TwitchChatTTS.Chat.Commands return; } - await _client.CreateEventSubscription("channel.chat.message", "1", _twitch.SessionId, _user.TwitchUserId.ToString(), fragment.Mention!.UserId); - _logger.Information($"Joined chat room [channel: {fragment.Mention.UserLogin}][channel id: {fragment.Mention.UserId}][invoker: {message.ChatterUserLogin}][id: {message.ChatterUserId}]"); + string targetUserId = fragment.Mention!.UserId!; + if (targetUserId == _user.TwitchUserId.ToString()) + { + _logger.Warning("Cannot join yourself."); + return; + } + + string[] subscriptions = ["channel.chat.message", "channel.chat.message_delete", "channel.chat.clear_user_messages"]; + foreach (var subscription in subscriptions) + { + _logger.Debug($"Attempting to subscribe to Twitch events [subscription: {subscription}]"); + var data = await _client.CreateEventSubscription(subscription, "1", _twitch.SessionId, _user.TwitchUserId.ToString(), targetUserId); + var info = data?.Data?.FirstOrDefault(); + if (info == null) + { + _logger.Warning("Could not find the subscription id."); + continue; + } + _twitch.AddSubscription(targetUserId, subscription, info.Id); + } + _logger.Information($"Joined chat room [channel: {fragment.Mention.UserLogin}][channel id: {targetUserId}][invoker: {message.ChatterUserLogin}][id: {message.ChatterUserId}]"); } } @@ -251,22 +270,33 @@ namespace TwitchChatTTS.Chat.Commands return; } - var subscriptionId = _twitch.GetSubscriptionId(_user.TwitchUserId.ToString(), "channel.chat.message"); - if (subscriptionId == null) + string targetUserId = fragment.Mention!.UserId!; + if (targetUserId == _user.TwitchUserId.ToString()) { - _logger.Warning("Cannot find the subscription for that channel."); + _logger.Warning("Cannot join yourself."); return; } - try + string[] subscriptions = ["channel.chat.message", "channel.chat.message_delete", "channel.chat.clear_user_messages"]; + foreach (var subscription in subscriptions) { - await _client.DeleteEventSubscription(subscriptionId); - _twitch.RemoveSubscription(fragment.Mention.UserId, "channel.chat.message"); - _logger.Information($"Joined chat room [channel: {fragment.Mention.UserLogin}][channel id: {fragment.Mention.UserId}][invoker: {message.ChatterUserLogin}][id: {message.ChatterUserId}]"); - } - catch (Exception ex) - { - _logger.Error(ex, "Failed to delete the subscription from Twitch."); + var subscriptionId = _twitch.GetSubscriptionId(targetUserId, subscription); + if (subscriptionId == null) + { + _logger.Warning($"Cannot find the subscription for that channel [subscription: {subscription}]"); + continue; + } + + try + { + await _client.DeleteEventSubscription(subscriptionId); + _twitch.RemoveSubscription(targetUserId, subscription); + _logger.Information($"Left chat room [channel: {fragment.Mention.UserLogin}][channel id: {targetUserId}][invoker: {message.ChatterUserLogin}][id: {message.ChatterUserId}]"); + } + catch (Exception ex) + { + _logger.Error(ex, $"Failed to delete the subscription from Twitch [subscription: {subscription}][subscription id: {subscriptionId}]"); + } } } } diff --git a/Chat/Groups/Permissions/GroupPermissionManager.cs b/Chat/Groups/Permissions/GroupPermissionManager.cs index 0403df5..bf969f0 100644 --- a/Chat/Groups/Permissions/GroupPermissionManager.cs +++ b/Chat/Groups/Permissions/GroupPermissionManager.cs @@ -36,16 +36,16 @@ namespace TwitchChatTTS.Chat.Groups.Permissions public bool? CheckIfAllowed(IEnumerable groups, string path) { - bool overall = true; + bool overall = false; foreach (var group in groups) { var result = CheckIfAllowed($"{group}.{path}"); - if (result == true) - return true; if (result == false) - overall = false; + return false; + if (result == true) + overall = true; } - return overall ? null : false; + return overall ? true : null; } public bool? CheckIfDirectAllowed(IEnumerable groups, string path)