private bool CommandsTooFast(ChatMessageEventArgs args, ChatUserInfo user, string namedCommand) { if (!args.IsModerator && !args.IsOwner) { if (DateTime.UtcNow - user.LastCommandTime < CooldownTime) { _logger.LogWarning($"Ignoring command {namedCommand} from {args.UserName} on {args.ServiceName}. Cooldown active"); return(true); } } return(false); }
private async void Chat_ChatMessage(object sender, ChatMessageEventArgs e) { if (string.IsNullOrEmpty(e.Message) || e.Message[0] != COMMAND_PREFIX) { return; // e.Message.StartsWith(...) did not work for some reason ?!? } var segments = e.Message.Substring(1).Split(' ', StringSplitOptions.RemoveEmptyEntries); if (segments.Length == 0) { return; } var chatService = sender as IChatService; Debug.Assert(chatService != null); if (!chatService.IsAuthenticated) { return; } // Ignore if the normal user is sending commands to fast var userKey = $"{e.ServiceName}:{e.UserName}"; if (!_activeUsers.TryGetValue(userKey, out var user)) { user = new ChatUserInfo(); } if (!e.IsModerator && !e.IsOwner) { if (DateTime.UtcNow - user.LastCommandTime < _cooldownTime) { _logger.LogWarning($"Ignoring command {segments[0]} from {e.UserName} on {e.ServiceName}. Cooldown active"); return; } } _logger.LogInformation($"!{segments[0]} from {e.UserName} on {e.ServiceName}"); // Handle commands switch (segments[0].ToLowerInvariant()) { case "help": await chatService.SendMessageAsync("Supported commands: !ping !echo !uptime !quote"); break; case "ping": await chatService.SendWhisperAsync(e.UserName, "pong"); break; case "echo": if (segments.Length < 2) { return; } await chatService.SendWhisperAsync(e.UserName, "Echo reply: " + string.Join(' ', segments.Skip(1))); break; case "uptime": { // Get uptime from the mixer stream service var mixer = Array.Find(_streamServices, x => x.Name == "Mixer"); if (mixer == null) { break; } if (mixer.Uptime.HasValue) { await chatService.SendMessageAsync($"The stream has been up for {mixer.Uptime.Value}"); } else { await chatService.SendMessageAsync("Stream is offline"); } break; } case "quote": if (_quotes == null) { break; } await chatService.SendMessageAsync(_quotes[_random.Next(_quotes.Length)]); break; default: return; // Unknown command } // Remember last command time user.LastCommandTime = DateTime.UtcNow; _activeUsers.AddOrUpdate(userKey, user, (k, v) => user); }
private async Task Chat_ChatMessage(object sender, ChatMessageEventArgs e) { // message is empty OR message doesn't start with ! AND doesn't end with ? if (e.Message.EndsWith("?")) { _logger.LogInformation($"Handling question: \"{e.Message}\" from {e.UserName} on {e.ServiceName}"); var azureUserKey = $"{e.ServiceName}:{e.UserName}"; if (!_activeUsers.TryGetValue(azureUserKey, out var azureUser)) { azureUser = new ChatUserInfo(); } if (CommandsTooFast(e, azureUser, "qna")) { return; } await HandleAzureQuestion(e.Message, e.UserName, sender as IChatService); return; } if (string.IsNullOrEmpty(e.Message) || (e.Message[0] != COMMAND_PREFIX & !e.Message.EndsWith("?"))) { return; // e.Message.StartsWith(...) did not work for some reason ?!? } var segments = e.Message.Substring(1).Split(' ', StringSplitOptions.RemoveEmptyEntries); if (segments.Length == 0) { return; } var chatService = sender as IChatService; Debug.Assert(chatService != null); if (!chatService.IsAuthenticated) { return; } var userKey = $"{e.ServiceName}:{e.UserName}"; if (!_activeUsers.TryGetValue(userKey, out var user)) { user = new ChatUserInfo(); } // Ignore if the normal user is sending commands to fast if (CommandsTooFast(e, user, segments[0])) { return; } _logger.LogInformation($"!{segments[0]} from {e.UserName} on {e.ServiceName}"); // Handle commands ICommand cmd = null; if (_CommandRegistry.TryGetValue(segments[0].ToLowerInvariant(), out cmd)) { cmd.ChatService = chatService; await cmd.Execute(e.UserName, e.Message); } else { await chatService.SendWhisperAsync(e.UserName, "Unknown command. Try !help for a list of available commands"); return; } // Remember last command time user.LastCommandTime = DateTime.UtcNow; _activeUsers.AddOrUpdate(userKey, user, (k, v) => user); }
private async Task Chat_ChatMessage(object sender, ChatMessageEventArgs e) { var userKey = $"{e.ServiceName}:{e.UserName}"; ChatUserInfo user; if (!_activeUsers.TryGetValue(userKey, out user)) { user = new ChatUserInfo(); } // message is empty OR message doesn't start with ! AND doesn't end with ? if (e.Message.EndsWith("?")) { _logger.LogInformation($"Handling question: \"{e.Message}\" from {e.UserName} on {e.ServiceName}"); if (CommandsTooFast("qna")) { return; } await HandleAzureQuestion(e.Message, e.UserName, sender as IChatService); return; } // Check for image processing var imageCheckPattern = @"http(s)?:?(\/\/[^""']*\.(?:png|jpg|jpeg|gif))"; var r = new Regex(imageCheckPattern, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. var imageCheck = r.Match(e.Message); if (imageCheck.Captures.Count > 0) { //cal the new comand var imageDescCommand = new ImageDescriptorCommand(_config); imageDescCommand.ChatService = sender as IChatService; await imageDescCommand.Execute(e.UserName, imageCheck.Captures[0].Value); return; } if (string.IsNullOrEmpty(e.Message) || (e.Message[0] != COMMAND_PREFIX & !e.Message.EndsWith("?"))) { return; // e.Message.StartsWith(...) did not work for some reason ?!? } var segments = e.Message.Substring(1).Split(' ', StringSplitOptions.RemoveEmptyEntries); if (segments.Length == 0) { return; } var chatService = sender as IChatService; Debug.Assert(chatService != null); if (!chatService.IsAuthenticated) { return; } // Ignore if the normal user is sending commands to fast if (CommandsTooFast(segments[0])) { return; } _logger.LogInformation($"!{segments[0]} from {e.UserName} on {e.ServiceName}"); // Handle commands ICommand cmd = null; if (_CommandRegistry.TryGetValue(segments[0].ToLowerInvariant(), out cmd)) { cmd.ChatService = chatService; await cmd.Execute(e.UserName, e.Message); } else { await chatService.SendWhisperAsync(e.UserName, "Unknown command. Try !help for a list of available commands"); return; } // Remember last command time user.LastCommandTime = DateTime.UtcNow; _activeUsers.AddOrUpdate(userKey, user, (k, v) => user); bool CommandsTooFast(string namedCommand) { if (!e.IsModerator && !e.IsOwner) { if (DateTime.UtcNow - user.LastCommandTime < CooldownTime) { _logger.LogWarning($"Ignoring command {namedCommand} from {e.UserName} on {e.ServiceName}. Cooldown active"); return(true); } } return(false); } }