Exemplo n.º 1
0
        public async Task CreateOrUpdateAsync(string key, string value)
        {
            var kv = await _context.KeyValues.FindAsync(key);

            if (kv == null)
            {
                _context.KeyValues.Add(new KeyValue(key, value));
            }
            else
            {
                kv.Value = value;
            }

            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        private async Task AddQuoteAsync(OnChatCommandReceivedArgs e)
        {
            if (e.Command.ArgumentsAsString.Length > 280)
            {
                _client.SendMessage(e, $"@{e.Command.ChatMessage.Username}: Quote is too long, not added.");
                return;
            }

            var quoteRecord = new T
            {
                Quote   = e.Command.ArgumentsAsString.Trim('"'),
                AddedBy = e.Command.ChatMessage.Username,
                AddedAt = DateTime.UtcNow
            };

            _quotes.Add(quoteRecord);

            using (var db = new VbContext())
            {
                db.Set <T>().Add(quoteRecord);
                await db.SaveChangesAsync();
            }

            _client.SendMessage(e, $"@{e.Command.ChatMessage.Username}: {_name} quote added.");
        }
Exemplo n.º 3
0
        public async Task <IActionResult> HandleWebhook([FromQuery] string username, [FromQuery] string userId)
        {
            _logger.LogInformation($"Received Twitch webhook for {username}");

            var bytes = await Request.GetBodyAsBytesAsync();

            if (!Request.Headers.TryGetValue("X-Hub-Signature", out var signature))
            {
                _logger.LogWarning("No X-Hub-Signature header found");
                return(BadRequest());
            }

            if (!TwitchSignatureVerifier.Verify(_twitchConfig.WebhookSecret, signature.ToString(), bytes))
            {
                _logger.LogWarning("X-Hub-Signature is invalid");
                return(BadRequest());
            }

            Request.Headers.TryGetValue("Twitch-Notification-Id", out var notificationId);
            _logger.LogInformation($"The notification's ID is: {notificationId}");
            if (_cache.TryGetValue(notificationId, out _))
            {
                _logger.LogInformation("Notification ID already being tracked, webhook ignored.");
                return(NoContent());
            }

            _cache.Set(notificationId, "", TimeSpan.FromDays(3));

            var bodyString = Encoding.UTF8.GetString(bytes);
            var payload    = JsonSerializer.Deserialize <StreamChangedPayload>(bodyString);

            var notificationMsg = new StreamChangedNotificationMessage(username, userId, payload.Data);
            var msgBody         = JsonSerializer.Serialize(notificationMsg);

            _logger.LogInformation($"Stream status changed for channel {username}");

            await _wsMgr.SendAllAsync(msgBody);

            if (string.Equals(username, "fitzyhere", StringComparison.OrdinalIgnoreCase))
            {
                await _fitzyWsMgr.SendAllAsync(msgBody);
            }

            _context.TwitchWebhookNotifications.Add(new TwitchWebhookNotification
            {
                Id         = notificationId,
                ReceivedAt = DateTimeOffset.UtcNow,
                UserId     = userId,
                Username   = username,
                GameId     = payload.Data?[0]?.GameId,
                Title      = payload.Data?[0]?.Title,
                StartedAt  = payload.Data?[0]?.StartedAt
            });

            await _context.SaveChangesAsync();

            return(NoContent());
        }
Exemplo n.º 4
0
        public static async Task DeleteByKeyAsync(string key)
        {
            using var db = new VbContext();

            var kv = await db.KeyValues.FindAsync(key);

            if (kv != null)
            {
                db.KeyValues.Remove(kv);
                await db.SaveChangesAsync();
            }
        }
        private async Task ClearMultitwitchAsync(OnChatCommandReceivedArgs e)
        {
            using (var db = new VbContext())
            {
                db.MultiStreamers.RemoveRange(_streamers);
                await db.SaveChangesAsync();
            }

            _streamers.Clear();

            GetMultitwitch(e);
        }
        private async Task UpdateMultitwitchAsync(OnChatCommandReceivedArgs e)
        {
            var streamers = e.Command.ArgumentsAsList
                            .Select(s => s.ToLower())
                            .Select(s => s.TrimStart('@'))
                            .Distinct()
                            .ToList();

            var crendorRemoved = streamers.Remove("crendor");

            if (streamers.Count == 0)
            {
                var msg = "You didn't specify any users, you nerd.";
                if (crendorRemoved)
                {
                    msg += " Crendor is automatically added, so he doesn't count.";
                }

                _client.SendMessage(e, msg);
                return;
            }

            var validUsernames = await _api.Helix.Users.GetUsersAsync(logins : streamers);

            if (validUsernames.Users.Length != streamers.Count)
            {
                _client.SendMessage(e, $"At least one of those isn't a valid user, you nerd.");

                return;
            }

            using (var db = new VbContext())
            {
                db.MultiStreamers.RemoveRange(_streamers);
                await db.SaveChangesAsync();
            }

            _streamers.Clear();

            foreach (var s in streamers)
            {
                _streamers.Add(new MultiStreamer(s));
            }

            using (var db = new VbContext())
            {
                db.MultiStreamers.AddRange(_streamers);
                await db.SaveChangesAsync();
            }

            GetMultitwitch(e);
        }
Exemplo n.º 7
0
        public async Task <decimal> AddSlothiesAsync(string userId, decimal count)
        {
            var isNew  = false;
            var record = _slothyRecords.Find(x => x.UserId == userId);

            if (record != null)
            {
                try
                {
                    record.Count += count;
                }
                catch (OverflowException)
                {
                    if (count > 0)
                    {
                        record.Count = decimal.MaxValue;
                    }
                    else
                    {
                        record.Count = decimal.MinValue;
                    }
                }
            }
            else
            {
                isNew  = true;
                record = new SlothyRecord
                {
                    UserId = userId,
                    Count  = count + 10
                };

                _slothyRecords.Add(record);
            }

            using (var db = new VbContext())
            {
                if (isNew)
                {
                    db.Slothies.Add(record);
                }
                else
                {
                    db.Slothies.Update(record);
                }

                await db.SaveChangesAsync();
            }

            return(record.Count);
        }
Exemplo n.º 8
0
        public static async Task CreateOrUpdateAsync(string key, string value)
        {
            using var db = new VbContext();

            var kv = await db.KeyValues.FindAsync(key);

            if (kv == null)
            {
                db.KeyValues.Add(new KeyValue(key, value));
            }
            else
            {
                kv.Value = value;
            }

            await db.SaveChangesAsync();
        }
Exemplo n.º 9
0
        public async Task <bool> ClearBetsAsync()
        {
            _betRecords.Clear();

            try
            {
                using var db = new VbContext();
                db.SlothyBetRecords.RemoveRange(db.SlothyBetRecords);
                await db.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                Utils.LogToConsole($"Error truncating slothy bets: {ex.Message}");
                return(false);
            }
        }
Exemplo n.º 10
0
        public async Task <bool> AddOrUpdateBetAsync(SlothyBetRecord record)
        {
            var update   = false;
            var existing = _betRecords.Find(x => x.UserId == record.UserId);

            if (existing != null)
            {
                _betRecords.Remove(existing);
                update = true;
            }

            _betRecords.Add(record);

            try
            {
                using var db = new VbContext();

                if (!update)
                {
                    db.SlothyBetRecords.Add(record);
                }
                else
                {
                    db.SlothyBetRecords.Update(record);
                }

                await db.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                Utils.LogToConsole($"Error saving slothy bet record to DB: {ex.Message}");
                return(false);
            }
        }