|
| 1 | +using Fritz.StreamLib.Core; |
| 2 | +using Fritz.StreamTools.Hubs; |
| 3 | +using Microsoft.AspNetCore.SignalR; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Linq; |
| 11 | +using System.Net.Http; |
| 12 | +using System.Text; |
| 13 | +using System.Threading.Tasks; |
| 14 | + |
| 15 | +namespace Fritz.Chatbot.Commands |
| 16 | +{ |
| 17 | + public class TeamCommand : IExtendedCommand |
| 18 | + { |
| 19 | + |
| 20 | + private static HashSet<string> _Teammates = new HashSet<string>(); |
| 21 | + private static Dictionary<string, DateTime> _TeammateCooldown = new Dictionary<string, DateTime>(); |
| 22 | + private string _TeamName; |
| 23 | + private HttpClient _HttpClient; |
| 24 | + private readonly IHubContext<AttentionHub> _Context; |
| 25 | + private ILogger _Logger; |
| 26 | + |
| 27 | + public string Name { get; } = "Team Detection"; |
| 28 | + public string Description { get; } = "Alert when a teammate joins the stream and starts chatting"; |
| 29 | + public int Order { get; } = 1; |
| 30 | + public bool Final { get; } = false; |
| 31 | + public TimeSpan? Cooldown { get; } = TimeSpan.FromSeconds(5); |
| 32 | + public TimeSpan ShoutoutCooldown; |
| 33 | + public string ShoutoutFormat; |
| 34 | + public Queue<string> _TeammateNotifications = new Queue<string>(); |
| 35 | + |
| 36 | + public TeamCommand(IConfiguration configuration, ILoggerFactory loggerFactory, IHubContext<AttentionHub> context, IHttpClientFactory httpClientFactory) |
| 37 | + { |
| 38 | + _TeamName = configuration["StreamServices:Twitch:Team"]; |
| 39 | + ShoutoutCooldown = configuration.GetValue("StreamServices:Twitch:TeamCooldown", TimeSpan.FromHours(1)); |
| 40 | + ShoutoutFormat = configuration.GetValue("StreamServices:Twitch:TeamShoutoutFormat", ""); |
| 41 | + _Context = context; |
| 42 | + _Logger = loggerFactory.CreateLogger(nameof(TeamCommand)); |
| 43 | + |
| 44 | + if (!string.IsNullOrEmpty(TwitchTokenConfig.Tokens?.access_token)) |
| 45 | + { |
| 46 | + _HttpClient = httpClientFactory.CreateClient("TeamLookup"); |
| 47 | + _HttpClient.BaseAddress = new Uri($"https://api.twitch.tv/kraken/teams/"); |
| 48 | + _HttpClient.DefaultRequestHeaders.Add("Client-ID", configuration["StreamServices:Twitch:ClientId"]); |
| 49 | + _HttpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.twitchtv.v5+json"); |
| 50 | + |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + _Logger.LogError("Unable to create HttpClient for Twitch with Bearer token"); |
| 55 | + } |
| 56 | + |
| 57 | + GetTeammates().GetAwaiter().GetResult(); |
| 58 | + |
| 59 | + Task.Run(SendNotificationsToWidget); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private void SendNotificationsToWidget() |
| 64 | + { |
| 65 | + |
| 66 | + while (true) { |
| 67 | + |
| 68 | + if (_TeammateNotifications.TryPeek(out var _)) { |
| 69 | + |
| 70 | + _Context.Clients.All.SendAsync("Teammate", _TeammateNotifications.Dequeue()); |
| 71 | + Task.Delay(5000); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public bool CanExecute(string userName, string fullCommandText) |
| 80 | + { |
| 81 | + |
| 82 | + var u = userName.ToLowerInvariant(); |
| 83 | + var isTeammate = _Teammates.Contains(u); |
| 84 | + var recentShoutout = _TeammateCooldown.ContainsKey(u) && (DateTime.UtcNow.Subtract(_TeammateCooldown[u]) < ShoutoutCooldown); |
| 85 | + |
| 86 | + return isTeammate && !recentShoutout; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + public async Task Execute(IChatService chatService, string userName, string fullCommandText) |
| 91 | + { |
| 92 | + _TeammateCooldown[userName.ToLowerInvariant()] = DateTime.UtcNow; |
| 93 | + if (ShoutoutFormat != "") |
| 94 | + { |
| 95 | + await chatService.SendMessageAsync(ShoutoutFormat.Replace("{teammate}", userName)); |
| 96 | + } |
| 97 | + |
| 98 | + _TeammateNotifications.Enqueue(userName); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private async Task GetTeammates() |
| 103 | + { |
| 104 | + |
| 105 | + var response = await _HttpClient.GetStringAsync(_TeamName); |
| 106 | + var team = JsonConvert.DeserializeObject<TeamResponse>(response); |
| 107 | + |
| 108 | + _Teammates = team.users.Select(u => u.name).ToHashSet(); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + internal class TeamResponse |
| 114 | + { |
| 115 | + public int _id { get; set; } |
| 116 | + public object background { get; set; } |
| 117 | + public string banner { get; set; } |
| 118 | + public DateTime created_at { get; set; } |
| 119 | + public string display_name { get; set; } |
| 120 | + public string info { get; set; } |
| 121 | + public string logo { get; set; } |
| 122 | + public string name { get; set; } |
| 123 | + public DateTime updated_at { get; set; } |
| 124 | + public User[] users { get; set; } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + internal class User |
| 129 | + { |
| 130 | + public int _id { get; set; } |
| 131 | + public string broadcaster_language { get; set; } |
| 132 | + public DateTime created_at { get; set; } |
| 133 | + public string display_name { get; set; } |
| 134 | + public int followers { get; set; } |
| 135 | + public string game { get; set; } |
| 136 | + public string language { get; set; } |
| 137 | + public string logo { get; set; } |
| 138 | + public bool mature { get; set; } |
| 139 | + public string name { get; set; } |
| 140 | + public bool partner { get; set; } |
| 141 | + public string profile_banner { get; set; } |
| 142 | + public object profile_banner_background_color { get; set; } |
| 143 | + public string status { get; set; } |
| 144 | + public DateTime updated_at { get; set; } |
| 145 | + public string url { get; set; } |
| 146 | + public object video_banner { get; set; } |
| 147 | + public int views { get; set; } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | +} |
0 commit comments