private void Client_OnChannelMessage(object sender, IrcEventArgs e) { if (string.IsNullOrEmpty(e.Data.Message)) { return; } var from = new SenderDetails { Nickname = e.Data.Nick, Ident = e.Data.Ident, Hostname = e.Data.Host, }; var messageDetails = new MessageDetails(e.Data.Channel, from, e.Data.Message); lock ( monitorsLock ) { foreach (var monitor in registeredMonitors) { monitor.DoMessage(messageDetails); } } }
protected abstract void OnMessage(MessageDetails msgDetails);
protected async override void OnMessage(MessageDetails msgDetails) { if (!Settings.Current.DoesChannelHaveTag(msgDetails.Channel, "spiders")) { // channel isn't configured for spider detection return; } if (string.IsNullOrEmpty(Settings.Current.CognitiveVisionKey)) { // api key is missing, not much to do return; } var urls = GetUrls(msgDetails.Message); var client = new VisionServiceClient(Settings.Current.CognitiveVisionKey, Settings.Current.CognitiveVisionEndpoint); foreach (var url in urls) { AnalysisResult result = null; try { result = await client.AnalyzeImageAsync(url, new[] { VisualFeature.Description, VisualFeature.Tags }); } catch (Exception ex) { Log.WriteError("Spiders", "Error occurred while submitting image for analysis: {0}", ex); return; } var spiderResult = result?.Tags? // order the spider tag first .OrderByDescending(t => string.Equals(t.Name, "spider")) .FirstOrDefault(t => { if (string.Equals(t.Name, "spider")) { return(t.Confidence > 0.40); } if (string.Equals(t.Name, "arthropod")) { return(t.Confidence > 0.80); } return(false); }); if (spiderResult != null) { // WTF if (!string.Equals(spiderResult.Name, "spider")) { // if we didn't directly match a spider, reduce our confidence level spiderResult.Confidence -= 0.40; } double confidencePercent = spiderResult.Confidence * 100.0; IRC.Instance.Send(msgDetails.Channel, $"🕷 Alert! I'm like {confidencePercent:0.00}% sure there's a spider in {url}"); return; } } }
internal void DoMessage(MessageDetails msgDetails) { OnMessage(msgDetails); }