Fixed raid spam prevention. Gave a proper error message when connecting to Twitch websockets without linking Twitch account to Twitch.
This commit is contained in:
@ -25,7 +25,8 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
if (data is not ChannelRaidMessage message)
|
||||
return;
|
||||
|
||||
var chatters = await _api.GetChatters(message.ToBroadcasterUserId, message.ToBroadcasterUserLogin);
|
||||
_logger.Information($"A raid has started. Starting raid spam prevention. [from: {message.FromBroadcasterUserLogin}][from id: {message.FromBroadcasterUserId}].");
|
||||
var chatters = await _api.GetChatters(_user.TwitchUserId.ToString(), _user.TwitchUserId.ToString());
|
||||
if (chatters?.Data == null)
|
||||
{
|
||||
_logger.Error("Could not fetch the list of chatters in chat.");
|
||||
@ -43,6 +44,11 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
}
|
||||
}
|
||||
|
||||
Task.Run(EndOfRaidSpamProtection);
|
||||
}
|
||||
|
||||
private async Task EndOfRaidSpamProtection()
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
||||
|
||||
lock (_lock)
|
||||
|
@ -33,10 +33,11 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
_messageTypes.Add("channel.adbreak.begin", typeof(ChannelAdBreakMessage));
|
||||
_messageTypes.Add("channel.ban", typeof(ChannelBanMessage));
|
||||
_messageTypes.Add("channel.chat.message", typeof(ChannelChatMessage));
|
||||
_messageTypes.Add("channel.chat.clear_user_messages", typeof(ChannelChatClearUserMessage));
|
||||
_messageTypes.Add("channel.chat.clear", typeof(ChannelChatClearMessage));
|
||||
_messageTypes.Add("channel.chat.clear_user_messages", typeof(ChannelChatClearUserMessage));
|
||||
_messageTypes.Add("channel.chat.message_delete", typeof(ChannelChatDeleteMessage));
|
||||
_messageTypes.Add("channel.channel_points_custom_reward_redemption.add", typeof(ChannelCustomRedemptionMessage));
|
||||
_messageTypes.Add("channel.raid", typeof(ChannelRaidMessage));
|
||||
_messageTypes.Add("channel.follow", typeof(ChannelFollowMessage));
|
||||
_messageTypes.Add("channel.subscribe", typeof(ChannelSubscriptionMessage));
|
||||
_messageTypes.Add("channel.subscription.message", typeof(ChannelResubscriptionMessage));
|
||||
|
@ -31,9 +31,17 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
return;
|
||||
}
|
||||
|
||||
await _hermes.AuthorizeTwitch();
|
||||
var token = await _hermes.FetchTwitchBotToken();
|
||||
_api.Initialize(token);
|
||||
try
|
||||
{
|
||||
await _hermes.AuthorizeTwitch();
|
||||
var token = await _hermes.FetchTwitchBotToken();
|
||||
_api.Initialize(token);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_logger.Error("Ensure you have your Twitch account linked on TTS. Restart application once you do.");
|
||||
return;
|
||||
}
|
||||
|
||||
string broadcasterId = _user.TwitchUserId.ToString();
|
||||
string[] subscriptionsv1 = [
|
||||
@ -46,8 +54,7 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
"channel.subscription.message",
|
||||
"channel.ad_break.begin",
|
||||
"channel.ban",
|
||||
"channel.channel_points_custom_reward_redemption.add",
|
||||
"channel.raid"
|
||||
"channel.channel_points_custom_reward_redemption.add"
|
||||
];
|
||||
string[] subscriptionsv2 = [
|
||||
"channel.follow",
|
||||
@ -77,6 +84,8 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "1");
|
||||
foreach (var subscription in subscriptionsv2)
|
||||
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "2");
|
||||
|
||||
await Subscribe(sender, "channel.raid", broadcasterId, async () => await _api.CreateChannelRaidEventSubscription("1", message.Session.Id, to: broadcasterId));
|
||||
|
||||
sender.Identify(message.Session.Id);
|
||||
}
|
||||
@ -111,5 +120,36 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
_logger.Error(ex, $"Failed to create an event subscription [subscription type: {subscriptionName}][reason: exception]");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Subscribe(TwitchWebsocketClient sender, string subscriptionName, string broadcasterId, Func<Task<EventResponse<NotificationInfo>?>> subscribe)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await subscribe();
|
||||
if (response == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (response.Data == null)
|
||||
{
|
||||
_logger.Error($"Failed to create an event subscription [subscription type: {subscriptionName}][reason: data is null]");
|
||||
return;
|
||||
}
|
||||
if (!response.Data.Any())
|
||||
{
|
||||
_logger.Error($"Failed to create an event subscription [subscription type: {subscriptionName}][reason: data is empty]");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var d in response.Data)
|
||||
sender.AddSubscription(broadcasterId, d.Type, d.Id);
|
||||
|
||||
_logger.Information($"Sucessfully added subscription to Twitch websockets [subscription type: {subscriptionName}]");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, $"Failed to create an event subscription [subscription type: {subscriptionName}][reason: exception]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user