2024-08-04 19:46:10 -04:00
|
|
|
using Serilog;
|
2024-08-12 15:45:17 -04:00
|
|
|
using TwitchChatTTS.Chat.Commands;
|
|
|
|
using TwitchChatTTS.Chat.Groups;
|
|
|
|
using TwitchChatTTS.Chat.Groups.Permissions;
|
2024-08-12 01:44:31 -04:00
|
|
|
using TwitchChatTTS.Chat.Messaging;
|
2024-08-04 19:46:10 -04:00
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Twitch.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class ChannelChatMessageHandler : ITwitchSocketHandler
|
|
|
|
{
|
|
|
|
public string Name => "channel.chat.message";
|
|
|
|
|
2024-08-12 01:44:31 -04:00
|
|
|
private readonly ChatMessageReader _reader;
|
2024-08-12 15:45:17 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private readonly ICommandManager _commands;
|
|
|
|
private readonly IGroupPermissionManager _permissionManager;
|
|
|
|
private readonly IChatterGroupManager _chatterGroupManager;
|
2024-08-04 19:46:10 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public ChannelChatMessageHandler(
|
2024-08-12 01:44:31 -04:00
|
|
|
ChatMessageReader reader,
|
2024-08-12 15:45:17 -04:00
|
|
|
ICommandManager commands,
|
|
|
|
IGroupPermissionManager permissionManager,
|
|
|
|
IChatterGroupManager chatterGroupManager,
|
|
|
|
User user,
|
2024-08-04 19:46:10 -04:00
|
|
|
ILogger logger
|
|
|
|
)
|
|
|
|
{
|
2024-08-12 01:44:31 -04:00
|
|
|
_reader = reader;
|
2024-08-12 15:45:17 -04:00
|
|
|
_user = user;
|
|
|
|
_commands = commands;
|
|
|
|
_permissionManager = permissionManager;
|
|
|
|
_chatterGroupManager = chatterGroupManager;
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-08-12 01:44:31 -04:00
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task Execute(TwitchWebsocketClient sender, object data)
|
2024-08-04 19:46:10 -04:00
|
|
|
{
|
|
|
|
if (sender == null)
|
|
|
|
return;
|
|
|
|
if (data is not ChannelChatMessage message)
|
|
|
|
return;
|
|
|
|
|
2024-08-12 15:45:17 -04:00
|
|
|
var broadcasterId = long.Parse(message.BroadcasterUserId);
|
|
|
|
var chatterId = long.Parse(message.ChatterUserId);
|
|
|
|
var chatterLogin = message.ChatterUserLogin;
|
|
|
|
var messageId = message.MessageId;
|
|
|
|
var fragments = message.Message.Fragments;
|
|
|
|
var groups = GetGroups(message.Badges, chatterId);
|
|
|
|
var bits = GetTotalBits(fragments);
|
|
|
|
|
|
|
|
var commandResult = await CheckForChatCommand(message.Message.Text, message, groups);
|
|
|
|
if (commandResult != ChatCommandResult.Unknown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!HasPermission(message.ChannelPointsCustomRewardId, chatterId, groups, bits))
|
|
|
|
{
|
|
|
|
_logger.Debug($"Blocked message by {chatterLogin}: {message}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int priority = _chatterGroupManager.GetPriorityFor(groups);
|
|
|
|
await _reader.Read(sender, broadcasterId, chatterId, chatterLogin, messageId, message.Reply, fragments, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<ChatCommandResult> CheckForChatCommand(string arguments, ChannelChatMessage message, IEnumerable<string> groups)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return await _commands.Execute(arguments, message, groups);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, $"Failed executing a chat command [message: {arguments}][chatter: {message.ChatterUserLogin}][chatter id: {message.ChatterUserId}][message id: {message.MessageId}]");
|
|
|
|
}
|
|
|
|
return ChatCommandResult.Fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetGroupNameByBadgeName(string badgeName)
|
|
|
|
{
|
|
|
|
if (badgeName == "subscriber")
|
|
|
|
return "subscribers";
|
|
|
|
if (badgeName == "moderator")
|
|
|
|
return "moderators";
|
|
|
|
return badgeName.ToLower();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<string> GetGroups(TwitchBadge[] badges, long chatterId)
|
|
|
|
{
|
|
|
|
var defaultGroups = new string[] { "everyone" };
|
|
|
|
var badgesGroups = badges.Select(b => b.SetId).Select(GetGroupNameByBadgeName);
|
|
|
|
var customGroups = _chatterGroupManager.GetGroupNamesFor(chatterId);
|
|
|
|
return defaultGroups.Union(badgesGroups).Union(customGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetTotalBits(TwitchChatFragment[] fragments)
|
|
|
|
{
|
|
|
|
return fragments.Where(f => f.Type == "cheermote" && f.Cheermote != null)
|
|
|
|
.Select(f => f.Cheermote!.Bits)
|
|
|
|
.Sum();
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool HasPermission(string? customRewardId, long chatterId, IEnumerable<string> groups, int bits)
|
|
|
|
{
|
|
|
|
var permissionPath = "tts.chat.messages.read";
|
|
|
|
if (!string.IsNullOrWhiteSpace(customRewardId))
|
|
|
|
permissionPath = "tts.chat.redemptions.read";
|
|
|
|
else if (bits > 0)
|
|
|
|
permissionPath = "tts.chat.bits.read";
|
|
|
|
|
|
|
|
return chatterId == _user.OwnerId ? true : _permissionManager.CheckIfAllowed(groups, permissionPath) == true;
|
2024-08-04 19:46:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|